Skip to content

Commit c0ce7f7

Browse files
author
fan1312
authored
feat: support for customizing the starting line number in a code block (#2917)
1 parent f0bcc22 commit c0ce7f7

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

Diff for: docs/guide/markdown.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ Please see [`markdown` options](../reference/site-config#markdown) for more deta
510510

511511
You can add `:line-numbers` / `:no-line-numbers` mark in your fenced code blocks to override the value set in config.
512512

513+
You can also customize the starting line number by adding `=` after `:line-numbers`. For example, `:line-numbers=2` means the line numbers in code blocks will start from `2`.
514+
513515
**Input**
514516

515517
````md
@@ -524,6 +526,12 @@ const line3 = 'This is line 3'
524526
const line2 = 'This is line 2'
525527
const line3 = 'This is line 3'
526528
```
529+
530+
```ts:line-numbers=2 {1}
531+
// line-numbers is enabled and start from 2
532+
const line3 = 'This is line 3'
533+
const line4 = 'This is line 4'
534+
```
527535
````
528536

529537
**Output**
@@ -540,6 +548,12 @@ const line2 = 'This is line 2'
540548
const line3 = 'This is line 3'
541549
```
542550

551+
```ts:line-numbers=2 {1}
552+
// line-numbers is enabled and start from 2
553+
const line3 = 'This is line 3'
554+
const line4 = 'This is line 4'
555+
````
556+
543557
## Import Code Snippets
544558
545559
You can import code snippets from existing files via following syntax:
@@ -566,7 +580,7 @@ It also supports [line highlighting](#line-highlighting-in-code-blocks):
566580

567581
**Output**
568582

569-
<<< @/snippets/snippet.js{2}
583+
<<< @/snippets/snippet.js
570584

571585
::: tip
572586
The value of `@` corresponds to the source root. By default it's the VitePress project root, unless `srcDir` is configured. Alternatively, you can also import from relative paths:

Diff for: src/node/markdown/plugins/highlight.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export async function highlight(
8787
const styleRE = /<pre[^>]*(style=".*?")/
8888
const preRE = /^<pre(.*?)>/
8989
const vueRE = /-vue$/
90-
const lineNoRE = /:(no-)?line-numbers$/
90+
const lineNoRE = /:(no-)?line-numbers(=\d*)?$/
9191
const mustacheRE = /\{\{.*?\}\}/g
9292

9393
return (str: string, lang: string, attrs: string) => {

Diff for: src/node/markdown/plugins/lineNumbers.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ export const lineNumberPlugin = (md: MarkdownIt, enable = false) => {
1212
const info = tokens[idx].info
1313

1414
if (
15-
(!enable && !/:line-numbers($| )/.test(info)) ||
15+
(!enable && !/:line-numbers($| |=)/.test(info)) ||
1616
(enable && /:no-line-numbers($| )/.test(info))
1717
) {
1818
return rawCode
1919
}
2020

21+
let startLineNumber = 1
22+
const matchStartLineNumber = info.match(/:line-numbers=(\d*)/)
23+
if (matchStartLineNumber && matchStartLineNumber[1]) {
24+
startLineNumber = parseInt(matchStartLineNumber[1])
25+
}
26+
2127
const code = rawCode.slice(
2228
rawCode.indexOf('<code>'),
2329
rawCode.indexOf('</code>')
@@ -26,7 +32,10 @@ export const lineNumberPlugin = (md: MarkdownIt, enable = false) => {
2632
const lines = code.split('\n')
2733

2834
const lineNumbersCode = [...Array(lines.length)]
29-
.map((_, index) => `<span class="line-number">${index + 1}</span><br>`)
35+
.map(
36+
(_, index) =>
37+
`<span class="line-number">${index + startLineNumber}</span><br>`
38+
)
3039
.join('')
3140

3241
const lineNumbersWrapperCode = `<div class="line-numbers-wrapper" aria-hidden="true">${lineNumbersCode}</div>`

Diff for: src/node/markdown/plugins/preWrapper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function extractTitle(info: string, html = false) {
4040
function extractLang(info: string) {
4141
return info
4242
.trim()
43-
.replace(/:(no-)?line-numbers({| |$).*/, '')
43+
.replace(/:(no-)?line-numbers({| |$|=\d*).*/, '')
4444
.replace(/(-vue|{| ).*$/, '')
4545
.replace(/^vue-html$/, 'template')
4646
.replace(/^ansi$/, '')

0 commit comments

Comments
 (0)