Skip to content

Commit c048076

Browse files
authored
feat(build): fence-level config for line-numbers (#1733)
1 parent 195d867 commit c048076

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

docs/guide/markdown.md

+36
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,38 @@ export default {
495495

496496
Please see [`markdown` options](../config/app-configs#markdown) for more details.
497497

498+
You can add `:line-numbers` / `:no-line-numbers` mark in your fenced code blocks to override the value set in config.
499+
500+
**Input**
501+
502+
````md
503+
```ts {1}
504+
// line-numbers is disabled by default
505+
const line2 = 'This is line 2'
506+
const line3 = 'This is line 3'
507+
```
508+
509+
```ts:line-numbers {1}
510+
// line-numbers is enabled
511+
const line2 = 'This is line 2'
512+
const line3 = 'This is line 3'
513+
```
514+
````
515+
516+
**Output**
517+
518+
```ts {1}
519+
// line-numbers is disabled by default
520+
const line2 = 'This is line 2'
521+
const line3 = 'This is line 3'
522+
```
523+
524+
```ts:line-numbers {1}
525+
// line-numbers is enabled
526+
const line2 = 'This is line 2'
527+
const line3 = 'This is line 3'
528+
```
529+
498530
## Import Code Snippets
499531

500532
You can import code snippets from existing files via following syntax:
@@ -551,6 +583,10 @@ You can also specify the language inside the braces (`{}`) like this:
551583
<!-- with line highlighting: -->
552584

553585
<<< @/snippets/snippet.cs{1,2,4-6 c#}
586+
587+
<!-- with line numbers: -->
588+
589+
<<< @/snippets/snippet.cs{1,2,4-6 c#:line-numbers}
554590
```
555591

556592
This is helpful if source language cannot be inferred from your file extension.

src/node/markdown/markdown.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,10 @@ export const createMarkdownRenderer = async (
7676
.use(imagePlugin)
7777
.use(
7878
linkPlugin,
79-
{
80-
target: '_blank',
81-
rel: 'noreferrer',
82-
...options.externalLinks
83-
},
79+
{ target: '_blank', rel: 'noreferrer', ...options.externalLinks },
8480
base
8581
)
82+
.use(lineNumberPlugin, options.lineNumbers)
8683

8784
// 3rd party plugins
8885
if (!options.attrs?.disable) {
@@ -115,8 +112,5 @@ export const createMarkdownRenderer = async (
115112
options.config(md)
116113
}
117114

118-
if (options.lineNumbers) {
119-
md.use(lineNumberPlugin)
120-
}
121115
return md
122116
}

src/node/markdown/plugins/highlight.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ export async function highlight(
7474
const styleRE = /<pre[^>]*(style=".*?")/
7575
const preRE = /^<pre(.*?)>/
7676
const vueRE = /-vue$/
77+
const lineNoRE = /:(no-)?line-numbers$/
7778

7879
return (str: string, lang: string, attrs: string) => {
7980
const vPre = vueRE.test(lang) ? '' : 'v-pre'
80-
lang = lang.replace(vueRE, '').toLowerCase() || defaultLang
81+
lang =
82+
lang.replace(lineNoRE, '').replace(vueRE, '').toLowerCase() || defaultLang
8183

8284
const lineOptions = attrsToLines(attrs)
8385
const cleanup = (str: string) =>

src/node/markdown/plugins/lineNumbers.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33

44
import MarkdownIt from 'markdown-it'
55

6-
export const lineNumberPlugin = (md: MarkdownIt) => {
6+
export const lineNumberPlugin = (md: MarkdownIt, enable = false) => {
77
const fence = md.renderer.rules.fence!
88
md.renderer.rules.fence = (...args) => {
99
const rawCode = fence(...args)
10+
11+
const [tokens, idx] = args
12+
const info = tokens[idx].info
13+
14+
if (
15+
(!enable && !/:line-numbers($| )/.test(info)) ||
16+
(enable && /:no-line-numbers($| )/.test(info))
17+
) {
18+
return rawCode
19+
}
20+
1021
const code = rawCode.slice(
1122
rawCode.indexOf('<code>'),
1223
rawCode.indexOf('</code>')

src/node/markdown/plugins/preWrapper.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function extractTitle(info: string) {
1919
const extractLang = (info: string) => {
2020
return info
2121
.trim()
22+
.replace(/:(no-)?line-numbers$/, '')
2223
.replace(/(-vue|{| ).*$/, '')
2324
.replace(/^vue-html$/, 'template')
2425
}

0 commit comments

Comments
 (0)