Skip to content

Commit

Permalink
feat: add disable option for highlighter line-number option and unify…
Browse files Browse the repository at this point in the history
… words
  • Loading branch information
Mister-Hope committed Sep 18, 2024
1 parent 7c2e8e9 commit 158f7ca
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 65 deletions.
13 changes: 7 additions & 6 deletions docs/plugins/markdown/prismjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,17 @@ export default {

### lineNumbers

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

- Default: `true`

- Details:

- `true`: enable line numbers.
- `false`: disabled line numbers.
- `number`: the minimum number of lines to enable line numbers.
For example, if you set it to 4, line numbers will only be enabled when your code block has at least 4 lines of code.
- `true`: enable line numbers globally.
- `false`: disable line numbers globally.
- `'disable'`: Completely disable line numbers, `:line-numbers` will not take effect.

You can add `:line-numbers` / `:no-line-numbers` mark in your fenced code blocks to override the value set in config, and customize the beginning number by adding `=` after `:line-numbers`. For example, `:line-numbers=2` means the line numbers in code blocks will start from `2`.

Expand Down Expand Up @@ -196,16 +197,16 @@ export default defineUserConfig({

### collapsedLines

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

- Default: `'disabled'`
- Default: `'disable'`

- Details: Default behavior of code block collapsing.

- `number`: collapse the code block starting from line `number` by default, for example, `12` means collapsing the code block starting from line 12.
- `true`: Equivalent to `15`, collapsing the code block starting from line 15 by default.
- `false`: Add support for code block collapsing, but disable it globally
- `'disabled'`: Completely disable code block collapsing, `:collapsed-lines` will not take effect.
- `'disable'`: Completely disable code block collapsing, `:collapsed-lines` will not take effect.

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

Expand Down
13 changes: 7 additions & 6 deletions docs/plugins/markdown/shiki.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,17 @@ export default {

### lineNumbers

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

- Default: `true`

- Details:

- `true`: enable line numbers.
- `false`: disabled line numbers.
- `number`: the minimum number of lines to enable line numbers.
For example, if you set it to 4, line numbers will only be enabled when your code block has at least 4 lines of code.
- `true`: enable line numbers globally.
- `false`: disable line numbers globally.
- `'disable'`: Completely disable line numbers, `:line-numbers` will not take effect.

You can add `:line-numbers` / `:no-line-numbers` mark in your fenced code blocks to override the value set in config, and customize the beginning number by adding `=` after `:line-numbers`. For example, `:line-numbers=2` means the line numbers in code blocks will start from `2`.

Expand Down Expand Up @@ -192,16 +193,16 @@ export default defineUserConfig({

### collapsedLines

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

- Default: `'disabled'`
- Default: `'disable'`

- Details: Default behavior of code block collapsing.

- `number`: collapse the code block starting from line `number` by default, for example, `12` means collapsing the code block starting from line 12.
- `true`: Equivalent to `15`, collapsing the code block starting from line 15 by default.
- `false`: Add support for code block collapsing, but disable it globally
- `'disabled'`: Completely disable code block collapsing, `:collapsed-lines` will not take effect.
- `'disable'`: Completely disable code block collapsing, `:collapsed-lines` will not take effect.

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

Expand Down
4 changes: 2 additions & 2 deletions docs/plugins/pwa/pwa/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@

### update

- Type: `"disabled" | "available" | "hint" | "force"`
- Type: `"disable" | "available" | "hint" | "force"`
- Default: `"available"`
- Details:
Control logic when new content is found.

- `"disabled"`: Do nothing even when new service worker is available. After new service work succeeds installing and starts waiting, it will control page and provide new content in next visit.
- `"disable"`: Do nothing even when new service worker is available. After new service work succeeds installing and starts waiting, it will control page and provide new content in next visit.

- `"available"`: Only display update popup when the new service worker is available

Expand Down
13 changes: 7 additions & 6 deletions docs/zh/plugins/markdown/prismjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,17 @@ export default {

### lineNumbers

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

- 默认值:`true`

- 详情:

- `true`:启用代码行号
- `false`:禁用代码行号。
- `number`:显示行号所需的最少行数。
例如,如果你将它设置为 4 ,那么只有在你的代码块包含至少 4 行代码时才会启用行号。
- `true`:全局启用代码行号
- `false`:全局禁用代码行号。
- `'disable'`: 完全禁用行号,`:line-numbers` 标记不会生效。

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

Expand Down Expand Up @@ -196,16 +197,16 @@ export default defineUserConfig({

### collapsedLines

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

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

- 详情:代码块折叠的默认行为。

- `number`: 从第 `number` 行开始折叠代码块,例如,`12` 表示从第 12 行开始折叠代码块。
- `true`: 等同于 `15`, 从第 15 行开始折叠代码块。
- `false`: 添加代码块折叠支持,但全局禁用此功能。
- `'disabled'`: 完全禁用代码块折叠, `:collapsed-lines` 标记不会生效。
- `'disable'`: 完全禁用代码块折叠, `:collapsed-lines` 标记不会生效。

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

Expand Down
13 changes: 7 additions & 6 deletions docs/zh/plugins/markdown/shiki.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,17 @@ export default {

### lineNumbers

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

- 默认值:`true`

- 详情:

- `true`:启用代码行号
- `false`:禁用代码行号。
- `number`:显示行号所需的最少行数。
例如,如果你将它设置为 4 ,那么只有在你的代码块包含至少 4 行代码时才会启用行号。
- `true`:全局启用代码行号
- `false`:全局禁用代码行号。
- `'disable'`: 完全禁用行号,`:line-numbers` 标记不会生效。

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

Expand Down Expand Up @@ -194,16 +195,16 @@ export default defineUserConfig({

### collapsedLines

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

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

- 详情:代码块折叠的默认行为。

- `number`: 从第 `number` 行开始折叠代码块,例如,`12` 表示从第 12 行开始折叠代码块。
- `true`: 等同于 `15`, 从第 15 行开始折叠代码块。
- `false`: 添加代码块折叠支持,但全局禁用此功能
- `'disabled'`: 完全禁用代码块折叠, `:collapsed-lines` 标记不会生效。
- `'disable'`: 完全禁用代码块折叠, `:collapsed-lines` 标记不会生效。

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

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/plugins/pwa/pwa/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ VuePress 本质上是一个 SPA。这意味着你只需要缓存主页并从主

如果你的文档仍在建设期,希望尽早提示用户他可能在阅读已过时的内容,你可以将其设置为 `"hint"`。这样用户在进入文档后数秒内就可以收到新内容已发布的通知。但这样做的负面效果是如果用户在新 SW 就绪前选择更新,那么他将在新 SW 安装并接管页面前,需要从互联网获取页面的全部资源。

如果你的文档很稳定,或者你在托管博客,不太关心用户立即接收到最新版本,你可以将其设置为 `"disabled"`,这意味着新的 SW 将在后台完全静默安装并在安装后等待,当旧版本 SW 控制的页面全部关闭后,新 SW 将再下次访问接管并提供用户新内容。此设置可以避免用户在访中被弹窗打扰。
如果你的文档很稳定,或者你在托管博客,不太关心用户立即接收到最新版本,你可以将其设置为 `"disable"`,这意味着新的 SW 将在后台完全静默安装并在安装后等待,当旧版本 SW 控制的页面全部关闭后,新 SW 将再下次访问接管并提供用户新内容。此设置可以避免用户在访中被弹窗打扰。

如果你希望通过 SW 来加速用户在弱网或无网条件下的访问,但同时希望用户时刻访问新内容,你可以将此选项设置为 `"force"`。这意味着检测到新 SW 后旧 SW 将会被立刻销毁并且页面会被刷新以确保用户浏览最新内容。最大的缺点就是致新 SW 发布后,用户在重新进入网站后的几秒内会遇到预期之外的突然刷新,并且他们将必须通过互联网访问文档并完全重新安装最新的 SW。

Expand Down
4 changes: 2 additions & 2 deletions e2e/tests/plugin-watermark/watermark.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from '@playwright/test'

test.describe('plugin-watermark', () => {
test('enabled watermark', async ({ page }) => {
test('watermark enabled', async ({ page }) => {
await page.goto('watermark/')

expect(
Expand All @@ -13,7 +13,7 @@ test.describe('plugin-watermark', () => {
).toContain('background-image: url("data:image/png;base64,')
})

test('disabled watermark', async ({ page }) => {
test('watermark disabled', async ({ page }) => {
await page.goto('watermark/disabled.html')

await expect(page.locator('//html/body/div[2]')).not.toBeVisible()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ declare global {
* @see https://umami.is/docs/tracker-functions
*/
export const useUmamiAnalytics = ({
// eslint-disable-next-line @typescript-eslint/no-deprecated
link = 'https://us.umami.is/script.js',
id,
domains,
Expand Down
13 changes: 7 additions & 6 deletions plugins/markdown/plugin-prismjs/src/node/options.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { MarkdownItCollapsedLinesOptions } from '@vuepress/highlighter-helper'
import type {
MarkdownItCollapsedLinesOptions,
MarkdownItLineNumbersOptions,
} from '@vuepress/highlighter-helper'
import type { HighlightOptions, PreWrapperOptions } from './types.js'
HighlightOptions,
LineNumbersOptions,
PreWrapperOptions,
} from './types.js'

export type PrismjsLightTheme =
| 'ateliersulphurpool-light'
Expand Down Expand Up @@ -49,9 +50,9 @@ export type PrismjsTheme = PrismjsDarkTheme | PrismjsLightTheme
* Options of @vuepress/plugin-prismjs
*/
export interface PrismjsPluginOptions
extends Pick<MarkdownItLineNumbersOptions, 'lineNumbers'>,
Pick<MarkdownItCollapsedLinesOptions, 'collapsedLines'>,
extends Pick<MarkdownItCollapsedLinesOptions, 'collapsedLines'>,
PreWrapperOptions,
LineNumbersOptions,
HighlightOptions {
/**
* Single theme
Expand Down
4 changes: 2 additions & 2 deletions plugins/markdown/plugin-prismjs/src/node/prepareConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const prepareConfigFile = (
)
}

if (lineNumbers) {
if (lineNumbers !== 'disable') {
imports.push(
`import "${getRealPath('@vuepress/highlighter-helper/styles/line-numbers.css', url)}"`,
)
Expand Down Expand Up @@ -81,7 +81,7 @@ export const prepareConfigFile = (
)
}

if (collapsedLines !== 'disabled') {
if (collapsedLines !== 'disable') {
imports.push(
`import "${getRealPath('@vuepress/highlighter-helper/styles/collapsed-lines.css', url)}"`,
`import { setupCollapsedLines } from "${getRealPath('@vuepress/highlighter-helper/composables/collapsedLines.js', url)}"`,
Expand Down
5 changes: 3 additions & 2 deletions plugins/markdown/plugin-prismjs/src/node/prismjsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ export const prismjsPlugin = (options: PrismjsPluginOptions = {}): Plugin => {
md.use(highlightPlugin, opt)
md.use(preWrapperPlugin, { preWrapper })
if (preWrapper) {
md.use(lineNumbersPlugin, { lineNumbers, removeLastLine: true })
if (collapsedLines !== 'disabled')
if (lineNumbers !== 'disable')
md.use(lineNumbersPlugin, { lineNumbers, removeLastLine: true })
if (collapsedLines !== 'disable')
md.use(collapsedLinesPlugin, { collapsedLines, removeLastLine: true })
}
},
Expand Down
5 changes: 3 additions & 2 deletions plugins/markdown/plugin-prismjs/src/node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ export interface LineNumbersOptions {
/**
* Enable line numbers or not
*
* - A `boolean` value is to enable line numbers or not.
* - A `boolean` value is to enable line numbers or not globally.
* - A `number` value is the minimum number of lines to enable line numbers
* - `disable` means completely disable the feature
*
* @default true
*/
lineNumbers?: boolean | number
lineNumbers?: boolean | number | 'disable'
}

export interface HighlightOptions {
Expand Down
4 changes: 2 additions & 2 deletions plugins/markdown/plugin-prismjs/tests/getCodeParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ console.log('hello world)\t
\t\tconsole.log('hello world')
console.log('hello world)\t
}`)
it('disabled global', () => {
it('disable global', () => {
const parser = getCodeParser(code)
metaWhitespace(parser, 'js', false)
const result = parser.stringify()
Expand All @@ -548,7 +548,7 @@ console.log('hello world)\t
expect(result).toMatchSnapshot()

Check failure on line 548 in plugins/markdown/plugin-prismjs/tests/getCodeParser.spec.ts

View workflow job for this annotation

GitHub Actions / coverage

plugins/markdown/plugin-prismjs/tests/getCodeParser.spec.ts > @vuepress/plugin-prismjs > parser > should work `metaWhitespace` with `false` > disable global

Error: Snapshot `@vuepress/plugin-prismjs > parser > should work `metaWhitespace` with `false` > disable global 1` mismatched ❯ plugins/markdown/plugin-prismjs/tests/getCodeParser.spec.ts:548:22

Check failure on line 548 in plugins/markdown/plugin-prismjs/tests/getCodeParser.spec.ts

View workflow job for this annotation

GitHub Actions / check

plugins/markdown/plugin-prismjs/tests/getCodeParser.spec.ts > @vuepress/plugin-prismjs > parser > should work `metaWhitespace` with `false` > disable global

Error: Snapshot `@vuepress/plugin-prismjs > parser > should work `metaWhitespace` with `false` > disable global 1` mismatched ❯ plugins/markdown/plugin-prismjs/tests/getCodeParser.spec.ts:548:22
})

it('disabled global and :whitespace', () => {
it('disable global and :whitespace', () => {
const parser = getCodeParser(code)
metaWhitespace(parser, 'js :whitespace', false)
const result = parser.stringify()
Expand Down
19 changes: 13 additions & 6 deletions plugins/markdown/plugin-shiki/src/node/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type {
MarkdownItCollapsedLinesOptions,
MarkdownItLineNumbersOptions,
} from '@vuepress/highlighter-helper'
import type { MarkdownItCollapsedLinesOptions } from '@vuepress/highlighter-helper'
import type { PreWrapperOptions, ShikiHighlightOptions } from './types.js'

/**
Expand All @@ -11,6 +8,16 @@ export type ShikiPluginOptions = Pick<
MarkdownItCollapsedLinesOptions,
'collapsedLines'
> &
Pick<MarkdownItLineNumbersOptions, 'lineNumbers'> &
PreWrapperOptions &
ShikiHighlightOptions
ShikiHighlightOptions & {
/**
* Line number options
*
* - A `boolean` value is to enable line numbers or not globally.
* - A `number` value is the minimum number of lines to enable line numbers
* - `disable` means completely disable the feature
*
* @default true
*/
lineNumbers?: boolean | number | 'disable'
}
4 changes: 2 additions & 2 deletions plugins/markdown/plugin-shiki/src/node/prepareConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const prepareConfigFile = (

const setups: string[] = []

if (lineNumbers) {
if (lineNumbers !== 'disable') {
imports.push(
`import "${getRealPath('@vuepress/highlighter-helper/styles/line-numbers.css', url)}"`,
)
Expand Down Expand Up @@ -67,7 +67,7 @@ export const prepareConfigFile = (
)
}

if (collapsedLines !== 'disabled') {
if (collapsedLines !== 'disable') {
imports.push(
`import "${getRealPath('@vuepress/highlighter-helper/styles/collapsed-lines.css', url)}"`,
`import { setupCollapsedLines } from "${getRealPath('@vuepress/highlighter-helper/composables/collapsedLines.js', url)}"`,
Expand Down
7 changes: 4 additions & 3 deletions plugins/markdown/plugin-shiki/src/node/shikiPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const shikiPlugin = (options: ShikiPluginOptions = {}): Plugin => {
const opt: ShikiPluginOptions = {
preWrapper: true,
lineNumbers: true,
collapsedLines: 'disabled',
collapsedLines: 'disable',
...options,
}

Expand All @@ -38,8 +38,9 @@ export const shikiPlugin = (options: ShikiPluginOptions = {}): Plugin => {
md.use(highlightLinesPlugin)
md.use(preWrapperPlugin, { preWrapper })
if (preWrapper) {
md.use(lineNumbersPlugin, { lineNumbers })
if (collapsedLines !== 'disabled')
if (lineNumbers !== 'disable')
md.use(lineNumbersPlugin, { lineNumbers })
if (collapsedLines !== 'disable')
md.use(collapsedLinesPlugin, { collapsedLines })
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const whitespaceTransformer = (
defaultPosition: WhitespacePosition | boolean = false,
): ShikiTransformer[] => {
const position = resolveWhitespacePosition(meta, defaultPosition)
// disabled current code block
// disable current code block
if (position === false) return []

return [transformerRenderWhitespace({ position })]
Expand Down
4 changes: 2 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,7 @@ const createMarkdown = async ({
md.use(preWrapperPlugin, { preWrapper })
if (preWrapper) {
md.use(lineNumbersPlugin, { lineNumbers })
if (collapsedLines !== 'disabled')
if (collapsedLines !== 'disable')
md.use(collapsedLinesPlugin, { collapsedLines })
}
return md
Expand Down Expand Up @@ -145,7 +145,7 @@ ${codeFence}{{ inlineCode }}${codeFence}
preWrapper: false,
})
const mdWithoutCollapsedLines = await createMarkdown({
collapsedLines: 'disabled',
collapsedLines: 'disable',
preWrapper: false,
})

Expand Down
Loading

0 comments on commit 158f7ca

Please sign in to comment.