-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(plugin-md-power): optimize
collapsed-lines
(#180)
- Loading branch information
1 parent
be47c9a
commit 0f1ffc7
Showing
5 changed files
with
86 additions
and
25 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
plugins/plugin-shikiji/src/node/markdown/collapsedLinesPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { Markdown } from 'vuepress/markdown' | ||
import { resolveCollapsedLines } from '../utils/index.js' | ||
|
||
export interface MarkdownItCollapsedLinesOptions { | ||
/** | ||
* Whether to collapse code blocks when they exceed a certain number of lines, | ||
* | ||
* - If `number`, collapse starts from line `number`. | ||
* - If `true`, collapse starts from line 15 by default. | ||
* - If `false`, disable collapse. | ||
* @default false | ||
*/ | ||
collapsedLines?: boolean | number | ||
} | ||
|
||
export function collapsedLinesPlugin(md: Markdown, { | ||
collapsedLines: collapsedLinesOptions = false, | ||
}: MarkdownItCollapsedLinesOptions = {}): void { | ||
const rawFence = md.renderer.rules.fence! | ||
|
||
md.renderer.rules.fence = (...args) => { | ||
const [tokens, index] = args | ||
const token = tokens[index] | ||
// get token info | ||
const info = token.info ? md.utils.unescapeAll(token.info).trim() : '' | ||
const code = rawFence(...args) | ||
|
||
// resolve collapsed-lines mark from token info | ||
const collapsedLinesInfo | ||
= resolveCollapsedLines(info) ?? collapsedLinesOptions | ||
|
||
if (collapsedLinesInfo === false) { | ||
return code | ||
} | ||
|
||
const lines | ||
= code.slice(code.indexOf('<code>'), code.indexOf('</code>')).split('\n').length | ||
|
||
const startLines | ||
= typeof collapsedLinesInfo === 'number' ? collapsedLinesInfo : 15 | ||
|
||
if (lines < startLines) { | ||
return code | ||
} | ||
|
||
const collapsedLinesCode = `<div class="collapsed-lines"></div>` | ||
const styles = `--vp-collapsed-lines:${startLines};` | ||
|
||
const finalCode = code | ||
.replace(/<\/div>$/, `${collapsedLinesCode}</div>`) | ||
.replace(/"(language-[^"]*)"/, '"$1 has-collapsed collapsed"') | ||
.replace(/^<div[^>]*>/, (match) => { | ||
if (!match.includes('style=')) { | ||
return `${match.slice(0, -1)} style="${styles}">` | ||
} | ||
return match.replace(/(style=")/, `$1${styles}`) | ||
}) | ||
|
||
return finalCode | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './collapsedLinesPlugin.js' | ||
export * from './highlightLinesPlugin.js' | ||
export * from './lineNumberPlugin.js' | ||
export * from './preWrapperPlugin.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
export const COLLAPSED_LINES_REGEXP = /:collapsed-lines(?:=(\d+))?\b/ | ||
export const NO_COLLAPSED_LINES_REGEXP = /:no-collapsed-lines\b/ | ||
const COLLAPSED_LINES_REGEXP = /:collapsed-lines\b/ | ||
const COLLAPSED_LINES_START_REGEXP = /:collapsed-lines=(\d+)\b/ | ||
const NO_COLLAPSED_LINES_REGEXP = /:no-collapsed-lines\b/ | ||
|
||
const DEFAULT_LINES = 15 | ||
/** | ||
* Resolve the `:collapsed-lines` `:collapsed-lines=num` / `:no-collapsed-lines` mark from token info | ||
*/ | ||
export function resolveCollapsedLines(info: string): boolean | number | null { | ||
const lines = COLLAPSED_LINES_START_REGEXP.exec(info)?.[1] | ||
|
||
export function resolveCollapsedLines(info: string, defaultLines: boolean | number): number | false { | ||
if (NO_COLLAPSED_LINES_REGEXP.test(info)) | ||
return false | ||
|
||
const lines = defaultLines === true ? DEFAULT_LINES : defaultLines | ||
if (lines) { | ||
return Number(lines) | ||
} | ||
|
||
const match = info.match(COLLAPSED_LINES_REGEXP) | ||
if (COLLAPSED_LINES_REGEXP.test(info)) { | ||
return true | ||
} | ||
|
||
if (match) { | ||
return Number(match[1]) || lines || DEFAULT_LINES | ||
if (NO_COLLAPSED_LINES_REGEXP.test(info)) { | ||
return false | ||
} | ||
return lines ?? false | ||
|
||
return null | ||
} |