diff --git a/plugins/plugin-md-power/__test__/__snapshots__/fileTreePlugin.spec.ts.snap b/plugins/plugin-md-power/__test__/__snapshots__/fileTreePlugin.spec.ts.snap index aea16acd..42d0caaf 100644 --- a/plugins/plugin-md-power/__test__/__snapshots__/fileTreePlugin.spec.ts.snap +++ b/plugins/plugin-md-power/__test__/__snapshots__/fileTreePlugin.spec.ts.snap @@ -161,3 +161,31 @@ exports[`fileTreePlugin > should work with default options 1`] = `
" `; + +exports[`fileTreePlugin > should work with nesting content 1`] = ` +" +" +`; diff --git a/plugins/plugin-md-power/__test__/fileTreePlugin.spec.ts b/plugins/plugin-md-power/__test__/fileTreePlugin.spec.ts index 41ab21b8..74b6701a 100644 --- a/plugins/plugin-md-power/__test__/fileTreePlugin.spec.ts +++ b/plugins/plugin-md-power/__test__/fileTreePlugin.spec.ts @@ -60,7 +60,8 @@ function createMarkdown(options?: FileTreeOptions) { } describe('fileTreePlugin', () => { - const code = `\ + it('should work with default options', () => { + const code = `\ :::file-tree - docs - README.md @@ -110,7 +111,27 @@ describe('fileTreePlugin', () => { ::: file-tree ::: ` - it('should work with default options', () => { + const md = createMarkdown() + + expect(md.render(code)).toMatchSnapshot() + }) + + // fix #795 + it('should work with nesting content', () => { + const code = `\ +- item1 + + ::: file-tree + - docs/ + - src + - a.js + - b.ts + - README.md + ::: + +- item2 +` + const md = createMarkdown() expect(md.render(code)).toMatchSnapshot() diff --git a/plugins/plugin-md-power/src/node/container/fileTree.ts b/plugins/plugin-md-power/src/node/container/fileTree.ts index d64d3b32..66b92e91 100644 --- a/plugins/plugin-md-power/src/node/container/fileTree.ts +++ b/plugins/plugin-md-power/src/node/container/fileTree.ts @@ -43,13 +43,15 @@ export interface FileTreeNodeProps { export function parseFileTreeRawContent(content: string): FileTreeNode[] { const root: FileTreeNode = { info: '', level: -1, children: [] } const stack: FileTreeNode[] = [root] - const lines = content.trim().split('\n') + const lines = content.trimEnd().split('\n') + const spaceLength = lines[0].match(/^\s*/)?.[0].length ?? 0 // 去除行首空格/) + for (const line of lines) { const match = line.match(/^(\s*)-(.*)$/) if (!match) continue - const level = Math.floor(match[1].length / 2) // 每两个空格为一个层级 + const level = Math.floor((match[1].length - spaceLength) / 2) // 每两个空格为一个层级 const info = match[2].trim() // 检索当前层级的父节点