Skip to content

Commit

Permalink
fix: 修复嵌套逻辑时的根路径问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zyao89 committed Oct 29, 2021
1 parent 5617c7f commit cb62f8e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
8 changes: 5 additions & 3 deletions theme/plugins/markdown/markdown-it-include.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ const include_plugin = (md, options) => {
};
}

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

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

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

const _includeFileParts = (state, startLine, endLine/*, silent*/) => {
const root = options.getRootDir(options, state, startLine, endLine);
state.src = _replaceIncludeByContent(state.src, root);
state.src = _replaceIncludeByContent(state.src, root, state);
};

md.core.ruler.before('normalize', 'include', _includeFileParts);
Expand Down
25 changes: 22 additions & 3 deletions theme/plugins/markdown/snippet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,28 @@ module.exports = function snippet (md, options = {}) {

const token = state.push('fence', 'code', 0)
token.info = extension + meta
token.src = ($relativePath
? path.resolve(root, path.dirname($relativePath), _filename) // 相对路径
: path.resolve(root, _filename)) + region

if ($relativePath) {
const _parentRootFilePaths = [].concat(state.env.parentRootFilePaths);
let _p;
let _currParentRootFilePath = _parentRootFilePaths.shift();
while(_currParentRootFilePath) {
_p = path.resolve(root, path.dirname(_currParentRootFilePath), _filename);
if (fs.existsSync(_p)) {
break;
}
_p = null;
_currParentRootFilePath = _parentRootFilePaths.shift();
}
if (!_p) {
_p = path.resolve(root, path.dirname($relativePath), _filename);
}
token.src = _p + region;
} else {
token.src = path.resolve(root, _filename) + region;
}


// token.src = path.resolve(root, filename) + region
token.markup = '```'
token.map = [startLine, startLine + 1]
Expand Down

0 comments on commit cb62f8e

Please sign in to comment.