Skip to content

Commit cb62f8e

Browse files
committed
fix: 修复嵌套逻辑时的根路径问题
1 parent 5617c7f commit cb62f8e

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ const include_plugin = (md, options) => {
2929
};
3030
}
3131

32-
const _replaceIncludeByContent = (src, rootdir, parentFilePath, filesProcessed) => {
32+
const _replaceIncludeByContent = (src, rootdir, state, parentFilePath, filesProcessed) => {
3333
filesProcessed = filesProcessed ? filesProcessed.slice() : []; // making a copy
3434
let cap, filePath, mdSrc, errorMessage, regionName;
3535

3636
// store parent file path to check circular references
3737
if (parentFilePath) {
3838
filesProcessed.push(parentFilePath);
39+
// fixed 嵌套引用记录
40+
state.env.parentRootFilePaths = filesProcessed;
3941
}
4042
while ((cap = options.includeRe.exec(src))) {
4143
let includePath = cap[1].trim();
@@ -81,7 +83,7 @@ const include_plugin = (md, options) => {
8183
// get content of child file
8284
mdSrc = fs.readFileSync(filePath, 'utf8');
8385
// check if child file also has includes
84-
mdSrc = _replaceIncludeByContent(mdSrc, path.dirname(filePath), filePath, filesProcessed);
86+
mdSrc = _replaceIncludeByContent(mdSrc, path.dirname(filePath), state, filePath, filesProcessed);
8587

8688
if (regionName) {
8789
const lines = mdSrc.split(/\r?\n/)
@@ -117,7 +119,7 @@ const include_plugin = (md, options) => {
117119

118120
const _includeFileParts = (state, startLine, endLine/*, silent*/) => {
119121
const root = options.getRootDir(options, state, startLine, endLine);
120-
state.src = _replaceIncludeByContent(state.src, root);
122+
state.src = _replaceIncludeByContent(state.src, root, state);
121123
};
122124

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

theme/plugins/markdown/snippet/index.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,28 @@ module.exports = function snippet (md, options = {}) {
9191

9292
const token = state.push('fence', 'code', 0)
9393
token.info = extension + meta
94-
token.src = ($relativePath
95-
? path.resolve(root, path.dirname($relativePath), _filename) // 相对路径
96-
: path.resolve(root, _filename)) + region
94+
95+
if ($relativePath) {
96+
const _parentRootFilePaths = [].concat(state.env.parentRootFilePaths);
97+
let _p;
98+
let _currParentRootFilePath = _parentRootFilePaths.shift();
99+
while(_currParentRootFilePath) {
100+
_p = path.resolve(root, path.dirname(_currParentRootFilePath), _filename);
101+
if (fs.existsSync(_p)) {
102+
break;
103+
}
104+
_p = null;
105+
_currParentRootFilePath = _parentRootFilePaths.shift();
106+
}
107+
if (!_p) {
108+
_p = path.resolve(root, path.dirname($relativePath), _filename);
109+
}
110+
token.src = _p + region;
111+
} else {
112+
token.src = path.resolve(root, _filename) + region;
113+
}
114+
115+
97116
// token.src = path.resolve(root, filename) + region
98117
token.markup = '```'
99118
token.map = [startLine, startLine + 1]

0 commit comments

Comments
 (0)