Skip to content

Commit fe20ccc

Browse files
committed
feat(markdown): support import code blocks (close #15)
1 parent cec6ae3 commit fe20ccc

File tree

16 files changed

+672
-6
lines changed

16 files changed

+672
-6
lines changed

Diff for: docs/guide/markdown.md

+65
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,71 @@ This v-pre extension is supported by our built-in plugin.
299299
Config reference: [markdown.code.vPre](../reference/config.md#markdown-vpre)
300300
:::
301301

302+
### Import Code Blocks
303+
304+
You can import code blocks from files with following syntax:
305+
306+
```md
307+
<!-- minimal syntax -->
308+
@[code](../foo.js)
309+
```
310+
311+
If you want to partially import the file:
312+
313+
```md
314+
<!-- partial import, from line 1 to line 10 -->
315+
@[code{1-10}](../foo.js)
316+
```
317+
318+
The code language is inferred from the file extension, while it is recommended to specify it explicitly:
319+
320+
```md
321+
<!-- specify the code language -->
322+
@[code js](../foo.js)
323+
```
324+
325+
In fact, the second part inside the `[]` will be treated as the mark of the code fence, so it supports all the syntax mentioned in the above [Code Blocks](#code-blocks) section:
326+
327+
```md
328+
<!-- line highlighting -->
329+
@[code js{2,4-5}](../foo.js)
330+
```
331+
332+
Here is a complex example:
333+
334+
- import line 3 to line 10 of the `'../foo.js'` file
335+
- specify the language as `'js'`
336+
- highlight line 3 of the imported code, i.e. line 5 of the `'../foo.js'` file
337+
- disable line numbers
338+
339+
```md
340+
@[code{3-10} js{3}:no-line-numbers](../foo.js)
341+
```
342+
343+
Notice that path aliases are not available in import code syntax. You can use following config to handle path alias yourself:
344+
345+
```js
346+
module.exports = {
347+
markdown: {
348+
importCode: {
349+
handleImportPath: (str) =>
350+
str.replace(/^@src/, path.resolve(__dirname, 'path/to/src')),
351+
},
352+
},
353+
}
354+
```
355+
356+
```md
357+
<!-- it will be resolved to 'path/to/src/foo.js' -->
358+
@[code](@src/foo.js)
359+
```
360+
361+
::: tip
362+
This import code extension is supported by our built-in plugin.
363+
364+
Config reference: [markdown.importCode](../reference/config.md#markdown-importcode)
365+
:::
366+
302367
## Using Vue in Markdown
303368

304369
This section will introduce some basic usage of Vue in Markdown.

Diff for: docs/reference/config.md

+26-3
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,29 @@ You should not configure it unless you understand what it is for.
427427
- Also see:
428428
- [Cookbook > Markdown and Vue SFC](../advanced/cookbook/markdown-and-vue-sfc.md)
429429

430+
### markdown.importCode
431+
432+
- Type: `ImportCodePluginOptions | false`
433+
434+
- Details:
435+
436+
Options for VuePress built-in markdown-it import-code plugin.
437+
438+
Set to `false` to disable this plugin.
439+
440+
- Also see:
441+
- [Guide > Markdown > Syntax Extensions > Import Code Blocks](../guide/markdown.md#import-code-blocks)
442+
443+
#### markdown.importCode.handleImportPath
444+
445+
- Type: `(str: string) => string`
446+
447+
- Default: `(str) => str`
448+
449+
- Details:
450+
451+
A function to handle the import path of the import code syntax.
452+
430453
### markdown.links
431454

432455
- Type: `LinksPluginOptions | false`
@@ -442,7 +465,7 @@ You should not configure it unless you understand what it is for.
442465
- Also see:
443466
- [Guide > Markdown > Syntax Extensions > Links](../guide/markdown.md#links)
444467

445-
### markdown.links.internalTag
468+
#### markdown.links.internalTag
446469

447470
- Type: `'a' | 'RouterLink'`
448471

@@ -454,7 +477,7 @@ You should not configure it unless you understand what it is for.
454477

455478
By default, this plugin will transform internal links to `<RouterLink>`. You can set this option to `'a'` to disable this feature.
456479

457-
### markdown.links.externalAttrs
480+
#### markdown.links.externalAttrs
458481

459482
- Type: `Record<string, string>`
460483

@@ -464,7 +487,7 @@ You should not configure it unless you understand what it is for.
464487

465488
Additional attributes for external links.
466489

467-
### markdown.links.externalIcon
490+
#### markdown.links.externalIcon
468491

469492
- Type: `boolean`
470493

Diff for: docs/zh/guide/markdown.md

+65
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,71 @@ v-pre 扩展是由我们的内置插件支持的。
302302
配置参考: [markdown.code.vPre](../reference/config.md#markdown-vpre)
303303
:::
304304

305+
### 导入代码块
306+
307+
你可以使用下面的语法,从文件中导入代码块:
308+
309+
```md
310+
<!-- 最简单的语法 -->
311+
@[code](../foo.js)
312+
```
313+
314+
如果你只想导入这个文件的一部分:
315+
316+
```md
317+
<!-- 仅导入第 1 行至第 10 行 -->
318+
@[code{1-10}](../foo.js)
319+
```
320+
321+
代码语言会根据文件扩展名进行推断,但我们建议你显式指定:
322+
323+
```md
324+
<!-- 指定代码语言 -->
325+
@[code js](../foo.js)
326+
```
327+
328+
实际上,`[]` 内的第二部分会被作为代码块标记来处理,因此在上面 [代码块](#代码块) 章节中提到的语法在这里都可以支持:
329+
330+
```md
331+
<!-- 行高亮 -->
332+
@[code js{2,4-5}](../foo.js)
333+
```
334+
335+
下面是一个复杂的例子:
336+
337+
- 导入 `'../foo.js'` 文件的第 3 行至第 10 行
338+
- 指定语言为 `'js'`
339+
- 对导入代码的第 3 行进行高亮,即 `'../foo.js'` 文件的第 5 行
340+
- 禁用行号
341+
342+
```md
343+
@[code{3-10} js{3}:no-line-numbers](../foo.js)
344+
```
345+
346+
需要注意的是,路径别名在导入代码语法中不会生效。你可以通过下面的配置来自行处理路径别名:
347+
348+
```js
349+
module.exports = {
350+
markdown: {
351+
importCode: {
352+
handleImportPath: (str) =>
353+
str.replace(/^@src/, path.resolve(__dirname, 'path/to/src')),
354+
},
355+
},
356+
}
357+
```
358+
359+
```md
360+
<!-- 会被解析至 'path/to/src/foo.js' -->
361+
@[code](@src/foo.js)
362+
```
363+
364+
::: tip
365+
导入代码扩展是由我们的内置插件支持的。
366+
367+
配置参考: [markdown.importCode](../reference/config.md#markdown-importcode)
368+
:::
369+
305370
## 在 Markdown 中使用 Vue
306371

307372
这一章节会介绍 Vue 在 Markdown 中一些基本用法。

Diff for: docs/zh/reference/config.md

+26-3
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,29 @@ module.exports = {
426426
- 参考:
427427
- [Cookbook > Markdown 与 Vue SFC](../advanced/cookbook/markdown-and-vue-sfc.md)
428428

429+
### markdown.importCode
430+
431+
- 类型: `ImportCodePluginOptions | false`
432+
433+
- 详情:
434+
435+
VuePress 内置的 markdown-it 导入代码插件的配置项。
436+
437+
设置为 `false` 可以禁用该插件。
438+
439+
- 参考:
440+
- [指南 > Markdown > 语法扩展 > 导入代码块](../guide/markdown.md#导入代码块)
441+
442+
#### markdown.importCode.handleImportPath
443+
444+
- 类型: `(str: string) => string`
445+
446+
- 默认值: `(str) => str`
447+
448+
- 详情:
449+
450+
一个函数,用于处理导入代码语法中的文件导入路径。
451+
429452
### markdown.links
430453

431454
- 类型: `LinkPluginOptions | false`
@@ -441,7 +464,7 @@ module.exports = {
441464
- 参考:
442465
- [指南 > Markdown > 语法扩展 > 链接](../guide/markdown.md#链接)
443466

444-
### markdown.links.internalTag
467+
#### markdown.links.internalTag
445468

446469
- 类型: `string`
447470

@@ -453,7 +476,7 @@ module.exports = {
453476

454477
默认情况下,该插件会把内部链接转换为 `<RouterLink>` 。你可以把该选项设置为 `'a'` 来禁用这个功能。
455478

456-
### markdown.links.externalAttrs
479+
#### markdown.links.externalAttrs
457480

458481
- 类型: `Record<string, string>`
459482

@@ -463,7 +486,7 @@ module.exports = {
463486

464487
为外部链接添加额外的属性。
465488

466-
### markdown.links.externalIcon
489+
#### markdown.links.externalIcon
467490

468491
- 类型: `boolean`
469492

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const msg = 'hello from js'
2+
3+
console.log(msg)
4+
5+
console.log('foo bar')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# msg
2+
3+
hello from md
4+
5+
## foo bar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`@vuepress/markdown > plugins > importCodePlugin compatibility with codePlugin should terminate paragraph 1`] = `
4+
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code>const msg = 'hello from js'
5+
6+
console.log(msg)
7+
8+
console.log('foo bar')
9+
</code></pre>
10+
<div class="highlight-lines">
11+
<div class="highlight-line">&nbsp;</div><br>
12+
<div class="highlight-line">&nbsp;</div>
13+
<div class="highlight-line">&nbsp;</div><br>
14+
</div>
15+
<div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div>
16+
</div>
17+
<div class="language-markdown ext-md"><pre v-pre class="language-markdown"><code># msg
18+
19+
hello from md
20+
21+
## foo bar
22+
</code></pre>
23+
</div>
24+
`;

0 commit comments

Comments
 (0)