Skip to content

Commit 21bba5c

Browse files
committed
feat(markdown): allow omitting start or end of import code lines range
1 parent 4bfed1f commit 21bba5c

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

packages/@vuepress/markdown/__tests__/plugins/importCodePlugin.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,24 @@ ${mdFixtureContent}\
6262
it('should import partial lines', () => {
6363
const source = `\
6464
@[code{1-2}](${jsFixturePathRelative})
65+
@[code{1-}](${jsFixturePathRelative})
6566
@[code{4-5}](${mdFixturePathRelative})
67+
@[code{-5}](${mdFixturePathRelative})
6668
`
6769

6870
const expected = `\
6971
<pre><code class="language-js">\
7072
${jsFixtureContent.split('\n').slice(0, 2).join('\n')}\
7173
</code></pre>
74+
<pre><code class="language-js">\
75+
${jsFixtureContent.split('\n').slice(0).join('\n')}\
76+
</code></pre>
7277
<pre><code class="language-md">\
7378
${mdFixtureContent.split('\n').slice(3, 5).join('\n')}\
7479
</code></pre>
80+
<pre><code class="language-md">\
81+
${mdFixtureContent.split('\n').slice(0, 5).join('\n')}\
82+
</code></pre>
7583
`
7684

7785
const md = MarkdownIt().use(importCodePlugin)
@@ -81,7 +89,12 @@ ${mdFixtureContent.split('\n').slice(3, 5).join('\n')}\
8189
const rendered = md.render(source, env)
8290

8391
expect(rendered).toEqual(expected)
84-
expect(env.importedFiles).toEqual([jsFixturePath, mdFixturePath])
92+
expect(env.importedFiles).toEqual([
93+
jsFixturePath,
94+
jsFixturePath,
95+
mdFixturePath,
96+
mdFixturePath,
97+
])
8598
})
8699
})
87100

packages/@vuepress/markdown/src/plugins/importCodePlugin/createImportCodeBlockRule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const MIN_LENGTH = 9
1010
const START_CODES = [64, 91, 99, 111, 100, 101]
1111

1212
// regexp to match the import syntax
13-
const SYNTAX_RE = /^@\[code(?:{(?:(\d+)-(\d+))})?(?: ([^\]]+))?\]\(([^)]*)\)/
13+
const SYNTAX_RE = /^@\[code(?:{(?:(\d+)?-(\d+)?)})?(?: ([^\]]+))?\]\(([^)]*)\)/
1414

1515
export const createImportCodeBlockRule = ({
1616
handleImportPath = (str) => str,
@@ -50,8 +50,8 @@ export const createImportCodeBlockRule = ({
5050

5151
const meta: ImportCodeTokenMeta = {
5252
importPath: handleImportPath(importPath),
53-
lineStart: Number.parseInt(lineStart || '0', 10),
54-
lineEnd: Number.parseInt(lineEnd || '0', 10),
53+
lineStart: lineStart ? Number.parseInt(lineStart, 10) : 0,
54+
lineEnd: lineEnd ? Number.parseInt(lineEnd, 10) : undefined,
5555
}
5656

5757
// create a import_code token

packages/@vuepress/markdown/src/plugins/importCodePlugin/resolveImportCode.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,12 @@ export const resolveImportCode = (
3434
// read file content
3535
const fileContent = fs.readFileSync(importFilePath).toString()
3636

37-
if (lineStart < 1 || lineEnd < 1 || lineStart > lineEnd) {
38-
return {
39-
importFilePath,
40-
importCode: fileContent,
41-
}
42-
}
43-
4437
// resolve partial import
4538
return {
4639
importFilePath,
4740
importCode: fileContent
4841
.split('\n')
49-
.slice(lineStart - 1, lineEnd)
42+
.slice(lineStart ? lineStart - 1 : lineStart, lineEnd)
5043
.join('\n'),
5144
}
5245
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface ImportCodeTokenMeta {
22
importPath: string
33
lineStart: number
4-
lineEnd: number
4+
lineEnd?: number
55
}

0 commit comments

Comments
 (0)