Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow lang='ts' on scripts in markdown #701

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ export async function createMarkdownToVueRenderFn(
}

const scriptRE = /<\/script>/
const scriptLangTsRE = /<\s*script[^>]*\blang=['"]ts['"][^>]*/
const scriptSetupRE = /<\s*script[^>]*\bsetup\b[^>]*/
const scriptClientRe = /<\s*script[^>]*\bclient\b[^>]*/
const scriptClientRE = /<\s*script[^>]*\bclient\b[^>]*/
const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)as(\s*)default/

Expand All @@ -176,10 +177,12 @@ function genPageDataCode(tags: string[], data: PageData) {
return (
scriptRE.test(tag) &&
!scriptSetupRE.test(tag) &&
!scriptClientRe.test(tag)
!scriptClientRE.test(tag)
)
})

const isUsingTS = tags.findIndex((tag) => scriptLangTsRE.test(tag)) > -1

if (existingScriptIndex > -1) {
const tagSrc = tags[existingScriptIndex]
// user has <script> tag inside markdown
Expand All @@ -196,7 +199,9 @@ function genPageDataCode(tags: string[], data: PageData) {
)
} else {
tags.unshift(
`<script>${code}\nexport default {name:'${data.relativePath}'}</script>`
`<script ${isUsingTS ? 'lang="ts"' : ''}>${code}\nexport default {name:'${
data.relativePath
}'}</script>`
)
}

Expand Down