@nuxtjs/mdc 0.3.2 → 0.4.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 +31 -13
- package/dist/module.d.ts +31 -13
- package/dist/module.json +1 -1
- package/dist/module.mjs +243 -78
- 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/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,47 @@
|
|
|
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
|
+
langs?: (BundledLanguage | LanguageRegistration)[];
|
|
37
|
+
/**
|
|
38
|
+
* Additional themes to be bundled loaded by Shiki
|
|
39
|
+
*/
|
|
40
|
+
themes?: (BundledTheme | ThemeRegistrationAny)[];
|
|
19
41
|
/**
|
|
20
42
|
* Preloaded languages that will be available for highlighting code blocks.
|
|
43
|
+
*
|
|
44
|
+
* @deprecated use `langs` instead.
|
|
21
45
|
*/
|
|
22
46
|
preload?: string[];
|
|
23
47
|
/**
|
|
@@ -41,14 +65,8 @@ interface ModuleOptions {
|
|
|
41
65
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
42
66
|
|
|
43
67
|
declare module '@nuxt/schema' {
|
|
44
|
-
interface
|
|
45
|
-
mdc:
|
|
46
|
-
highlight: {
|
|
47
|
-
theme?: Theme;
|
|
48
|
-
preload?: string[];
|
|
49
|
-
wrapperStyle?: boolean | string;
|
|
50
|
-
};
|
|
51
|
-
};
|
|
68
|
+
interface NuxtHooks {
|
|
69
|
+
'mdc:configSources': (configs: string[]) => void;
|
|
52
70
|
}
|
|
53
71
|
interface PublicRuntimeConfig {
|
|
54
72
|
mdc: {
|
package/dist/module.d.ts
CHANGED
|
@@ -1,23 +1,47 @@
|
|
|
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
|
+
langs?: (BundledLanguage | LanguageRegistration)[];
|
|
37
|
+
/**
|
|
38
|
+
* Additional themes to be bundled loaded by Shiki
|
|
39
|
+
*/
|
|
40
|
+
themes?: (BundledTheme | ThemeRegistrationAny)[];
|
|
19
41
|
/**
|
|
20
42
|
* Preloaded languages that will be available for highlighting code blocks.
|
|
43
|
+
*
|
|
44
|
+
* @deprecated use `langs` instead.
|
|
21
45
|
*/
|
|
22
46
|
preload?: string[];
|
|
23
47
|
/**
|
|
@@ -41,14 +65,8 @@ interface ModuleOptions {
|
|
|
41
65
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
42
66
|
|
|
43
67
|
declare module '@nuxt/schema' {
|
|
44
|
-
interface
|
|
45
|
-
mdc:
|
|
46
|
-
highlight: {
|
|
47
|
-
theme?: Theme;
|
|
48
|
-
preload?: string[];
|
|
49
|
-
wrapperStyle?: boolean | string;
|
|
50
|
-
};
|
|
51
|
-
};
|
|
68
|
+
interface NuxtHooks {
|
|
69
|
+
'mdc:configSources': (configs: string[]) => void;
|
|
52
70
|
}
|
|
53
71
|
interface PublicRuntimeConfig {
|
|
54
72
|
mdc: {
|
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,127 @@ 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
|
+
}
|
|
60
|
+
}) {
|
|
61
|
+
if (!options || !options.highlighter)
|
|
62
|
+
return "export default () => { throw new Error('[@nuxtjs/mdc] No highlighter specified') }";
|
|
63
|
+
if (options.highlighter === "shiki") {
|
|
64
|
+
const file = [
|
|
65
|
+
shikiPath,
|
|
66
|
+
shikiPath + ".mjs"
|
|
67
|
+
].find((file2) => existsSync(file2));
|
|
68
|
+
if (!file)
|
|
69
|
+
throw new Error(`[@nuxtjs/mdc] Could not find shiki highlighter: ${shikiPath}`);
|
|
70
|
+
const code = await fs.readFile(file, "utf-8");
|
|
71
|
+
const { bundledLanguagesInfo } = await import('shiki/langs');
|
|
72
|
+
const langsMap = /* @__PURE__ */ new Map();
|
|
73
|
+
options.langs?.forEach((lang) => {
|
|
74
|
+
if (typeof lang === "string") {
|
|
75
|
+
const info = bundledLanguagesInfo.find((i) => i.aliases?.includes?.(lang) || i.id === lang);
|
|
76
|
+
if (!info) {
|
|
77
|
+
throw new Error(`[@nuxtjs/mdc] Could not find shiki language: ${lang}`);
|
|
78
|
+
}
|
|
79
|
+
langsMap.set(info.id, info.id);
|
|
80
|
+
for (const alias of info.aliases || []) {
|
|
81
|
+
langsMap.set(alias, info.id);
|
|
82
|
+
}
|
|
83
|
+
} else {
|
|
84
|
+
langsMap.set(lang.name, lang);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
const themes = Array.from(/* @__PURE__ */ new Set([
|
|
88
|
+
...typeof options?.theme === "string" ? [options?.theme] : Object.values(options?.theme || {}),
|
|
89
|
+
...options?.themes || []
|
|
90
|
+
]));
|
|
91
|
+
return [
|
|
92
|
+
"import { getMdcConfigs } from '#mdc-configs'",
|
|
93
|
+
code,
|
|
94
|
+
"const bundledLangs = {",
|
|
95
|
+
...Array.from(langsMap.entries()).map(([name, lang]) => typeof lang === "string" ? JSON.stringify(name) + `: () => import('shiki/langs/${lang}.mjs'),` : JSON.stringify(name) + ": " + JSON.stringify(lang) + ","),
|
|
96
|
+
"}",
|
|
97
|
+
"const bundledThemes = {",
|
|
98
|
+
...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) + ","),
|
|
99
|
+
"}",
|
|
100
|
+
"const options = " + JSON.stringify({
|
|
101
|
+
theme: options.theme,
|
|
102
|
+
wrapperStyle: options.wrapperStyle
|
|
103
|
+
}),
|
|
104
|
+
"const highlighter = createShikiHighlighter({ bundledLangs, bundledThemes, options, getMdcConfigs })",
|
|
105
|
+
"export default highlighter"
|
|
106
|
+
].join("\n");
|
|
107
|
+
}
|
|
108
|
+
if (options.highlighter === "custom") {
|
|
109
|
+
return [
|
|
110
|
+
"import { getMdcConfigs } from '#mdc-configs'",
|
|
111
|
+
`export default function (...args) {
|
|
112
|
+
' const configs = await getMdcConfigs()`,
|
|
113
|
+
" for (const config of configs) {",
|
|
114
|
+
" if (config.highlighter) {",
|
|
115
|
+
" return config.highlighter(...args)",
|
|
116
|
+
" }",
|
|
117
|
+
" }",
|
|
118
|
+
" throw new Error('[@nuxtjs/mdc] No custom highlighter specified')",
|
|
119
|
+
"}"
|
|
120
|
+
].join("\n");
|
|
121
|
+
}
|
|
122
|
+
return "export { default } from " + JSON.stringify(options.highlighter);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function mdcImports({ options }) {
|
|
126
|
+
const imports = [];
|
|
127
|
+
const { imports: remarkImports, definitions: remarkDefinitions } = processUnistPlugins(options.remarkPlugins);
|
|
128
|
+
const { imports: rehypeImports, definitions: rehypeDefinitions } = processUnistPlugins(options.rehypePlugins);
|
|
129
|
+
return [
|
|
130
|
+
...remarkImports,
|
|
131
|
+
...rehypeImports,
|
|
132
|
+
...imports,
|
|
133
|
+
"",
|
|
134
|
+
"export const remarkPlugins = {",
|
|
135
|
+
...remarkDefinitions,
|
|
136
|
+
"}",
|
|
137
|
+
"",
|
|
138
|
+
"export const rehypePlugins = {",
|
|
139
|
+
...rehypeDefinitions,
|
|
140
|
+
"}",
|
|
141
|
+
"",
|
|
142
|
+
`export const highlight = ${JSON.stringify({
|
|
143
|
+
theme: options.highlight?.theme,
|
|
144
|
+
wrapperStyle: options.highlight?.wrapperStyle
|
|
145
|
+
})}`
|
|
146
|
+
].join("\n");
|
|
147
|
+
}
|
|
148
|
+
function processUnistPlugins(plugins) {
|
|
149
|
+
const imports = [];
|
|
150
|
+
const definitions = [];
|
|
151
|
+
Object.entries(plugins).forEach(([name, plugin]) => {
|
|
152
|
+
imports.push(`import ${pascalCase(name)} from '${plugin.src || name}'`);
|
|
153
|
+
if (Object.keys(plugin).length) {
|
|
154
|
+
definitions.push(` '${name}': { instance: ${pascalCase(name)}, options: ${JSON.stringify(plugin.options || plugin)} },`);
|
|
155
|
+
} else {
|
|
156
|
+
definitions.push(` '${name}': { instance: ${pascalCase(name)} },`);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
return { imports, definitions };
|
|
160
|
+
}
|
|
161
|
+
|
|
84
162
|
const module = defineNuxtModule({
|
|
85
163
|
meta: {
|
|
86
164
|
name: "@nuxtjs/mdc",
|
|
@@ -107,6 +185,7 @@ const module = defineNuxtModule({
|
|
|
107
185
|
}
|
|
108
186
|
},
|
|
109
187
|
async setup(options, nuxt) {
|
|
188
|
+
resolveOptions(options);
|
|
110
189
|
const resolver = createResolver(import.meta.url);
|
|
111
190
|
nuxt.options.runtimeConfig.public.mdc = defu(nuxt.options.runtimeConfig.public.mdc, {
|
|
112
191
|
components: {
|
|
@@ -115,24 +194,6 @@ const module = defineNuxtModule({
|
|
|
115
194
|
},
|
|
116
195
|
headings: options.headings
|
|
117
196
|
});
|
|
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
197
|
if (options.highlight) {
|
|
137
198
|
nuxt.hook("ready", () => {
|
|
138
199
|
const nitro = useNitro();
|
|
@@ -156,15 +217,63 @@ const module = defineNuxtModule({
|
|
|
156
217
|
addWasmSupport(prerenderer);
|
|
157
218
|
});
|
|
158
219
|
});
|
|
159
|
-
addServerHandler({
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
220
|
+
addServerHandler({
|
|
221
|
+
route: "/api/_mdc/highlight",
|
|
222
|
+
handler: resolver.resolve("./runtime/highlighter/event-handler")
|
|
223
|
+
});
|
|
224
|
+
options.rehypePlugins ||= {};
|
|
225
|
+
options.rehypePlugins.highlight ||= {};
|
|
226
|
+
options.rehypePlugins.highlight.src ||= await resolver.resolvePath("./runtime/highlighter/rehype");
|
|
227
|
+
options.rehypePlugins.highlight.options ||= {};
|
|
228
|
+
}
|
|
229
|
+
const registerTemplate = (options2) => {
|
|
230
|
+
const name = options2.filename.replace(/\.m?js$/, "");
|
|
231
|
+
const alias = "#" + name;
|
|
232
|
+
const results = addTemplate({
|
|
233
|
+
...options2,
|
|
234
|
+
write: true
|
|
235
|
+
// Write to disk for Nitro to consume
|
|
236
|
+
});
|
|
237
|
+
nuxt.options.nitro.alias ||= {};
|
|
238
|
+
nuxt.options.nitro.externals ||= {};
|
|
239
|
+
nuxt.options.nitro.externals.inline ||= [];
|
|
240
|
+
nuxt.options.alias[alias] = results.dst;
|
|
241
|
+
nuxt.options.nitro.alias[alias] = nuxt.options.alias[alias];
|
|
242
|
+
nuxt.options.nitro.externals.inline.push(nuxt.options.alias[alias]);
|
|
243
|
+
nuxt.options.nitro.externals.inline.push(alias);
|
|
244
|
+
return results;
|
|
245
|
+
};
|
|
246
|
+
const mdcConfigs$1 = [];
|
|
247
|
+
for (const layer of nuxt.options._layers) {
|
|
248
|
+
let path = resolve(layer.config.srcDir, "mdc.config.ts");
|
|
249
|
+
if (fs$1.existsSync(path)) {
|
|
250
|
+
mdcConfigs$1.push(path);
|
|
251
|
+
} else {
|
|
252
|
+
path = resolve(layer.config.srcDir, "mdc.config.js");
|
|
253
|
+
if (fs$1.existsSync(path)) {
|
|
254
|
+
mdcConfigs$1.push(path);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
163
257
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
258
|
+
await nuxt.callHook("mdc:configSources", mdcConfigs$1);
|
|
259
|
+
registerTemplate({
|
|
260
|
+
filename: "mdc-configs.mjs",
|
|
261
|
+
getContents: mdcConfigs,
|
|
262
|
+
options: { configs: mdcConfigs$1 }
|
|
263
|
+
});
|
|
264
|
+
registerTemplate({
|
|
265
|
+
filename: "mdc-highlighter.mjs",
|
|
266
|
+
getContents: mdcHighlighter,
|
|
267
|
+
options: {
|
|
268
|
+
shikiPath: resolver.resolve("../dist/runtime/highlighter/shiki"),
|
|
269
|
+
options: options.highlight
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
registerTemplate({
|
|
273
|
+
filename: "mdc-imports.mjs",
|
|
274
|
+
getContents: mdcImports,
|
|
275
|
+
options
|
|
276
|
+
});
|
|
168
277
|
addComponent({ name: "MDC", filePath: resolver.resolve("./runtime/components/MDC") });
|
|
169
278
|
addComponent({ name: "MDCRenderer", filePath: resolver.resolve("./runtime/components/MDCRenderer") });
|
|
170
279
|
addComponent({ name: "MDCSlot", filePath: resolver.resolve("./runtime/components/MDCSlot") });
|
|
@@ -180,15 +289,49 @@ const module = defineNuxtModule({
|
|
|
180
289
|
});
|
|
181
290
|
}
|
|
182
291
|
extendViteConfig((config) => {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
292
|
+
const include = [
|
|
293
|
+
"remark-gfm",
|
|
294
|
+
// from runtime/parser/index.ts
|
|
295
|
+
"remark-emoji",
|
|
296
|
+
// from runtime/parser/index.ts
|
|
297
|
+
"remark-mdc",
|
|
298
|
+
// from runtime/parser/index.ts
|
|
299
|
+
"remark-rehype",
|
|
300
|
+
// from runtime/parser/index.ts
|
|
301
|
+
"rehype-raw",
|
|
302
|
+
// from runtime/parser/index.ts
|
|
303
|
+
"parse5",
|
|
304
|
+
// transitive deps of rehype
|
|
305
|
+
"unist-util-visit",
|
|
306
|
+
// from runtime/highlighter/rehype.ts
|
|
307
|
+
"unified",
|
|
308
|
+
// deps by all the plugins
|
|
309
|
+
"debug"
|
|
310
|
+
// deps by many libraries but it's not an ESM
|
|
311
|
+
];
|
|
312
|
+
const exclude = [
|
|
313
|
+
"@nuxtjs/mdc"
|
|
314
|
+
// package itself, it's a build time module
|
|
315
|
+
];
|
|
316
|
+
config.optimizeDeps ||= {};
|
|
317
|
+
config.optimizeDeps.exclude ||= [];
|
|
318
|
+
config.optimizeDeps.include ||= [];
|
|
319
|
+
for (const pkg of include) {
|
|
320
|
+
if (!config.optimizeDeps.include.includes(pkg)) {
|
|
321
|
+
config.optimizeDeps.include.push(pkg);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
for (const pkg of exclude) {
|
|
325
|
+
if (!config.optimizeDeps.exclude.includes(pkg)) {
|
|
326
|
+
config.optimizeDeps.exclude.push(pkg);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
186
329
|
});
|
|
187
330
|
const _layers = [...nuxt.options._layers].reverse();
|
|
188
331
|
for (const layer of _layers) {
|
|
189
332
|
const srcDir = layer.config.srcDir;
|
|
190
333
|
const globalComponents = resolver.resolve(srcDir, "components/mdc");
|
|
191
|
-
const dirStat = await fs.promises.stat(globalComponents).catch(() => null);
|
|
334
|
+
const dirStat = await fs$1.promises.stat(globalComponents).catch(() => null);
|
|
192
335
|
if (dirStat && dirStat.isDirectory()) {
|
|
193
336
|
nuxt.hook("components:dirs", (dirs) => {
|
|
194
337
|
dirs.unshift({
|
|
@@ -203,5 +346,27 @@ const module = defineNuxtModule({
|
|
|
203
346
|
registerMDCSlotTransformer(resolver);
|
|
204
347
|
}
|
|
205
348
|
});
|
|
349
|
+
function resolveOptions(options) {
|
|
350
|
+
if (options.highlight !== false) {
|
|
351
|
+
options.highlight ||= {};
|
|
352
|
+
options.highlight.highlighter ||= "shiki";
|
|
353
|
+
options.highlight.theme ||= {
|
|
354
|
+
default: "github-light",
|
|
355
|
+
dark: "github-dark"
|
|
356
|
+
};
|
|
357
|
+
options.highlight.langs ||= [
|
|
358
|
+
"js",
|
|
359
|
+
"ts",
|
|
360
|
+
"vue",
|
|
361
|
+
"css",
|
|
362
|
+
"html",
|
|
363
|
+
"vue",
|
|
364
|
+
"shell"
|
|
365
|
+
];
|
|
366
|
+
if (options.highlight.preload) {
|
|
367
|
+
options.highlight.langs.push(...options.highlight.preload || []);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
206
371
|
|
|
207
372
|
export { 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 = {}) {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<
|
|
1
|
+
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
|
|
2
2
|
export default _default;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { eventHandler, getQuery } from "h3";
|
|
2
|
+
import highlighter from "#mdc-highlighter";
|
|
3
|
+
export default eventHandler(async (event) => {
|
|
4
|
+
const { code, lang, theme: themeString, options: optionsStr } = getQuery(event);
|
|
5
|
+
const theme = JSON.parse(themeString);
|
|
6
|
+
const options = optionsStr ? JSON.parse(optionsStr) : {};
|
|
7
|
+
return await highlighter(code, lang, theme, options);
|
|
8
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Root } from 'hast';
|
|
2
|
+
import type { Highlighter, MdcThemeOptions } from './types';
|
|
3
|
+
export interface RehypeHighlightOption {
|
|
4
|
+
theme?: MdcThemeOptions;
|
|
5
|
+
highlighter?: Highlighter;
|
|
6
|
+
}
|
|
7
|
+
export default rehypeHighlight;
|
|
8
|
+
export declare function rehypeHighlight(opts?: RehypeHighlightOption): (tree: Root) => Promise<void>;
|