-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-sfc): extract SFC blocks as structured data
BREAKING CHANGE: the type of `env.sfcBlocks` has been changed
- Loading branch information
Showing
9 changed files
with
343 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const TAG_NAME_SCRIPT = 'script'; | ||
export const TAG_NAME_STYLE = 'style'; | ||
export const TAG_NAME_TEMPLATE = 'template'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './constants.js'; | ||
export * from './sfc-plugin.js'; | ||
export * from './sfc-regexp.js'; | ||
export * from './types.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,91 @@ | ||
import type { MarkdownItEnv } from '@mdit-vue/types'; | ||
import type { PluginWithOptions } from 'markdown-it'; | ||
import { | ||
TAG_NAME_SCRIPT, | ||
TAG_NAME_STYLE, | ||
TAG_NAME_TEMPLATE, | ||
} from './constants.js'; | ||
import { SCRIPT_SETUP_TAG_OPEN_REGEXP, createSfcRegexp } from './sfc-regexp.js'; | ||
import type { SfcRegExpMatchArray } from './sfc-regexp.js'; | ||
import type { SfcPluginOptions } from './types.js'; | ||
|
||
/** | ||
* Avoid rendering vue SFC script / style / custom blocks | ||
* Get Vue SFC blocks | ||
* | ||
* Extract them into env | ||
* Extract them into env and avoid rendering them | ||
*/ | ||
export const sfcPlugin: PluginWithOptions<SfcPluginOptions> = ( | ||
md, | ||
{ customBlocks = [] }: SfcPluginOptions = {}, | ||
): void => { | ||
// extract `<script>`, `<style>` and other user defined custom blocks | ||
const sfcBlocks = Array.from(new Set(['script', 'style', ...customBlocks])); | ||
const sfcBlocksRegexp = new RegExp( | ||
`^<(${sfcBlocks.join('|')})(?=(\\s|>|$))`, | ||
'i', | ||
); | ||
|
||
const rawRule = md.renderer.rules.html_block!; | ||
const sfcRegexp = createSfcRegexp({ customBlocks }); | ||
|
||
// wrap the original render function | ||
const render = md.render.bind(md); | ||
md.render = (src, env: MarkdownItEnv = {}) => { | ||
// initialize `env.sfcBlocks` | ||
env.sfcBlocks = { | ||
template: null, | ||
script: null, | ||
scriptSetup: null, | ||
styles: [], | ||
customBlocks: [], | ||
}; | ||
|
||
// call the original render function to get the rendered result | ||
const rendered = render(src, env); | ||
|
||
// create template block from the rendered result | ||
env.sfcBlocks.template = { | ||
type: TAG_NAME_TEMPLATE, | ||
content: `<${TAG_NAME_TEMPLATE}>${rendered}</${TAG_NAME_TEMPLATE}>`, | ||
contentStripped: rendered, | ||
tagOpen: `<${TAG_NAME_TEMPLATE}>`, | ||
tagClose: `</${TAG_NAME_TEMPLATE}>`, | ||
}; | ||
|
||
return rendered; | ||
}; | ||
|
||
// wrap the original html_block renderer rule | ||
const htmlBlockRule = md.renderer.rules.html_block!; | ||
md.renderer.rules.html_block = ( | ||
tokens, | ||
idx, | ||
options, | ||
env: MarkdownItEnv, | ||
self, | ||
) => { | ||
const content = tokens[idx].content; | ||
// skip if `env.sfcBlocks` is not initialized | ||
if (!env.sfcBlocks) { | ||
return htmlBlockRule(tokens, idx, options, env, self); | ||
} | ||
|
||
// get the html_block token | ||
const token = tokens[idx]; | ||
const content = token.content; | ||
|
||
// try to match sfc | ||
const match = content.match(sfcRegexp) as SfcRegExpMatchArray | null; | ||
if (!match) { | ||
return htmlBlockRule(tokens, idx, options, env, self); | ||
} | ||
|
||
// extract sfc blocks to env and do not render them | ||
if (sfcBlocksRegexp.test(content.trim())) { | ||
env.sfcBlocks ??= []; | ||
env.sfcBlocks.push(content); | ||
return ''; | ||
// extract sfc blocks to `env.sfcBlocks` | ||
const sfcBlock = match.groups; | ||
if (sfcBlock.type === TAG_NAME_SCRIPT) { | ||
if (SCRIPT_SETUP_TAG_OPEN_REGEXP.test(sfcBlock.tagOpen)) { | ||
env.sfcBlocks.scriptSetup = sfcBlock; | ||
} else { | ||
env.sfcBlocks.script = sfcBlock; | ||
} | ||
} else if (sfcBlock.type === TAG_NAME_STYLE) { | ||
env.sfcBlocks.styles.push(sfcBlock); | ||
} else { | ||
env.sfcBlocks.customBlocks.push(sfcBlock); | ||
} | ||
|
||
return rawRule(tokens, idx, options, env, self); | ||
// avoid rendering sfc blocks | ||
return ''; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { TAG_NAME_SCRIPT, TAG_NAME_STYLE } from './constants.js'; | ||
import type { SfcBlock, SfcPluginOptions } from './types.js'; | ||
|
||
export const SCRIPT_SETUP_TAG_OPEN_REGEXP = /^<script\s+.*?\bsetup\b.*?>$/is; | ||
|
||
export interface SfcRegExpMatchArray extends Omit<RegExpMatchArray, 'groups'> { | ||
groups: SfcBlock; | ||
} | ||
|
||
/** | ||
* Generate RegExp for sfc blocks | ||
*/ | ||
export const createSfcRegexp = ({ | ||
customBlocks, | ||
}: Required<Pick<SfcPluginOptions, 'customBlocks'>>): RegExp => { | ||
const sfcTags = Array.from( | ||
new Set([TAG_NAME_SCRIPT, TAG_NAME_STYLE, ...customBlocks]), | ||
).join('|'); | ||
return new RegExp( | ||
`^\\s*(?<content>(?<tagOpen><(?<type>${sfcTags})\\s?.*?>)(?<contentStripped>.*)(?<tagClose><\\/\\k<type>\\s*>))\\s*$`, | ||
'is', | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
packages/plugin-sfc/tests/__snapshots__/sfc-plugin.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`@mdit-vue/plugin-sfc > sfc-plugin > should extract custom blocks correctly 1`] = ` | ||
"<h1>hello vuepress</h1> | ||
<p>{{ msg }}</p> | ||
" | ||
`; | ||
|
||
exports[`@mdit-vue/plugin-sfc > sfc-plugin > should extract custom blocks correctly 2`] = ` | ||
{ | ||
"customBlocks": [ | ||
{ | ||
"content": "<docs> | ||
extra hoisted tag | ||
</docs>", | ||
"contentStripped": " | ||
extra hoisted tag | ||
", | ||
"tagClose": "</docs>", | ||
"tagOpen": "<docs>", | ||
"type": "docs", | ||
}, | ||
], | ||
"script": { | ||
"content": "<script> | ||
export default { | ||
setup() { | ||
return { | ||
msg: 'script' | ||
} | ||
} | ||
} | ||
</script>", | ||
"contentStripped": " | ||
export default { | ||
setup() { | ||
return { | ||
msg: 'script' | ||
} | ||
} | ||
} | ||
", | ||
"tagClose": "</script>", | ||
"tagOpen": "<script>", | ||
"type": "script", | ||
}, | ||
"scriptSetup": { | ||
"content": "<script setup lang=\\"ts\\"> | ||
const foo = 'scriptSetup' | ||
</script>", | ||
"contentStripped": " | ||
const foo = 'scriptSetup' | ||
", | ||
"tagClose": "</script>", | ||
"tagOpen": "<script setup lang=\\"ts\\">", | ||
"type": "script", | ||
}, | ||
"styles": [ | ||
{ | ||
"content": "<style lang=\\"stylus\\"> | ||
.h1 | ||
red | ||
</style>", | ||
"contentStripped": " | ||
.h1 | ||
red | ||
", | ||
"tagClose": "</style>", | ||
"tagOpen": "<style lang=\\"stylus\\">", | ||
"type": "style", | ||
}, | ||
], | ||
"template": { | ||
"content": "<template><h1>hello vuepress</h1> | ||
<p>{{ msg }}</p> | ||
</template>", | ||
"contentStripped": "<h1>hello vuepress</h1> | ||
<p>{{ msg }}</p> | ||
", | ||
"tagClose": "</template>", | ||
"tagOpen": "<template>", | ||
"type": "template", | ||
}, | ||
} | ||
`; | ||
exports[`@mdit-vue/plugin-sfc > sfc-plugin > should extract default sfc blocks correctly 1`] = ` | ||
"<h1>hello vuepress</h1> | ||
<p>{{ msg }}</p> | ||
<docs> | ||
extra hoisted tag | ||
</docs> | ||
" | ||
`; | ||
exports[`@mdit-vue/plugin-sfc > sfc-plugin > should extract default sfc blocks correctly 2`] = ` | ||
{ | ||
"customBlocks": [], | ||
"script": { | ||
"content": "<script> | ||
export default { | ||
setup() { | ||
return { | ||
msg: 'script' | ||
} | ||
} | ||
} | ||
</script>", | ||
"contentStripped": " | ||
export default { | ||
setup() { | ||
return { | ||
msg: 'script' | ||
} | ||
} | ||
} | ||
", | ||
"tagClose": "</script>", | ||
"tagOpen": "<script>", | ||
"type": "script", | ||
}, | ||
"scriptSetup": { | ||
"content": "<script setup lang=\\"ts\\"> | ||
const foo = 'scriptSetup' | ||
</script>", | ||
"contentStripped": " | ||
const foo = 'scriptSetup' | ||
", | ||
"tagClose": "</script>", | ||
"tagOpen": "<script setup lang=\\"ts\\">", | ||
"type": "script", | ||
}, | ||
"styles": [ | ||
{ | ||
"content": "<style lang=\\"stylus\\"> | ||
.h1 | ||
red | ||
</style>", | ||
"contentStripped": " | ||
.h1 | ||
red | ||
", | ||
"tagClose": "</style>", | ||
"tagOpen": "<style lang=\\"stylus\\">", | ||
"type": "style", | ||
}, | ||
], | ||
"template": { | ||
"content": "<template><h1>hello vuepress</h1> | ||
<p>{{ msg }}</p> | ||
<docs> | ||
extra hoisted tag | ||
</docs> | ||
</template>", | ||
"contentStripped": "<h1>hello vuepress</h1> | ||
<p>{{ msg }}</p> | ||
<docs> | ||
extra hoisted tag | ||
</docs> | ||
", | ||
"tagClose": "</template>", | ||
"tagOpen": "<template>", | ||
"type": "template", | ||
}, | ||
} | ||
`; |
Oops, something went wrong.