fix(plugin-md-power): fix stringifyAttrs parse fail (#701)

This commit is contained in:
pengzhanbo 2025-09-20 15:13:21 +08:00 committed by GitHub
parent 69b9d0bc3d
commit 001896b5fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 7 deletions

View File

@ -151,7 +151,7 @@ export function codeTreePlugin(md: Markdown, app: App, options: CodeTreeOptions
expanded: true,
filepath: node.filepath,
}
return `<FileTreeNode${stringifyAttrs(props)}>
return `<FileTreeNode${stringifyAttrs(props, false, ['filename', 'filepath'])}>
<template #icon><VPIcon provider="iconify" name="${getIcon(node.filename, props.type, mode)}" /></template>
${node.children?.length ? renderFileTree(node.children, mode) : ''}
</FileTreeNode>`

View File

@ -162,7 +162,7 @@ export function fileTreePlugin(md: Markdown, options: FileTreeOptions = {}): voi
filename,
level,
}
return `<FileTreeNode${stringifyAttrs(props)}>
return `<FileTreeNode${stringifyAttrs(props, false, ['filename'])}>
${renderedIcon}${renderedComment}${children.length > 0 ? renderFileTree(children, meta) : ''}
</FileTreeNode>`
}).join('\n')

View File

@ -2,16 +2,17 @@ import { isBoolean, isNull, isNumber, isString, isUndefined, kebabCase } from '@
export function stringifyAttrs<T extends object = object>(
attrs: T,
withUndefined = false,
withUndefinedOrNull = false,
forceStringify: (keyof T)[] = [],
): string {
const result = Object.entries(attrs)
.map(([key, value]) => {
const k = kebabCase(key)
if (isUndefined(value) || value === 'undefined')
return withUndefined ? `:${k}="undefined"` : ''
return withUndefinedOrNull ? `:${k}="undefined"` : ''
if (isNull(value) || value === 'null')
return withUndefined ? `:${k}="null"` : ''
return withUndefinedOrNull ? `:${k}="null"` : ''
if (value === 'true')
value = true
@ -25,8 +26,12 @@ export function stringifyAttrs<T extends object = object>(
return `:${k}="${value}"`
// like object or array
if (isString(value) && (value[0] === '{' || value[0] === '['))
return `:${k}="${value.replaceAll('\"', '\'')}"`
if (isString(value) && (value[0] === '{' || value[0] === '[')) {
const v = value.replaceAll('\"', '\'')
if (forceStringify.includes(key as keyof T))
return `${k}="${v}"`
return `:${k}="${v}"`
}
const hasDynamic = key[0] === ':'
return `${hasDynamic ? ':' : ''}${k}="${String(value)}"`