|
1 | 1 | import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit' |
2 | | -import type { Options } from 'unplugin-vue-markdown/types' |
| 2 | +import type { Options as MarkdownOptions } from 'unplugin-vue-markdown/types' |
3 | 3 | import Markdown from 'unplugin-vue-markdown' |
4 | 4 |
|
5 | | -export interface ModuleOptions extends Options { } |
| 5 | +export interface ModuleOptions extends MarkdownOptions { } |
6 | 6 |
|
7 | 7 | export default defineNuxtModule<ModuleOptions>({ |
8 | 8 | meta: { |
@@ -51,37 +51,57 @@ export default defineNuxtModule<ModuleOptions>({ |
51 | 51 | nuxt.options.vite.vue.include.push(reEndingOrQuery) |
52 | 52 | } |
53 | 53 |
|
54 | | - options.include = [reEndingOrQuery] |
55 | 54 | const originalMarkdownItSetup = options.markdownItSetup |
56 | | - options.markdownItSetup = async (md) => { |
57 | | - await originalMarkdownItSetup?.(md) |
| 55 | + const resolvedOptions: MarkdownOptions = { |
| 56 | + wrapperClasses: '', |
| 57 | + ...options, |
| 58 | + include: [reEndingOrQuery], |
| 59 | + headEnabled: false, |
| 60 | + async markdownItSetup(md) { |
| 61 | + await originalMarkdownItSetup?.(md) |
58 | 62 |
|
59 | | - // Replace <a> with <NuxtLink> |
60 | | - md.core.ruler.push( |
61 | | - 'NuxtLink', |
62 | | - (state) => { |
63 | | - type Token = (typeof state.tokens)[0] |
64 | | - function visit(token: Token) { |
65 | | - if (token.tag === 'a') { |
66 | | - token.tag = 'NuxtLink' |
67 | | - // Replace `href` with `to` |
68 | | - const hrefTag = token.attrs?.find(([name]) => name === 'href') |
69 | | - if (hrefTag) |
70 | | - hrefTag[0] = 'to' |
| 63 | + // Replace <a> with <NuxtLink> |
| 64 | + md.core.ruler.push( |
| 65 | + 'NuxtLink', |
| 66 | + (state) => { |
| 67 | + type Token = (typeof state.tokens)[0] |
| 68 | + function visit(token: Token) { |
| 69 | + if (token.tag === 'a') { |
| 70 | + token.tag = 'NuxtLink' |
| 71 | + // Replace `href` with `to` |
| 72 | + const hrefTag = token.attrs?.find(([name]) => name === 'href') |
| 73 | + if (hrefTag) |
| 74 | + hrefTag[0] = 'to' |
| 75 | + } |
| 76 | + token.children?.forEach(visit) |
71 | 77 | } |
72 | | - token.children?.forEach(visit) |
| 78 | + |
| 79 | + state.tokens.forEach(visit) |
| 80 | + }, |
| 81 | + ) |
| 82 | + }, |
| 83 | + |
| 84 | + transforms: { |
| 85 | + extraScripts(frontmatter) { |
| 86 | + const scripts: string[] = [] |
| 87 | + |
| 88 | + if (frontmatter.meta) |
| 89 | + scripts.push(`definePageMeta(${JSON.stringify(frontmatter.meta)})`) |
| 90 | + |
| 91 | + if (frontmatter.seo || frontmatter.title || frontmatter.description) { |
| 92 | + scripts.push(`useSeoMeta(${JSON.stringify({ |
| 93 | + ...frontmatter.title ? { title: frontmatter.title } : {}, |
| 94 | + ...frontmatter.description ? { description: frontmatter.description } : {}, |
| 95 | + ...frontmatter.seo, |
| 96 | + })})`) |
73 | 97 | } |
74 | 98 |
|
75 | | - state.tokens.forEach(visit) |
| 99 | + return scripts |
76 | 100 | }, |
77 | | - ) |
| 101 | + }, |
78 | 102 | } |
79 | 103 |
|
80 | | - options.wrapperClasses = options.wrapperClasses || '' |
81 | | - // Force disabling head (leveraging `seo` key) |
82 | | - options.headEnabled = false |
83 | | - |
84 | | - addVitePlugin(() => Markdown.vite(options)) |
85 | | - addWebpackPlugin(() => Markdown.webpack(options)) |
| 104 | + addVitePlugin(() => Markdown.vite(resolvedOptions)) |
| 105 | + addWebpackPlugin(() => Markdown.webpack(resolvedOptions)) |
86 | 106 | }, |
87 | 107 | }) |
0 commit comments