Skip to content

Commit

Permalink
chore: tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Sep 15, 2024
1 parent e3cf4a0 commit 1ff4ed1
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 78 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export default defineUserConfig({
notationHighlight: true,
notationWordHighlight: true,
whitespace: true,
collapsedLines: false,
})
: [],
cachePlugin(),
Expand Down
1 change: 1 addition & 0 deletions docs/.vuepress/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default defaultTheme({
notationHighlight: true,
notationWordHighlight: true,
whitespace: true,
collapsedLines: false,
},
},
})
7 changes: 4 additions & 3 deletions docs/plugins/markdown/prismjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ export default defineUserConfig({

### collapsedLines

- Type: `boolean | number`
- Type: `boolean | number | 'disabled'`

- Default: `false`
- Default: `'disabled'`

- Details: Whether to enable code block collapsing.

- `number`: collapse the code block starting from line `number`, for example, `12` means collapsing the code block starting from line 12.
- `true`: Equivalent to `15`, collapsing the code block starting from line 15.
- `false`: Disable code block collapsing.
- `false`: Disable code block collapsed, but `:collapsed-lines` can still be added to collapse individual code blocks.
- `'disabled'`: Completely disable code block collapsed, `:collapsed-lines` will not take effect.

You can add the `:collapsed-lines` / `:no-collapsed-lines` marker to the code block to override the settings in the configuration item. You can also add `=` after `:collapsed-lines` to customize the starting collapsed line number, for example, `:collapsed-lines=12` means collapsing the code block starting from line 12.

Expand Down
7 changes: 4 additions & 3 deletions docs/plugins/markdown/shiki.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,16 @@ export default defineUserConfig({

### collapsedLines

- Type: `boolean | number`
- Type: `boolean | number | 'disabled'`

- Default: `false`
- Default: `'disabled'`

- Details: Whether to enable code block collapsing.

- `number`: collapse the code block starting from line `number`, for example, `12` means collapsing the code block starting from line 12.
- `true`: Equivalent to `15`, collapsing the code block starting from line 15.
- `false`: Disable code block collapsing.
- `false`: Disable code block collapsed, but `:collapsed-lines` can still be added to collapse individual code blocks.
- `'disabled'`: Completely disable code block collapsed, `:collapsed-lines` will not take effect.

You can add the `:collapsed-lines` / `:no-collapsed-lines` marker to the code block to override the settings in the configuration item. You can also add `=` after `:collapsed-lines` to customize the starting collapsed line number, for example, `:collapsed-lines=12` means collapsing the code block starting from line 12.

Expand Down
7 changes: 4 additions & 3 deletions docs/zh/plugins/markdown/prismjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ export default defineUserConfig({

### collapsedLines

- 类型:`boolean | number`
- 类型:`boolean | number | 'disabled'`

- 默认值:`false`
- 默认值:`'disabled`

- 详情:是否启用代码块折叠。

- `number`: 从第 `number` 行开始折叠代码块,例如,`12` 表示从第 12 行开始折叠代码块。
- `true`: 等同于 `15`, 从第 15 行开始折叠代码块。
- `false`: 禁用代码块折叠。
- `false`: 不启用代码块折叠,但仍然可以添加 `:collapsed-lines` 标记为单个代码块折叠。
- `'disabled'`: 完全禁用代码块折叠, `:collapsed-lines` 标记不会生效。

你可以在代码块添加 `:collapsed-lines` / `:no-collapsed-lines` 标记来覆盖配置项中的设置。还可以在 `:collapsed-lines` 之后添加 `=` 来自定义起始折叠行号,例如 `:collapsed-lines=12` 表示代码块从第 12 行开始折叠。

Expand Down
7 changes: 4 additions & 3 deletions docs/zh/plugins/markdown/shiki.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,16 @@ export default defineUserConfig({

### collapsedLines

- 类型:`boolean | number`
- 类型:`boolean | number | 'disabled'`

- 默认值:`false`
- 默认值:`'disabled'`

- 详情:是否启用代码块折叠。

- `number`: 从第 `number` 行开始折叠代码块,例如,`12` 表示从第 12 行开始折叠代码块。
- `true`: 等同于 `15`, 从第 15 行开始折叠代码块。
- `false`: 禁用代码块折叠。
- `false`: 不启用代码块折叠,但仍然可以添加 `:collapsed-lines` 标记为单个代码块折叠。
- `'disabled'`: 完全禁用代码块折叠, `:collapsed-lines` 标记不会生效。

你可以在代码块添加 `:collapsed-lines` / `:no-collapsed-lines` 标记来覆盖配置项中的设置。还可以在 `:collapsed-lines` 之后添加 `=` 来自定义起始折叠行号,例如 `:collapsed-lines=12` 表示代码块从第 12 行开始折叠。

Expand Down
13 changes: 8 additions & 5 deletions plugins/markdown/plugin-prismjs/src/node/prepareConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const prepareConfigFile = (
theme,
themes,
lineNumbers = true,
collapsedLines,
notationDiff,
notationErrorLevel,
notationFocus,
Expand Down Expand Up @@ -80,11 +81,13 @@ export const prepareConfigFile = (
)
}

imports.push(
`import "${getRealPath('@vuepress/highlighter-helper/styles/collapsed-lines.css', url)}"`,
`import { setupCollapsedLines } from "${getRealPath('@vuepress/highlighter-helper/composables/collapsedLines.js', url)}"`,
)
setups.push('setupCollapsedLines()')
if (collapsedLines !== 'disabled') {
imports.push(
`import "${getRealPath('@vuepress/highlighter-helper/styles/collapsed-lines.css', url)}"`,
`import { setupCollapsedLines } from "${getRealPath('@vuepress/highlighter-helper/composables/collapsedLines.js', url)}"`,
)
setups.push('setupCollapsedLines()')
}

let code = imports.join('\n')

Expand Down
58 changes: 32 additions & 26 deletions plugins/markdown/plugin-prismjs/src/node/prismjsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,40 @@ import { highlightPlugin, preWrapperPlugin } from './markdown/index.js'
import type { PrismjsPluginOptions } from './options.js'
import { prepareConfigFile } from './prepareConfigFile.js'
import { resolveHighlighter } from './resolveHighlighter.js'
import type { HighlightOptions, PreWrapperOptions } from './types.js'

export const prismjsPlugin = ({
preloadLanguages = ['markdown', 'jsdoc', 'yaml'],
preWrapper = true,
lineNumbers = true,
collapsedLines = false,
...options
}: PrismjsPluginOptions = {}): Plugin => ({
name: '@vuepress/plugin-prismjs',
export const prismjsPlugin = (options: PrismjsPluginOptions = {}): Plugin => {
const opt: PrismjsPluginOptions = {
preloadLanguages: ['markdown', 'jsdoc', 'yaml'],
preWrapper: true,
lineNumbers: true,
collapsedLines: false,
...options,
}

extendsMarkdown(md) {
if (preloadLanguages.length !== 0) {
loadLanguages(preloadLanguages)
}
return {
name: '@vuepress/plugin-prismjs',

md.options.highlight = (code, lang) => {
const highlighter = resolveHighlighter(lang)
return highlighter?.(code) || ''
}
extendsMarkdown(md) {
const { preloadLanguages, preWrapper, lineNumbers, collapsedLines } = opt

md.use<HighlightOptions>(highlightPlugin, options)
md.use<PreWrapperOptions>(preWrapperPlugin, { preWrapper })
if (preWrapper) {
md.use(lineNumbersPlugin, { lineNumbers, removeLastLine: true })
md.use(collapsedLinesPlugin, { collapsedLines, removeLastLine: true })
}
},
if (preloadLanguages?.length) {
loadLanguages(preloadLanguages)
}

clientConfigFile: (app) => prepareConfigFile(app, options),
})
md.options.highlight = (code, lang) => {
const highlighter = resolveHighlighter(lang)
return highlighter?.(code) || ''
}

md.use(highlightPlugin, opt)
md.use(preWrapperPlugin, { preWrapper })
if (preWrapper) {
md.use(lineNumbersPlugin, { lineNumbers, removeLastLine: true })
if (collapsedLines !== 'disabled')
md.use(collapsedLinesPlugin, { collapsedLines, removeLastLine: true })
}
},

clientConfigFile: (app) => prepareConfigFile(app, opt),
}
}
13 changes: 8 additions & 5 deletions plugins/markdown/plugin-shiki/src/node/prepareConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const prepareConfigFile = (
app: App,
{
lineNumbers = true,
collapsedLines,
notationDiff,
notationErrorLevel,
notationFocus,
Expand Down Expand Up @@ -66,11 +67,13 @@ export const prepareConfigFile = (
)
}

imports.push(
`import "${getRealPath('@vuepress/highlighter-helper/styles/collapsed-lines.css', url)}"`,
`import { setupCollapsedLines } from "${getRealPath('@vuepress/highlighter-helper/composables/collapsedLines.js', url)}"`,
)
setups.push('setupCollapsedLines()')
if (collapsedLines !== 'disabled') {
imports.push(
`import "${getRealPath('@vuepress/highlighter-helper/styles/collapsed-lines.css', url)}"`,
`import { setupCollapsedLines } from "${getRealPath('@vuepress/highlighter-helper/composables/collapsedLines.js', url)}"`,
)
setups.push('setupCollapsedLines()')
}

let code = imports.join('\n')

Expand Down
55 changes: 31 additions & 24 deletions plugins/markdown/plugin-shiki/src/node/shikiPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,38 @@ import {
import type { ShikiPluginOptions } from './options.js'
import { prepareConfigFile } from './prepareConfigFile.js'

export const shikiPlugin = ({
preWrapper = true,
lineNumbers = true,
collapsedLines = false,
...options
}: ShikiPluginOptions = {}): Plugin => ({
name: '@vuepress/plugin-shiki',
export const shikiPlugin = (options: ShikiPluginOptions = {}): Plugin => {
const opt: ShikiPluginOptions = {
preWrapper: true,
lineNumbers: true,
collapsedLines: 'disabled',
...options,
}

extendsMarkdown: async (md, app) => {
// FIXME: Remove in stable version
// eslint-disable-next-line @typescript-eslint/no-deprecated
const { code } = app.options.markdown
return {
name: '@vuepress/plugin-shiki',

await applyHighlighter(md, app, {
...(isPlainObject(code) ? code : {}),
...options,
})
extendsMarkdown: async (md, app) => {
// FIXME: Remove in stable version
// eslint-disable-next-line @typescript-eslint/no-deprecated
const { code } = app.options.markdown

md.use(highlightLinesPlugin)
md.use(preWrapperPlugin, { preWrapper })
if (preWrapper) {
md.use(lineNumbersPlugin, { lineNumbers })
md.use(collapsedLinesPlugin, { collapsedLines })
}
},
await applyHighlighter(md, app, {
...(isPlainObject(code) ? code : {}),
...options,
})

clientConfigFile: (app) => prepareConfigFile(app, options),
})
const { preWrapper, lineNumbers, collapsedLines } = opt

md.use(highlightLinesPlugin)
md.use(preWrapperPlugin, { preWrapper })
if (preWrapper) {
md.use(lineNumbersPlugin, { lineNumbers })
if (collapsedLines !== 'disabled')
md.use(collapsedLinesPlugin, { collapsedLines })
}
},

clientConfigFile: (app) => prepareConfigFile(app, opt),
}
}
5 changes: 3 additions & 2 deletions plugins/markdown/plugin-shiki/tests/shiki-preWrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const createMarkdown = async ({
md.use(preWrapperPlugin, { preWrapper })
if (preWrapper) {
md.use(lineNumbersPlugin, { lineNumbers })
md.use(collapsedLinesPlugin, { collapsedLines })
if (collapsedLines !== 'disabled')
md.use(collapsedLinesPlugin, { collapsedLines })
}
return md
}
Expand Down Expand Up @@ -144,7 +145,7 @@ ${codeFence}{{ inlineCode }}${codeFence}
preWrapper: false,
})
const mdWithoutCollapsedLines = await createMarkdown({
collapsedLines: false,
collapsedLines: 'disabled',
preWrapper: false,
})

Expand Down
7 changes: 4 additions & 3 deletions tools/highlighter-helper/src/node/collapsedLines/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ export interface MarkdownItCollapsedLinesOptions {
*
* - If `number`, collapse starts from line `number`.
* - If `true`, collapse starts from line 15 by default.
* - If `false`, disable collapse.
* @default false
* - If `false`, do not enable `collapsedLines` globally, but you can enable it for individual code blocks using `:collapsed-lines`
* - If `'disabled'`, Completely disable `collapsedLines`
* @default 'disabled'
*/
collapsedLines?: boolean | number
collapsedLines?: boolean | number | 'disabled'

/**
* @default false
Expand Down
4 changes: 3 additions & 1 deletion tools/highlighter-helper/src/node/collapsedLines/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { resolveCollapsedLines } from './resolveCollapsedLine.js'
export const collapsedLines = (
md: Markdown,
{
collapsedLines: collapsedLinesOptions = false,
collapsedLines: collapsedLinesOptions = 'disabled',
removeLastLine,
}: MarkdownItCollapsedLinesOptions = {},
): void => {
if (collapsedLinesOptions === 'disabled') return

const rawFence = md.renderer.rules.fence!

md.renderer.rules.fence = (...args) => {
Expand Down

0 comments on commit 1ff4ed1

Please sign in to comment.