From be47c9a025dc6cf3154955dcf2f7b18c2a04974d Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Sat, 14 Sep 2024 23:02:34 +0800 Subject: [PATCH] fix(plugin-md-power): incorrect empty tree node (#179) --- .../src/node/features/fileTree/index.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plugins/plugin-md-power/src/node/features/fileTree/index.ts b/plugins/plugin-md-power/src/node/features/fileTree/index.ts index 7508b1d5..818f307a 100644 --- a/plugins/plugin-md-power/src/node/features/fileTree/index.ts +++ b/plugins/plugin-md-power/src/node/features/fileTree/index.ts @@ -18,6 +18,7 @@ export async function fileTreePlugin(app: App, md: Markdown) { const validate = (info: string): boolean => info.trim().startsWith(type) const render = (tokens: Token[], idx: number): string => { if (tokens[idx].nesting === 1) { + const hasRes: number[] = [] // level stack for ( let i = idx + 1; !(tokens[i].nesting === -1 @@ -28,6 +29,7 @@ export async function fileTreePlugin(app: App, md: Markdown) { if (token.type === 'list_item_open') { const result = resolveTreeNodeInfo(tokens, token, i) if (result) { + hasRes.push(token.level) const [info, inline] = result const { filename, type, expanded, empty } = info const icon = type === 'file' ? getFileIcon(filename) : folderIcon @@ -40,10 +42,15 @@ export async function fileTreePlugin(app: App, md: Markdown) { updateInlineToken(inline, info, `${classPrefix}${icon.name}`) addIcon(icon) } + else { + hasRes.push(-1) + } } else if (token.type === 'list_item_close') { - token.type = itemClose - token.tag = componentName + if (token.level === hasRes.pop()) { + token.type = itemClose + token.tag = componentName + } } } const info = tokens[idx].info.trim()