Skip to content

Commit 1208d9c

Browse files
committed
:enhance: 增强文本块导入能力
1 parent f1b2830 commit 1208d9c

5 files changed

Lines changed: 39 additions & 8 deletions

File tree

docs/markdown/advance/5-import.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 导入代码段
22

33
::: tip 注意
4-
由于代码段的导入将在 webpack 编译之前执行,因此你无法使用 webpack 中的路径别名,默认基于文档更目录开始
4+
由于代码段的导入将在 webpack 编译之前执行,因此你无法使用 webpack 中的路径别名,因此加载逻辑分为三层,`node_modules` > `@`(文档根目录) > 相对目录
55
:::
66

77

docs/markdown/advance/6-include.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 导入文本
22

33
::: tip 注意
4-
由于代码段的导入将在 webpack 编译之前执行,因此你无法使用 webpack 中的路径别名,默认基于文档更目录开始
4+
由于代码段的导入将在 webpack 编译之前执行,因此你无法使用 webpack 中的路径别名,`node_modules` > `@`(文档根目录) > 相对目录
55
:::
66

77
::: warning 注意

theme/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ module.exports = (options, ctx) => {
9393
},
9494
extendMarkdown: md => {
9595
md.set({ breaks: true });
96+
97+
const render = md.render
98+
md.render = (...args) => {
99+
if (args.length > 1) {
100+
const [, { relativePath }] = args;
101+
md.$relativePath = relativePath;
102+
}
103+
return render.call(md, ...args);
104+
};
96105
},
97106
chainMarkdown(config) {
98107
config
@@ -119,12 +128,17 @@ module.exports = (options, ctx) => {
119128
// include
120129
config.plugin('include').use(require('./plugins/markdown/markdown-it-include'), [ {
121130
root: sourceDir, // root path
122-
includeRe: /<<<include(.+)/i,
131+
includeRe: /<{3}include(.+)/i,
123132
bracesAreOptional: true,
124133
getRootDir(pluginOptions, state, startLine, endLine) {
125134
// const pos = state.bMarks[startLine] + state.tShift[startLine]
126135
// const max = state.eMarks[startLine]
127-
return pluginOptions.root;
136+
const root = pluginOptions.root;
137+
const { $relativePath } = state.md || {};
138+
if (!$relativePath) {
139+
return root;
140+
}
141+
return path.resolve(root, path.dirname($relativePath));
128142
},
129143
} ])
130144

theme/plugins/markdown/markdown-it-include.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ const include_plugin = (md, options) => {
5252
}
5353

5454
if (!errorMessage) {
55-
const [ _includePath, _regionName ] = includePath ? includePath.split('#') : [''];
55+
let [ _includePath, _regionName ] = includePath ? includePath.split('#') : [''];
56+
try {
57+
_includePath = require.resolve(_includePath);
58+
} catch (e) {
59+
_includePath = _includePath.trim().replace(/^@/, rootdir).trim()
60+
}
5661
filePath = path.resolve(rootdir, _includePath);
5762
regionName = _regionName;
5863

@@ -111,7 +116,8 @@ const include_plugin = (md, options) => {
111116
};
112117

113118
const _includeFileParts = (state, startLine, endLine/*, silent*/) => {
114-
state.src = _replaceIncludeByContent(state.src, options.getRootDir(options, state, startLine, endLine));
119+
const root = options.getRootDir(options, state, startLine, endLine);
120+
state.src = _replaceIncludeByContent(state.src, root);
115121
};
116122

117123
md.core.ruler.before('normalize', 'include', _includeFileParts);

theme/plugins/markdown/snippet/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = function snippet (md, options = {}) {
4242
}
4343

4444
function parser (state, startLine, endLine, silent) {
45+
const { $relativePath } = state.md || {};
4546
const CH = '<'.charCodeAt(0)
4647
const pos = state.bMarks[startLine] + state.tShift[startLine]
4748
const max = state.eMarks[startLine]
@@ -76,14 +77,24 @@ module.exports = function snippet (md, options = {}) {
7677
*/
7778
const rawPathRegexp = /^(.+?(?:\.([a-z]+))?)(?:(#[\w-]+))?(?: ?({\d+(?:[,-]\d+)*}))?$/
7879

79-
const rawPath = state.src.slice(start, end).trim()
80+
// 通过 @ 识别根目录
81+
const rawPath = state.src.slice(start, end).trim();
8082
const [filename = '', extension = '', region = '', meta = ''] = (rawPathRegexp.exec(rawPath) || []).slice(1)
83+
let _filename = filename;
84+
try {
85+
_filename = require.resolve(_filename);
86+
} catch (error) {
87+
_filename = _filename.replace(/^@/, root).trim();
88+
}
8189

8290
state.line = startLine + 1
8391

8492
const token = state.push('fence', 'code', 0)
8593
token.info = extension + meta
86-
token.src = path.resolve(root, filename) + region
94+
token.src = ($relativePath
95+
? path.resolve(root, path.dirname($relativePath), _filename) // 相对路径
96+
: path.resolve(root, _filename)) + region
97+
// token.src = path.resolve(root, filename) + region
8798
token.markup = '```'
8899
token.map = [startLine, startLine + 1]
89100

0 commit comments

Comments
 (0)