Skip to content

Commit 345143d

Browse files
committed
chore: update snap
1 parent 78c4501 commit 345143d

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/markdown.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export function createMarkdown(options: ResolvedOptions) {
9999
let excerptExportsLine = ''
100100
let excerptKeyOverlapping = false
101101

102+
function hasExplicitExports() {
103+
return defineExposeRE.test(hoistScripts.scripts.map(i => i.code).join(''))
104+
}
105+
102106
if (options.frontmatter) {
103107
if (options.excerpt && data) {
104108
if (data.excerpt !== undefined)
@@ -115,7 +119,7 @@ export function createMarkdown(options: ResolvedOptions) {
115119

116120
frontmatterExportsLines = Object.entries(frontmatter).map(([key, value]) => `export const ${key} = ${JSON.stringify(value)}`)
117121

118-
if (!isVue2 && options.exposeFrontmatter && !defineExposeRE.test(hoistScripts.scripts.join('')))
122+
if (!isVue2 && options.exposeFrontmatter && !hasExplicitExports())
119123
scriptLines.push('defineExpose({ frontmatter })')
120124

121125
if (!isVue2 && headEnabled && head) {
@@ -131,38 +135,40 @@ export function createMarkdown(options: ResolvedOptions) {
131135
if (!excerptKeyOverlapping)
132136
excerptExportsLine = `export const excerpt = ${JSON.stringify(excerpt)}\n`
133137

134-
if (!isVue2 && options.exposeExcerpt && !defineExposeRE.test(hoistScripts.scripts.join('')))
138+
if (!isVue2 && options.exposeExcerpt && !hasExplicitExports())
135139
scriptLines.push('defineExpose({ excerpt })')
136140
}
137141

138142
scriptLines.push(...hoistScripts.scripts.map(i => i.code))
139143

140-
const attrs = uniq(hoistScripts.scripts.map(i => i.attr)).join(' ')
144+
let attrs = uniq(hoistScripts.scripts.map(i => i.attr)).join(' ').trim()
145+
if (attrs)
146+
attrs = ` ${attrs}`
141147

142148
const scripts = isVue2
143149
? [
144-
`<script ${attrs}>`,
150+
`<script${attrs}>`,
145151
...scriptLines,
146152
...frontmatterExportsLines,
147153
excerptExportsLine,
148154
'export default { data() { return { frontmatter } } }',
149155
'</script>',
150156
]
151157
: [
152-
`<script setup ${attrs}>`,
158+
`<script setup${attrs}>`,
153159
...scriptLines,
154160
'</script>',
155161
...((frontmatterExportsLines.length || excerptExportsLine)
156162
? [
157-
`<script ${attrs}>`,
163+
`<script${attrs}>`,
158164
...frontmatterExportsLines,
159165
excerptExportsLine,
160166
'</script>',
161167
]
162168
: []),
163169
]
164170

165-
const sfc = `<template>${html}</template>\n${scripts.join('\n')}\n${customBlocks.blocks.join('\n')}\n`
171+
const sfc = `<template>${html}</template>\n${scripts.filter(Boolean).join('\n')}\n${customBlocks.blocks.join('\n')}\n`
166172

167173
return sfc
168174
}

test/__snapshots__/excerpt.test.ts.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ const excerpt = \\"\\\\nThis is an excerpt.\\\\n\\\\n\\"
1717
</script>
1818
<script>
1919
export const title = \\"Hey\\"
20-
</script>
21-
<script>
2220
export const excerpt = \\"\\\\nThis is an excerpt.\\\\n\\\\n\\"
21+
2322
</script>
2423
2524
"

test/__snapshots__/transform.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ exports[`transform > script setup 1`] = `
8181
"<template><div class=\\"markdown-body\\"><h1>Hello</h1>
8282
8383
</div></template>
84-
<script setup>
84+
<script setup lang=\\"ts\\">
8585
const frontmatter = {}
8686
defineExpose({ frontmatter })
8787

0 commit comments

Comments
 (0)