@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
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { getHighlighter, addClassToHast } from "shikiji";
|
|
2
|
-
import {
|
|
3
|
-
transformerNotationDiff,
|
|
4
|
-
transformerNotationErrorLevel,
|
|
5
|
-
transformerNotationFocus,
|
|
6
|
-
transformerNotationHighlight
|
|
7
|
-
} from "shikiji-transformers";
|
|
8
|
-
export const useShikiHighlighter = createSingleton((opts) => {
|
|
9
|
-
const { theme, preload, wrapperStyle } = opts || {};
|
|
10
|
-
let promise;
|
|
11
|
-
const getShikiHighlighter = () => {
|
|
12
|
-
if (!promise) {
|
|
13
|
-
promise = getHighlighter({
|
|
14
|
-
themes: [
|
|
15
|
-
theme?.default || theme || "dark-plus"
|
|
16
|
-
],
|
|
17
|
-
langs: [
|
|
18
|
-
...preload || [],
|
|
19
|
-
"diff",
|
|
20
|
-
"json",
|
|
21
|
-
"js",
|
|
22
|
-
"ts",
|
|
23
|
-
"css",
|
|
24
|
-
"shell",
|
|
25
|
-
"html",
|
|
26
|
-
"md",
|
|
27
|
-
"yaml",
|
|
28
|
-
"vue",
|
|
29
|
-
"mdc"
|
|
30
|
-
]
|
|
31
|
-
}).then((highlighter) => {
|
|
32
|
-
const themes = Object.values(typeof theme === "string" ? { default: theme } : theme || {});
|
|
33
|
-
if (themes.length) {
|
|
34
|
-
return Promise.all(themes.map((theme2) => highlighter.loadTheme(theme2))).then(() => highlighter);
|
|
35
|
-
}
|
|
36
|
-
return highlighter;
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
return promise;
|
|
40
|
-
};
|
|
41
|
-
const transformers = [
|
|
42
|
-
transformerNotationDiff(),
|
|
43
|
-
transformerNotationFocus(),
|
|
44
|
-
transformerNotationHighlight(),
|
|
45
|
-
transformerNotationErrorLevel()
|
|
46
|
-
];
|
|
47
|
-
const getHighlightedAST = async (code, lang, theme2, opts2) => {
|
|
48
|
-
try {
|
|
49
|
-
const highlighter = await getShikiHighlighter();
|
|
50
|
-
const { highlights = [] } = opts2 || {};
|
|
51
|
-
const themesObject = typeof theme2 === "string" ? { default: theme2 } : theme2 || {};
|
|
52
|
-
const themeNames = Object.values(themesObject);
|
|
53
|
-
if (themeNames.length) {
|
|
54
|
-
await Promise.all(themeNames.map((theme3) => highlighter.loadTheme(theme3)));
|
|
55
|
-
}
|
|
56
|
-
if (lang && !highlighter.getLoadedLanguages().includes(lang)) {
|
|
57
|
-
try {
|
|
58
|
-
await highlighter.loadLanguage(lang);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
if (highlights.length) {
|
|
61
|
-
console.warn("[@nuxtjs/mdc] Defaulting to no language to be able to highlight lines:", error.message);
|
|
62
|
-
lang = "";
|
|
63
|
-
} else
|
|
64
|
-
throw error;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const root = highlighter.codeToHast(code.trimEnd(), {
|
|
68
|
-
lang,
|
|
69
|
-
themes: themesObject,
|
|
70
|
-
defaultColor: false,
|
|
71
|
-
transformers: [
|
|
72
|
-
...transformers,
|
|
73
|
-
{
|
|
74
|
-
name: "mdc:highlight",
|
|
75
|
-
line(node, line) {
|
|
76
|
-
if (highlights.includes(line))
|
|
77
|
-
addClassToHast(node, "highlight");
|
|
78
|
-
node.properties.line = line;
|
|
79
|
-
}
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
name: "mdc:newline",
|
|
83
|
-
line(node) {
|
|
84
|
-
if (code?.includes("\n")) {
|
|
85
|
-
if (node.children.length === 0 || node.children.length === 1 && node.children[0].type === "element" && node.children[0].children.length === 1 && node.children[0].children[0].type === "text" && node.children[0].children[0].value === "") {
|
|
86
|
-
node.children = [{
|
|
87
|
-
type: "element",
|
|
88
|
-
tagName: "span",
|
|
89
|
-
properties: {
|
|
90
|
-
emptyLinePlaceholder: true
|
|
91
|
-
},
|
|
92
|
-
children: [{ type: "text", value: "\n" }]
|
|
93
|
-
}];
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
const last = node.children.at(-1);
|
|
97
|
-
if (last?.type === "element" && last.tagName === "span") {
|
|
98
|
-
const text = last.children.at(-1);
|
|
99
|
-
if (text?.type === "text")
|
|
100
|
-
text.value += "\n";
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
]
|
|
106
|
-
});
|
|
107
|
-
const preEl = root.children[0];
|
|
108
|
-
const codeEl = preEl.children[0];
|
|
109
|
-
preEl.properties.style = wrapperStyle ? typeof wrapperStyle === "string" ? wrapperStyle : preEl.properties.style : "";
|
|
110
|
-
const styles = [];
|
|
111
|
-
Object.keys(themesObject).forEach((color) => {
|
|
112
|
-
const colorScheme = color !== "default" ? `.${color}` : "";
|
|
113
|
-
styles.push(
|
|
114
|
-
wrapperStyle ? `${colorScheme} .shiki,` : "",
|
|
115
|
-
`html .${color} .shiki span {`,
|
|
116
|
-
`color: var(--shiki-${color});`,
|
|
117
|
-
`background: var(--shiki-${color}-bg);`,
|
|
118
|
-
`font-style: var(--shiki-${color}-font-style);`,
|
|
119
|
-
`font-weight: var(--shiki-${color}-font-weight);`,
|
|
120
|
-
`text-decoration: var(--shiki-${color}-text-decoration);`,
|
|
121
|
-
"}"
|
|
122
|
-
);
|
|
123
|
-
styles.unshift(
|
|
124
|
-
`html${colorScheme} .shiki span {`,
|
|
125
|
-
`color: var(--shiki-${color});`,
|
|
126
|
-
`background: var(--shiki-${color}-bg);`,
|
|
127
|
-
`font-style: var(--shiki-${color}-font-style);`,
|
|
128
|
-
`font-weight: var(--shiki-${color}-font-weight);`,
|
|
129
|
-
`text-decoration: var(--shiki-${color}-text-decoration);`,
|
|
130
|
-
"}"
|
|
131
|
-
);
|
|
132
|
-
});
|
|
133
|
-
return {
|
|
134
|
-
tree: codeEl.children,
|
|
135
|
-
className: Array.isArray(preEl.properties.class) ? preEl.properties.class.join(" ") : preEl.properties.class,
|
|
136
|
-
inlineStyle: preEl.properties.style,
|
|
137
|
-
style: styles.join("")
|
|
138
|
-
};
|
|
139
|
-
} catch (error) {
|
|
140
|
-
console.warn("[@nuxtjs/mdc] Failed to highlight code block:", error.message);
|
|
141
|
-
return {
|
|
142
|
-
tree: [{ type: "text", value: code }],
|
|
143
|
-
className: "",
|
|
144
|
-
inlineStyle: "",
|
|
145
|
-
style: ""
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
return {
|
|
150
|
-
getHighlightedAST
|
|
151
|
-
};
|
|
152
|
-
});
|
|
153
|
-
function createSingleton(fn) {
|
|
154
|
-
let instance;
|
|
155
|
-
return (...args) => {
|
|
156
|
-
if (!instance) {
|
|
157
|
-
instance = fn(...args);
|
|
158
|
-
}
|
|
159
|
-
return instance;
|
|
160
|
-
};
|
|
161
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Root } from '../types/hast';
|
|
2
|
-
import type { Highlighter, Theme } from './types';
|
|
3
|
-
interface RehypeShikiOption {
|
|
4
|
-
theme?: Theme;
|
|
5
|
-
highlighter?: Highlighter;
|
|
6
|
-
}
|
|
7
|
-
export default rehypeShiki;
|
|
8
|
-
export declare function rehypeShiki(opts?: RehypeShikiOption): (tree: Root) => Promise<void>;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Element, Text } from '../types/hast';
|
|
2
|
-
import type { BuiltinTheme } from 'shikiji';
|
|
3
|
-
export type Theme = BuiltinTheme | Record<string, BuiltinTheme>;
|
|
4
|
-
export interface HighlighterOptions {
|
|
5
|
-
highlights: number[];
|
|
6
|
-
}
|
|
7
|
-
export interface HighlightResult {
|
|
8
|
-
tree: (Element | Text)[];
|
|
9
|
-
className: string;
|
|
10
|
-
style: string;
|
|
11
|
-
inlineStyle: string;
|
|
12
|
-
}
|
|
13
|
-
export type Highlighter = (code: string, lang: string, theme: Theme, highlights: number[]) => Promise<HighlightResult>;
|
|
File without changes
|