Skip to content

Commit 145bfff

Browse files
committedJul 21, 2023
fix: avoid exporting js keywords
1 parent c3c8726 commit 145bfff

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed
 

‎src/markdown.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ import { preprocessHead } from './head'
99
const scriptSetupRE = /<\s*script([^>]*)\bsetup\b([^>]*)>([\s\S]*)<\/script>/mg
1010
const defineExposeRE = /defineExpose\s*\(/mg
1111

12+
const EXPORTS_KEYWORDS = [
13+
'class',
14+
'default',
15+
'export',
16+
'function',
17+
'import',
18+
'let',
19+
'var',
20+
'const',
21+
'from',
22+
'as',
23+
'return',
24+
'if',
25+
'else',
26+
'switch',
27+
'case',
28+
'break',
29+
'for',
30+
'while',
31+
'do',
32+
]
33+
1234
interface ScriptMeta {
1335
code: string
1436
attr: string
@@ -150,7 +172,14 @@ export function createMarkdown(options: ResolvedOptions) {
150172

151173
scriptLines.push(`const frontmatter = ${JSON.stringify(frontmatter)}`)
152174

153-
frontmatterExportsLines = Object.entries(frontmatter).map(([key, value]) => `export const ${key} = ${JSON.stringify(value)}`)
175+
if (options.exportFontmatter) {
176+
frontmatterExportsLines = Object.entries(frontmatter)
177+
.map(([key, value]) => {
178+
if (EXPORTS_KEYWORDS.includes(key))
179+
key = `_${key}`
180+
return `export const ${key} = ${JSON.stringify(value)}`
181+
})
182+
}
154183

155184
if (!isVue2 && options.exposeFrontmatter && !hasExplicitExports())
156185
scriptLines.push('defineExpose({ frontmatter })')

‎src/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function resolveOptions(userOptions: Options): ResolvedOptions {
99
excerpt: false,
1010
exposeFrontmatter: true,
1111
exposeExcerpt: false,
12+
exportFontmatter: true,
1213
escapeCodeTagInterpolation: true,
1314
customSfcBlocks: ['route', 'i18n', 'style'],
1415
componentOptions: {},

‎src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ export interface Options {
132132
*/
133133
exposeExcerpt?: boolean
134134

135+
/**
136+
* Export frontmatter in component module
137+
*
138+
* @default true
139+
*/
140+
exportFontmatter?: boolean
141+
135142
/**
136143
* Add `v-pre` to `<code>` tag to escape curly brackets interpolation
137144
*

‎test/__snapshots__/transform.test.ts.snap

+13
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ defineExpose({ frontmatter })
5656
</script>"
5757
`;
5858
59+
exports[`transform > export keyword frontmatters 1`] = `
60+
"<template><div class=\\"markdown-body\\"><p>Hello</p>
61+
</div></template>
62+
<script setup>
63+
const frontmatter = {\\"class\\":\\"text\\",\\"default\\":\\"foo\\"}
64+
defineExpose({ frontmatter })
65+
</script>
66+
<script>
67+
export const _class = \\"text\\"
68+
export const _default = \\"foo\\"
69+
</script>"
70+
`;
71+
5972
exports[`transform > exposes frontmatter 1`] = `
6073
"<template><div class=\\"markdown-body\\"><h1>Hello</h1>
6174
</div></template>

‎test/transform.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ function onClick() {
9898
</script>
9999
100100
<button @click="onClick"></button>
101+
`
102+
expect(markdownToVue('', md).code).toMatchSnapshot()
103+
})
104+
105+
it('export keyword frontmatters', () => {
106+
const md = `
107+
---
108+
class: 'text'
109+
default: 'foo'
110+
---
111+
112+
Hello
101113
`
102114
expect(markdownToVue('', md).code).toMatchSnapshot()
103115
})

0 commit comments

Comments
 (0)
Please sign in to comment.