@nuxtjs/mdc 0.3.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config.cjs +1 -0
- package/dist/config.d.mts +49 -0
- package/dist/config.d.ts +49 -0
- package/dist/config.mjs +5 -0
- package/dist/module.d.mts +39 -14
- package/dist/module.d.ts +39 -14
- package/dist/module.json +1 -1
- package/dist/module.mjs +261 -79
- package/dist/runtime/components/MDCRenderer.vue +2 -2
- package/dist/runtime/{shiki → highlighter}/event-handler.d.ts +1 -1
- package/dist/runtime/highlighter/event-handler.mjs +8 -0
- package/dist/runtime/highlighter/rehype.d.ts +8 -0
- package/dist/runtime/{shiki/index.mjs → highlighter/rehype.mjs} +12 -13
- package/dist/runtime/highlighter/shiki.d.ts +14 -0
- package/dist/runtime/highlighter/shiki.mjs +159 -0
- package/dist/runtime/highlighter/types.d.ts +14 -0
- package/dist/runtime/index.d.ts +0 -1
- package/dist/runtime/index.mjs +0 -1
- package/dist/runtime/parser/compiler.mjs +2 -1
- package/dist/runtime/parser/index.d.ts +1 -1
- package/dist/runtime/parser/index.mjs +38 -4
- package/dist/runtime/types/config.d.ts +45 -0
- package/dist/runtime/types/config.mjs +0 -0
- package/dist/runtime/types/parser.d.ts +7 -5
- package/dist/runtime/utils/node.d.ts +1 -1
- package/dist/shared/mdc.a6f76af4.d.mts +9 -0
- package/dist/shared/mdc.a6f76af4.d.ts +9 -0
- package/dist/types.d.mts +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +34 -15
- package/dist/runtime/shiki/event-handler.mjs +0 -17
- package/dist/runtime/shiki/highlighter.d.ts +0 -5
- package/dist/runtime/shiki/highlighter.mjs +0 -161
- package/dist/runtime/shiki/index.d.ts +0 -8
- package/dist/runtime/shiki/types.d.ts +0 -13
- /package/dist/runtime/{shiki → highlighter}/types.mjs +0 -0
package/dist/config.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = {}; module.exports.defineConfig = configs => configs
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Highlighter, ShikiTransformer, HighlighterCore } from 'shiki';
|
|
2
|
+
import { Processor } from 'unified';
|
|
3
|
+
import { M as MdcThemeOptions, H as HighlighterOptions } from './shared/mdc.a6f76af4.mjs';
|
|
4
|
+
|
|
5
|
+
type Awaitable<T> = T | Promise<T>;
|
|
6
|
+
interface MdcConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Hooks for the unified markdown pipeline
|
|
9
|
+
*/
|
|
10
|
+
unified?: {
|
|
11
|
+
/**
|
|
12
|
+
* Custom setup for unified processor before other plugins
|
|
13
|
+
*/
|
|
14
|
+
pre?: (processor: Processor) => Awaitable<void | Processor>;
|
|
15
|
+
/**
|
|
16
|
+
* Custom setup for unified processor after remark but before rehype
|
|
17
|
+
*/
|
|
18
|
+
remark?: (processor: Processor) => Awaitable<void | Processor>;
|
|
19
|
+
/**
|
|
20
|
+
* Custom setup for unified processor after rehype
|
|
21
|
+
*/
|
|
22
|
+
rehype?: (processor: Processor) => Awaitable<void | Processor>;
|
|
23
|
+
/**
|
|
24
|
+
* Custom setup for unified processor after all plugins
|
|
25
|
+
*/
|
|
26
|
+
post?: (processor: Processor) => Awaitable<void | Processor>;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Custom hightlighter, available when `highlighter` is set to `custom`
|
|
30
|
+
*/
|
|
31
|
+
highlighter?: Highlighter;
|
|
32
|
+
/**
|
|
33
|
+
* Hooks for shiki
|
|
34
|
+
*/
|
|
35
|
+
shiki?: {
|
|
36
|
+
/**
|
|
37
|
+
* Get transformers for shiki
|
|
38
|
+
*/
|
|
39
|
+
transformers?: ShikiTransformer[] | ((code: string, lang: string, theme: MdcThemeOptions, options: Partial<HighlighterOptions>) => Awaitable<ShikiTransformer[]>);
|
|
40
|
+
/**
|
|
41
|
+
* Custom setup for shiki instance, only called once on server or client
|
|
42
|
+
*/
|
|
43
|
+
setup?: (highlighter: HighlighterCore) => Awaitable<void>;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare function defineConfig(config: MdcConfig): MdcConfig;
|
|
48
|
+
|
|
49
|
+
export { defineConfig };
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Highlighter, ShikiTransformer, HighlighterCore } from 'shiki';
|
|
2
|
+
import { Processor } from 'unified';
|
|
3
|
+
import { M as MdcThemeOptions, H as HighlighterOptions } from './shared/mdc.a6f76af4.js';
|
|
4
|
+
|
|
5
|
+
type Awaitable<T> = T | Promise<T>;
|
|
6
|
+
interface MdcConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Hooks for the unified markdown pipeline
|
|
9
|
+
*/
|
|
10
|
+
unified?: {
|
|
11
|
+
/**
|
|
12
|
+
* Custom setup for unified processor before other plugins
|
|
13
|
+
*/
|
|
14
|
+
pre?: (processor: Processor) => Awaitable<void | Processor>;
|
|
15
|
+
/**
|
|
16
|
+
* Custom setup for unified processor after remark but before rehype
|
|
17
|
+
*/
|
|
18
|
+
remark?: (processor: Processor) => Awaitable<void | Processor>;
|
|
19
|
+
/**
|
|
20
|
+
* Custom setup for unified processor after rehype
|
|
21
|
+
*/
|
|
22
|
+
rehype?: (processor: Processor) => Awaitable<void | Processor>;
|
|
23
|
+
/**
|
|
24
|
+
* Custom setup for unified processor after all plugins
|
|
25
|
+
*/
|
|
26
|
+
post?: (processor: Processor) => Awaitable<void | Processor>;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Custom hightlighter, available when `highlighter` is set to `custom`
|
|
30
|
+
*/
|
|
31
|
+
highlighter?: Highlighter;
|
|
32
|
+
/**
|
|
33
|
+
* Hooks for shiki
|
|
34
|
+
*/
|
|
35
|
+
shiki?: {
|
|
36
|
+
/**
|
|
37
|
+
* Get transformers for shiki
|
|
38
|
+
*/
|
|
39
|
+
transformers?: ShikiTransformer[] | ((code: string, lang: string, theme: MdcThemeOptions, options: Partial<HighlighterOptions>) => Awaitable<ShikiTransformer[]>);
|
|
40
|
+
/**
|
|
41
|
+
* Custom setup for shiki instance, only called once on server or client
|
|
42
|
+
*/
|
|
43
|
+
setup?: (highlighter: HighlighterCore) => Awaitable<void>;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare function defineConfig(config: MdcConfig): MdcConfig;
|
|
48
|
+
|
|
49
|
+
export { defineConfig };
|
package/dist/config.mjs
ADDED
package/dist/module.d.mts
CHANGED
|
@@ -1,23 +1,53 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
type Theme = BuiltinTheme | Record<string, BuiltinTheme>;
|
|
2
|
+
import { M as MdcThemeOptions } from './shared/mdc.a6f76af4.mjs';
|
|
3
|
+
import { BundledLanguage, LanguageRegistration, BundledTheme, ThemeRegistrationAny } from 'shiki';
|
|
5
4
|
|
|
6
5
|
interface UnistPlugin {
|
|
7
6
|
src?: string;
|
|
8
7
|
options?: Record<string, any>;
|
|
9
8
|
}
|
|
10
9
|
interface ModuleOptions {
|
|
10
|
+
/**
|
|
11
|
+
* A map of remark plugins to be used for processing markdown.
|
|
12
|
+
*/
|
|
11
13
|
remarkPlugins?: Record<string, UnistPlugin>;
|
|
14
|
+
/**
|
|
15
|
+
* A map of remark plugins to be used for processing markdown.
|
|
16
|
+
*/
|
|
12
17
|
rehypePlugins?: Record<string, UnistPlugin>;
|
|
13
18
|
highlight?: {
|
|
14
|
-
|
|
19
|
+
/**
|
|
20
|
+
* The highlighter to be used for highlighting code blocks.
|
|
21
|
+
*
|
|
22
|
+
* Set to `custom` to provide your own highlighter function in `mdc.config.ts`.
|
|
23
|
+
* Set to `shiki` to use the builtin highlighter based on Shiki.
|
|
24
|
+
* Or provide the path to your own highlighter module with the default export.
|
|
25
|
+
*
|
|
26
|
+
* @default 'shiki'
|
|
27
|
+
*/
|
|
28
|
+
highlighter?: 'shiki' | 'custom' | string;
|
|
15
29
|
/**
|
|
16
30
|
* Default theme that will be used for highlighting code blocks.
|
|
17
31
|
*/
|
|
18
|
-
theme?:
|
|
32
|
+
theme?: MdcThemeOptions;
|
|
33
|
+
/**
|
|
34
|
+
* Languages to be bundled loaded by Shiki
|
|
35
|
+
*
|
|
36
|
+
* All languages used has to be included in this list at build time, to create granular bundles.
|
|
37
|
+
*
|
|
38
|
+
* Unlike the `preload` option, when this option is provided, it will override the default languages.
|
|
39
|
+
*
|
|
40
|
+
* @default ['js','jsx','json','ts','tsx','vue','css','html','vue','bash','md','mdc','yaml']
|
|
41
|
+
*/
|
|
42
|
+
langs?: (BundledLanguage | LanguageRegistration)[];
|
|
43
|
+
/**
|
|
44
|
+
* Additional themes to be bundled loaded by Shiki
|
|
45
|
+
*/
|
|
46
|
+
themes?: (BundledTheme | ThemeRegistrationAny)[];
|
|
19
47
|
/**
|
|
20
48
|
* Preloaded languages that will be available for highlighting code blocks.
|
|
49
|
+
*
|
|
50
|
+
* @deprecated use `langs` instead.
|
|
21
51
|
*/
|
|
22
52
|
preload?: string[];
|
|
23
53
|
/**
|
|
@@ -38,17 +68,12 @@ interface ModuleOptions {
|
|
|
38
68
|
};
|
|
39
69
|
}
|
|
40
70
|
|
|
71
|
+
declare const DefaultHighlightLangs: BundledLanguage[];
|
|
41
72
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
42
73
|
|
|
43
74
|
declare module '@nuxt/schema' {
|
|
44
|
-
interface
|
|
45
|
-
mdc:
|
|
46
|
-
highlight: {
|
|
47
|
-
theme?: Theme;
|
|
48
|
-
preload?: string[];
|
|
49
|
-
wrapperStyle?: boolean | string;
|
|
50
|
-
};
|
|
51
|
-
};
|
|
75
|
+
interface NuxtHooks {
|
|
76
|
+
'mdc:configSources': (configs: string[]) => void;
|
|
52
77
|
}
|
|
53
78
|
interface PublicRuntimeConfig {
|
|
54
79
|
mdc: {
|
|
@@ -74,4 +99,4 @@ declare module '@nuxt/schema' {
|
|
|
74
99
|
}
|
|
75
100
|
}
|
|
76
101
|
|
|
77
|
-
export { _default as default };
|
|
102
|
+
export { DefaultHighlightLangs, _default as default };
|
package/dist/module.d.ts
CHANGED
|
@@ -1,23 +1,53 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
type Theme = BuiltinTheme | Record<string, BuiltinTheme>;
|
|
2
|
+
import { M as MdcThemeOptions } from './shared/mdc.a6f76af4.js';
|
|
3
|
+
import { BundledLanguage, LanguageRegistration, BundledTheme, ThemeRegistrationAny } from 'shiki';
|
|
5
4
|
|
|
6
5
|
interface UnistPlugin {
|
|
7
6
|
src?: string;
|
|
8
7
|
options?: Record<string, any>;
|
|
9
8
|
}
|
|
10
9
|
interface ModuleOptions {
|
|
10
|
+
/**
|
|
11
|
+
* A map of remark plugins to be used for processing markdown.
|
|
12
|
+
*/
|
|
11
13
|
remarkPlugins?: Record<string, UnistPlugin>;
|
|
14
|
+
/**
|
|
15
|
+
* A map of remark plugins to be used for processing markdown.
|
|
16
|
+
*/
|
|
12
17
|
rehypePlugins?: Record<string, UnistPlugin>;
|
|
13
18
|
highlight?: {
|
|
14
|
-
|
|
19
|
+
/**
|
|
20
|
+
* The highlighter to be used for highlighting code blocks.
|
|
21
|
+
*
|
|
22
|
+
* Set to `custom` to provide your own highlighter function in `mdc.config.ts`.
|
|
23
|
+
* Set to `shiki` to use the builtin highlighter based on Shiki.
|
|
24
|
+
* Or provide the path to your own highlighter module with the default export.
|
|
25
|
+
*
|
|
26
|
+
* @default 'shiki'
|
|
27
|
+
*/
|
|
28
|
+
highlighter?: 'shiki' | 'custom' | string;
|
|
15
29
|
/**
|
|
16
30
|
* Default theme that will be used for highlighting code blocks.
|
|
17
31
|
*/
|
|
18
|
-
theme?:
|
|
32
|
+
theme?: MdcThemeOptions;
|
|
33
|
+
/**
|
|
34
|
+
* Languages to be bundled loaded by Shiki
|
|
35
|
+
*
|
|
36
|
+
* All languages used has to be included in this list at build time, to create granular bundles.
|
|
37
|
+
*
|
|
38
|
+
* Unlike the `preload` option, when this option is provided, it will override the default languages.
|
|
39
|
+
*
|
|
40
|
+
* @default ['js','jsx','json','ts','tsx','vue','css','html','vue','bash','md','mdc','yaml']
|
|
41
|
+
*/
|
|
42
|
+
langs?: (BundledLanguage | LanguageRegistration)[];
|
|
43
|
+
/**
|
|
44
|
+
* Additional themes to be bundled loaded by Shiki
|
|
45
|
+
*/
|
|
46
|
+
themes?: (BundledTheme | ThemeRegistrationAny)[];
|
|
19
47
|
/**
|
|
20
48
|
* Preloaded languages that will be available for highlighting code blocks.
|
|
49
|
+
*
|
|
50
|
+
* @deprecated use `langs` instead.
|
|
21
51
|
*/
|
|
22
52
|
preload?: string[];
|
|
23
53
|
/**
|
|
@@ -38,17 +68,12 @@ interface ModuleOptions {
|
|
|
38
68
|
};
|
|
39
69
|
}
|
|
40
70
|
|
|
71
|
+
declare const DefaultHighlightLangs: BundledLanguage[];
|
|
41
72
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
42
73
|
|
|
43
74
|
declare module '@nuxt/schema' {
|
|
44
|
-
interface
|
|
45
|
-
mdc:
|
|
46
|
-
highlight: {
|
|
47
|
-
theme?: Theme;
|
|
48
|
-
preload?: string[];
|
|
49
|
-
wrapperStyle?: boolean | string;
|
|
50
|
-
};
|
|
51
|
-
};
|
|
75
|
+
interface NuxtHooks {
|
|
76
|
+
'mdc:configSources': (configs: string[]) => void;
|
|
52
77
|
}
|
|
53
78
|
interface PublicRuntimeConfig {
|
|
54
79
|
mdc: {
|
|
@@ -74,4 +99,4 @@ declare module '@nuxt/schema' {
|
|
|
74
99
|
}
|
|
75
100
|
}
|
|
76
101
|
|
|
77
|
-
export { _default as default };
|
|
102
|
+
export { DefaultHighlightLangs, _default as default };
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,52 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import { pascalCase } from 'scule';
|
|
1
|
+
import { extendViteConfig, defineNuxtModule, createResolver, useNitro, addServerHandler, addComponent, addImports, addServerImports, addComponentsDir, addTemplate } from '@nuxt/kit';
|
|
2
|
+
import fs$1, { existsSync } from 'fs';
|
|
4
3
|
import { defu } from 'defu';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const resolver = createResolver(import.meta.url);
|
|
9
|
-
const imports = [];
|
|
10
|
-
const { imports: remarkImports, definitions: remarkDefinitions } = processUnistPlugins(options.remarkPlugins);
|
|
11
|
-
const { imports: rehypeImports, definitions: rehypeDefinitions } = processUnistPlugins(options.rehypePlugins);
|
|
12
|
-
let highlighter = "false";
|
|
13
|
-
if (options.highlight) {
|
|
14
|
-
highlighter = JSON.stringify(options.highlight);
|
|
15
|
-
if (options.highlight.highlighter) {
|
|
16
|
-
const path = await resolver.resolvePath(options.highlight.highlighter, { alias: nuxt.options.alias });
|
|
17
|
-
imports.push(`import syntaxHighlighter from '${path}'`);
|
|
18
|
-
highlighter = highlighter.replace(`"${options.highlight.highlighter}"`, "syntaxHighlighter");
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return [
|
|
22
|
-
...remarkImports,
|
|
23
|
-
...rehypeImports,
|
|
24
|
-
...imports,
|
|
25
|
-
"",
|
|
26
|
-
"export const remarkPlugins = {",
|
|
27
|
-
...remarkDefinitions,
|
|
28
|
-
"}",
|
|
29
|
-
"",
|
|
30
|
-
"export const rehypePlugins = {",
|
|
31
|
-
...rehypeDefinitions,
|
|
32
|
-
"}",
|
|
33
|
-
"",
|
|
34
|
-
`export const highlight = ${highlighter}`
|
|
35
|
-
].join("\n");
|
|
36
|
-
};
|
|
37
|
-
function processUnistPlugins(plugins) {
|
|
38
|
-
const imports = [];
|
|
39
|
-
const definitions = [];
|
|
40
|
-
Object.entries(plugins).forEach(([name, plugin]) => {
|
|
41
|
-
imports.push(`import ${pascalCase(name)} from '${plugin.src || name}'`);
|
|
42
|
-
if (Object.keys(plugin).length) {
|
|
43
|
-
definitions.push(` '${name}': { instance: ${pascalCase(name)}, options: ${JSON.stringify(plugin.options || plugin)} },`);
|
|
44
|
-
} else {
|
|
45
|
-
definitions.push(` '${name}': { instance: ${pascalCase(name)} },`);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
return { imports, definitions };
|
|
49
|
-
}
|
|
4
|
+
import { resolve } from 'pathe';
|
|
5
|
+
import fs from 'fs/promises';
|
|
6
|
+
import { pascalCase } from 'scule';
|
|
50
7
|
|
|
51
8
|
const registerMDCSlotTransformer = (resolver) => {
|
|
52
9
|
extendViteConfig((config) => {
|
|
@@ -81,6 +38,150 @@ const registerMDCSlotTransformer = (resolver) => {
|
|
|
81
38
|
});
|
|
82
39
|
};
|
|
83
40
|
|
|
41
|
+
async function mdcConfigs({ options }) {
|
|
42
|
+
return [
|
|
43
|
+
"let configs",
|
|
44
|
+
"export function getMdcConfigs () {",
|
|
45
|
+
"if (!configs) {",
|
|
46
|
+
" configs = Promise.all([",
|
|
47
|
+
...options.configs.map((item) => ` import('${item}').then(m => m.default),`),
|
|
48
|
+
" ])",
|
|
49
|
+
"}",
|
|
50
|
+
"return configs",
|
|
51
|
+
"}"
|
|
52
|
+
].join("\n");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function mdcHighlighter({
|
|
56
|
+
options: {
|
|
57
|
+
shikiPath,
|
|
58
|
+
options,
|
|
59
|
+
useWasmAssets
|
|
60
|
+
}
|
|
61
|
+
}) {
|
|
62
|
+
if (!options || !options.highlighter)
|
|
63
|
+
return "export default () => { throw new Error('[@nuxtjs/mdc] No highlighter specified') }";
|
|
64
|
+
if (options.highlighter === "shiki") {
|
|
65
|
+
const file = [
|
|
66
|
+
shikiPath,
|
|
67
|
+
shikiPath + ".mjs"
|
|
68
|
+
].find((file2) => existsSync(file2));
|
|
69
|
+
if (!file)
|
|
70
|
+
throw new Error(`[@nuxtjs/mdc] Could not find shiki highlighter: ${shikiPath}`);
|
|
71
|
+
let code = await fs.readFile(file, "utf-8");
|
|
72
|
+
if (useWasmAssets) {
|
|
73
|
+
code = code.replace(
|
|
74
|
+
/import\((['"])shiki\/wasm\1\)/,
|
|
75
|
+
// We can remove the .client condition once Vite supports WASM ESM import
|
|
76
|
+
"import.meta.client ? import('shiki/wasm') : import('shiki/onig.wasm')"
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const { bundledLanguagesInfo } = await import('shiki/langs');
|
|
80
|
+
const langsMap = /* @__PURE__ */ new Map();
|
|
81
|
+
options.langs?.forEach((lang) => {
|
|
82
|
+
if (typeof lang === "string") {
|
|
83
|
+
const info = bundledLanguagesInfo.find((i) => i.aliases?.includes?.(lang) || i.id === lang);
|
|
84
|
+
if (!info) {
|
|
85
|
+
throw new Error(`[@nuxtjs/mdc] Could not find shiki language: ${lang}`);
|
|
86
|
+
}
|
|
87
|
+
langsMap.set(info.id, info.id);
|
|
88
|
+
for (const alias of info.aliases || []) {
|
|
89
|
+
langsMap.set(alias, info.id);
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
langsMap.set(lang.name, lang);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
const themes = Array.from(/* @__PURE__ */ new Set([
|
|
96
|
+
...typeof options?.theme === "string" ? [options?.theme] : Object.values(options?.theme || {}),
|
|
97
|
+
...options?.themes || []
|
|
98
|
+
]));
|
|
99
|
+
return [
|
|
100
|
+
"import { getMdcConfigs } from '#mdc-configs'",
|
|
101
|
+
code,
|
|
102
|
+
"const bundledLangs = {",
|
|
103
|
+
...Array.from(langsMap.entries()).map(([name, lang]) => typeof lang === "string" ? JSON.stringify(name) + `: () => import('shiki/langs/${lang}.mjs'),` : JSON.stringify(name) + ": " + JSON.stringify(lang) + ","),
|
|
104
|
+
"}",
|
|
105
|
+
"const bundledThemes = {",
|
|
106
|
+
...themes.map((theme) => typeof theme === "string" ? JSON.stringify(theme) + `: () => import('shiki/themes/${theme}.mjs').then(r => r.default),` : JSON.stringify(theme.name) + ": " + JSON.stringify(theme) + ","),
|
|
107
|
+
"}",
|
|
108
|
+
"const options = " + JSON.stringify({
|
|
109
|
+
theme: options.theme,
|
|
110
|
+
wrapperStyle: options.wrapperStyle
|
|
111
|
+
}),
|
|
112
|
+
"const highlighter = createShikiHighlighter({ bundledLangs, bundledThemes, options, getMdcConfigs })",
|
|
113
|
+
"export default highlighter"
|
|
114
|
+
].join("\n");
|
|
115
|
+
}
|
|
116
|
+
if (options.highlighter === "custom") {
|
|
117
|
+
return [
|
|
118
|
+
"import { getMdcConfigs } from '#mdc-configs'",
|
|
119
|
+
`export default function (...args) {
|
|
120
|
+
' const configs = await getMdcConfigs()`,
|
|
121
|
+
" for (const config of configs) {",
|
|
122
|
+
" if (config.highlighter) {",
|
|
123
|
+
" return config.highlighter(...args)",
|
|
124
|
+
" }",
|
|
125
|
+
" }",
|
|
126
|
+
" throw new Error('[@nuxtjs/mdc] No custom highlighter specified')",
|
|
127
|
+
"}"
|
|
128
|
+
].join("\n");
|
|
129
|
+
}
|
|
130
|
+
return "export { default } from " + JSON.stringify(options.highlighter);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function mdcImports({ options }) {
|
|
134
|
+
const imports = [];
|
|
135
|
+
const { imports: remarkImports, definitions: remarkDefinitions } = processUnistPlugins(options.remarkPlugins);
|
|
136
|
+
const { imports: rehypeImports, definitions: rehypeDefinitions } = processUnistPlugins(options.rehypePlugins);
|
|
137
|
+
return [
|
|
138
|
+
...remarkImports,
|
|
139
|
+
...rehypeImports,
|
|
140
|
+
...imports,
|
|
141
|
+
"",
|
|
142
|
+
"export const remarkPlugins = {",
|
|
143
|
+
...remarkDefinitions,
|
|
144
|
+
"}",
|
|
145
|
+
"",
|
|
146
|
+
"export const rehypePlugins = {",
|
|
147
|
+
...rehypeDefinitions,
|
|
148
|
+
"}",
|
|
149
|
+
"",
|
|
150
|
+
`export const highlight = ${JSON.stringify({
|
|
151
|
+
theme: options.highlight?.theme,
|
|
152
|
+
wrapperStyle: options.highlight?.wrapperStyle
|
|
153
|
+
})}`
|
|
154
|
+
].join("\n");
|
|
155
|
+
}
|
|
156
|
+
function processUnistPlugins(plugins) {
|
|
157
|
+
const imports = [];
|
|
158
|
+
const definitions = [];
|
|
159
|
+
Object.entries(plugins).forEach(([name, plugin]) => {
|
|
160
|
+
imports.push(`import ${pascalCase(name)} from '${plugin.src || name}'`);
|
|
161
|
+
if (Object.keys(plugin).length) {
|
|
162
|
+
definitions.push(` '${name}': { instance: ${pascalCase(name)}, options: ${JSON.stringify(plugin.options || plugin)} },`);
|
|
163
|
+
} else {
|
|
164
|
+
definitions.push(` '${name}': { instance: ${pascalCase(name)} },`);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
return { imports, definitions };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const DefaultHighlightLangs = [
|
|
171
|
+
"js",
|
|
172
|
+
"jsx",
|
|
173
|
+
"json",
|
|
174
|
+
"ts",
|
|
175
|
+
"tsx",
|
|
176
|
+
"vue",
|
|
177
|
+
"css",
|
|
178
|
+
"html",
|
|
179
|
+
"vue",
|
|
180
|
+
"bash",
|
|
181
|
+
"md",
|
|
182
|
+
"mdc",
|
|
183
|
+
"yaml"
|
|
184
|
+
];
|
|
84
185
|
const module = defineNuxtModule({
|
|
85
186
|
meta: {
|
|
86
187
|
name: "@nuxtjs/mdc",
|
|
@@ -107,6 +208,7 @@ const module = defineNuxtModule({
|
|
|
107
208
|
}
|
|
108
209
|
},
|
|
109
210
|
async setup(options, nuxt) {
|
|
211
|
+
resolveOptions(options);
|
|
110
212
|
const resolver = createResolver(import.meta.url);
|
|
111
213
|
nuxt.options.runtimeConfig.public.mdc = defu(nuxt.options.runtimeConfig.public.mdc, {
|
|
112
214
|
components: {
|
|
@@ -115,24 +217,6 @@ const module = defineNuxtModule({
|
|
|
115
217
|
},
|
|
116
218
|
headings: options.headings
|
|
117
219
|
});
|
|
118
|
-
nuxt.options.runtimeConfig.mdc = defu(nuxt.options.runtimeConfig.mdc, {
|
|
119
|
-
highlight: options.highlight ? {
|
|
120
|
-
theme: options.highlight.theme,
|
|
121
|
-
preload: options.highlight.preload,
|
|
122
|
-
wrapperStyle: options.highlight.wrapperStyle
|
|
123
|
-
} : {}
|
|
124
|
-
});
|
|
125
|
-
nuxt.hook("vite:extendConfig", (viteConfig) => {
|
|
126
|
-
const optimizeList = ["debug", "flat", "node-emoji", "extend", "hast-util-raw"];
|
|
127
|
-
viteConfig.optimizeDeps ||= {};
|
|
128
|
-
viteConfig.optimizeDeps.include ||= [];
|
|
129
|
-
const list = viteConfig.optimizeDeps.include;
|
|
130
|
-
optimizeList.forEach((pkg) => {
|
|
131
|
-
if (!list.includes(pkg)) {
|
|
132
|
-
list.push(pkg);
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
220
|
if (options.highlight) {
|
|
137
221
|
nuxt.hook("ready", () => {
|
|
138
222
|
const nitro = useNitro();
|
|
@@ -156,15 +240,65 @@ const module = defineNuxtModule({
|
|
|
156
240
|
addWasmSupport(prerenderer);
|
|
157
241
|
});
|
|
158
242
|
});
|
|
159
|
-
addServerHandler({
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
243
|
+
addServerHandler({
|
|
244
|
+
route: "/api/_mdc/highlight",
|
|
245
|
+
handler: resolver.resolve("./runtime/highlighter/event-handler")
|
|
246
|
+
});
|
|
247
|
+
options.rehypePlugins ||= {};
|
|
248
|
+
options.rehypePlugins.highlight ||= {};
|
|
249
|
+
options.rehypePlugins.highlight.src ||= await resolver.resolvePath("./runtime/highlighter/rehype");
|
|
250
|
+
options.rehypePlugins.highlight.options ||= {};
|
|
163
251
|
}
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
252
|
+
const registerTemplate = (options2) => {
|
|
253
|
+
const name = options2.filename.replace(/\.m?js$/, "");
|
|
254
|
+
const alias = "#" + name;
|
|
255
|
+
const results = addTemplate({
|
|
256
|
+
...options2,
|
|
257
|
+
write: true
|
|
258
|
+
// Write to disk for Nitro to consume
|
|
259
|
+
});
|
|
260
|
+
nuxt.options.nitro.alias ||= {};
|
|
261
|
+
nuxt.options.nitro.externals ||= {};
|
|
262
|
+
nuxt.options.nitro.externals.inline ||= [];
|
|
263
|
+
nuxt.options.alias[alias] = results.dst;
|
|
264
|
+
nuxt.options.nitro.alias[alias] = nuxt.options.alias[alias];
|
|
265
|
+
nuxt.options.nitro.externals.inline.push(nuxt.options.alias[alias]);
|
|
266
|
+
nuxt.options.nitro.externals.inline.push(alias);
|
|
267
|
+
return results;
|
|
268
|
+
};
|
|
269
|
+
const mdcConfigs$1 = [];
|
|
270
|
+
for (const layer of nuxt.options._layers) {
|
|
271
|
+
let path = resolve(layer.config.srcDir, "mdc.config.ts");
|
|
272
|
+
if (fs$1.existsSync(path)) {
|
|
273
|
+
mdcConfigs$1.push(path);
|
|
274
|
+
} else {
|
|
275
|
+
path = resolve(layer.config.srcDir, "mdc.config.js");
|
|
276
|
+
if (fs$1.existsSync(path)) {
|
|
277
|
+
mdcConfigs$1.push(path);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
await nuxt.callHook("mdc:configSources", mdcConfigs$1);
|
|
282
|
+
registerTemplate({
|
|
283
|
+
filename: "mdc-configs.mjs",
|
|
284
|
+
getContents: mdcConfigs,
|
|
285
|
+
options: { configs: mdcConfigs$1 }
|
|
286
|
+
});
|
|
287
|
+
registerTemplate({
|
|
288
|
+
filename: "mdc-highlighter.mjs",
|
|
289
|
+
getContents: mdcHighlighter,
|
|
290
|
+
options: {
|
|
291
|
+
shikiPath: resolver.resolve("../dist/runtime/highlighter/shiki"),
|
|
292
|
+
options: options.highlight,
|
|
293
|
+
// When WASM support enabled in Nitro, we could use the .wasm file directly for Cloudflare Workers
|
|
294
|
+
useWasmAssets: !nuxt.options.dev && !!nuxt.options.nitro.experimental?.wasm
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
registerTemplate({
|
|
298
|
+
filename: "mdc-imports.mjs",
|
|
299
|
+
getContents: mdcImports,
|
|
300
|
+
options
|
|
301
|
+
});
|
|
168
302
|
addComponent({ name: "MDC", filePath: resolver.resolve("./runtime/components/MDC") });
|
|
169
303
|
addComponent({ name: "MDCRenderer", filePath: resolver.resolve("./runtime/components/MDCRenderer") });
|
|
170
304
|
addComponent({ name: "MDCSlot", filePath: resolver.resolve("./runtime/components/MDCSlot") });
|
|
@@ -180,15 +314,49 @@ const module = defineNuxtModule({
|
|
|
180
314
|
});
|
|
181
315
|
}
|
|
182
316
|
extendViteConfig((config) => {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
317
|
+
const include = [
|
|
318
|
+
"remark-gfm",
|
|
319
|
+
// from runtime/parser/index.ts
|
|
320
|
+
"remark-emoji",
|
|
321
|
+
// from runtime/parser/index.ts
|
|
322
|
+
"remark-mdc",
|
|
323
|
+
// from runtime/parser/index.ts
|
|
324
|
+
"remark-rehype",
|
|
325
|
+
// from runtime/parser/index.ts
|
|
326
|
+
"rehype-raw",
|
|
327
|
+
// from runtime/parser/index.ts
|
|
328
|
+
"parse5",
|
|
329
|
+
// transitive deps of rehype
|
|
330
|
+
"unist-util-visit",
|
|
331
|
+
// from runtime/highlighter/rehype.ts
|
|
332
|
+
"unified",
|
|
333
|
+
// deps by all the plugins
|
|
334
|
+
"debug"
|
|
335
|
+
// deps by many libraries but it's not an ESM
|
|
336
|
+
];
|
|
337
|
+
const exclude = [
|
|
338
|
+
"@nuxtjs/mdc"
|
|
339
|
+
// package itself, it's a build time module
|
|
340
|
+
];
|
|
341
|
+
config.optimizeDeps ||= {};
|
|
342
|
+
config.optimizeDeps.exclude ||= [];
|
|
343
|
+
config.optimizeDeps.include ||= [];
|
|
344
|
+
for (const pkg of include) {
|
|
345
|
+
if (!config.optimizeDeps.include.includes(pkg)) {
|
|
346
|
+
config.optimizeDeps.include.push(pkg);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
for (const pkg of exclude) {
|
|
350
|
+
if (!config.optimizeDeps.exclude.includes(pkg)) {
|
|
351
|
+
config.optimizeDeps.exclude.push(pkg);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
186
354
|
});
|
|
187
355
|
const _layers = [...nuxt.options._layers].reverse();
|
|
188
356
|
for (const layer of _layers) {
|
|
189
357
|
const srcDir = layer.config.srcDir;
|
|
190
358
|
const globalComponents = resolver.resolve(srcDir, "components/mdc");
|
|
191
|
-
const dirStat = await fs.promises.stat(globalComponents).catch(() => null);
|
|
359
|
+
const dirStat = await fs$1.promises.stat(globalComponents).catch(() => null);
|
|
192
360
|
if (dirStat && dirStat.isDirectory()) {
|
|
193
361
|
nuxt.hook("components:dirs", (dirs) => {
|
|
194
362
|
dirs.unshift({
|
|
@@ -203,5 +371,19 @@ const module = defineNuxtModule({
|
|
|
203
371
|
registerMDCSlotTransformer(resolver);
|
|
204
372
|
}
|
|
205
373
|
});
|
|
374
|
+
function resolveOptions(options) {
|
|
375
|
+
if (options.highlight !== false) {
|
|
376
|
+
options.highlight ||= {};
|
|
377
|
+
options.highlight.highlighter ||= "shiki";
|
|
378
|
+
options.highlight.theme ||= {
|
|
379
|
+
default: "github-light",
|
|
380
|
+
dark: "github-dark"
|
|
381
|
+
};
|
|
382
|
+
options.highlight.langs ||= DefaultHighlightLangs;
|
|
383
|
+
if (options.highlight.preload) {
|
|
384
|
+
options.highlight.langs.push(...options.highlight.preload || []);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
206
388
|
|
|
207
|
-
export { module as default };
|
|
389
|
+
export { DefaultHighlightLangs, module as default };
|
|
@@ -72,8 +72,8 @@ export default defineComponent({
|
|
|
72
72
|
}
|
|
73
73
|
const meta = { ...data, tags };
|
|
74
74
|
const component = tag !== false ? resolveVueComponent(tag || meta.component?.name || meta.component || "div") : void 0;
|
|
75
|
-
const
|
|
76
|
-
return component ? h(component, { ...meta.component?.props, ...this.$attrs, key: contentKey },
|
|
75
|
+
const childrenRenderer = renderSlots(body, h, meta, meta);
|
|
76
|
+
return component ? h(component, { ...meta.component?.props, ...this.$attrs, key: contentKey }, childrenRenderer) : childrenRenderer.default?.();
|
|
77
77
|
}
|
|
78
78
|
});
|
|
79
79
|
function renderNode(node, h2, documentMeta, parentScope = {}) {
|