fix(plugin-md-power): incorrect demo container rendering

This commit is contained in:
pengzhanbo 2025-04-28 19:22:02 +08:00
parent a446a86dbf
commit 0bf7c865ac
6 changed files with 15 additions and 12 deletions

View File

@ -18,6 +18,10 @@ describe('stringifyAttrs', () => {
expect(stringifyAttrs({ disabled: true, readonly: false, checked: 'true', selected: 'false' })).toBe(' disabled checked')
})
it('should handle dymamic attributes', () => {
expect(stringifyAttrs({ ':id': 'test' })).toBe(' :id="test"')
})
it('should handle null and undefined values', () => {
expect(stringifyAttrs({ id: null, class: undefined })).toBe('')
expect(stringifyAttrs({ id: null, class: undefined, foo: 'undefined', bar: 'null' }, true)).toBe(' :id="null" :class="undefined" :foo="undefined" :bar="null"')

View File

@ -28,9 +28,9 @@ export function useResources(el: ShallowRef<HTMLDivElement | null>, config: Mayb
if (!conf)
return []
return [
{ name: 'JavaScript', items: conf.jsLib.map(url => ({ name: normalizeName(url), url })) },
{ name: 'CSS', items: conf.cssLib.map(url => ({ name: normalizeName(url), url })) },
].filter(i => i.items.length)
{ name: 'JavaScript', items: conf.jsLib?.map(url => ({ name: normalizeName(url), url })) },
{ name: 'CSS', items: conf.cssLib?.map(url => ({ name: normalizeName(url), url })) },
].filter(i => i.items?.length)
})
function normalizeName(url: string) {

View File

@ -29,7 +29,7 @@ export function markdownEmbed(
<template #code>
${md.render(`\`\`\`md ${codeSetting}\n${code}\n\`\`\``, {})}
</template>
</VPDemoBasic$>`
</VPDemoBasic>`
}
export const markdownContainerRender: DemoContainerRender = {

View File

@ -126,10 +126,9 @@ export function normalEmbed(
env.demoFiles.push(demo)
insertSetupScript({ ...demo, path: output }, env)
}
return `<VPDemoNormal${stringifyAttrs({ config: name, title, desc, expanded })}>
return `<VPDemoNormal${stringifyAttrs({ ':config': name, title, desc, expanded })}>
${codeToHtml(md, source, codeSetting)}
</VPDemoNormal$>`
</VPDemoNormal>`
}
export const normalContainerRender: DemoContainerRender = {
@ -148,8 +147,7 @@ export const normalContainerRender: DemoContainerRender = {
const source = parseContainerCode(codeMap)
compileCode(source, output)
return `<VPDemoNormal${stringifyAttrs({ config: name, title, desc, expanded })}>`
return `<VPDemoNormal${stringifyAttrs({ ':config': name, title, desc, expanded })}>`
},
after: () => '</VPDemoNormal>',

View File

@ -36,7 +36,7 @@ export function vueEmbed(
<template #code>
${md.render(`\`\`\`${ext}${codeSetting}\n${code}\n\`\`\``, {})}
</template>
</VPDemoBasic$>`
</VPDemoBasic>`
}
const target = 'md-power/demo/vue'

View File

@ -6,7 +6,7 @@ export function stringifyAttrs<T extends object = object>(
): string {
const result = Object.entries(attrs)
.map(([key, value]) => {
const k = kebabCase(String(key))
const k = kebabCase(key)
if (isUndefined(value) || value === 'undefined')
return withUndefined ? `:${k}="undefined"` : ''
@ -28,7 +28,8 @@ export function stringifyAttrs<T extends object = object>(
if (isString(value) && (value[0] === '{' || value[0] === '['))
return `:${k}="${value.replaceAll('\"', '\'')}"`
return `${k}="${String(value)}"`
const hasDynamic = key[0] === ':'
return `${hasDynamic ? ':' : ''}${k}="${String(value)}"`
})
.filter(Boolean)
.join(' ')