@vuepress/plugin-shiki 2.0.0-rc.31 → 2.0.0-rc.33
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/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/markdown/applyHighlighter/createMarkdownFilePathGetter.d.ts +3 -0
- package/lib/node/markdown/applyHighlighter/createMarkdownFilePathGetter.js +10 -0
- package/lib/node/markdown/applyHighlighter/getLanguage.d.ts +2 -0
- package/lib/node/markdown/applyHighlighter/getLanguage.js +20 -0
- package/lib/node/markdown/applyHighlighter/handleMustache.d.ts +1 -0
- package/lib/node/markdown/applyHighlighter/handleMustache.js +26 -0
- package/lib/node/markdown/applyHighlighter/index.d.ts +4 -0
- package/lib/node/markdown/applyHighlighter/index.js +45 -0
- package/lib/node/markdown/index.d.ts +1 -0
- package/lib/node/markdown/index.js +1 -0
- package/lib/node/markdown/lineNumberPlugin.js +4 -1
- package/lib/node/markdown/preWrapperPlugin.js +31 -4
- package/lib/node/options.d.ts +5 -0
- package/lib/node/options.js +1 -0
- package/lib/node/shiki.d.ts +2 -0
- package/lib/node/shiki.js +3 -0
- package/lib/node/shikiPlugin.d.ts +1 -5
- package/lib/node/shikiPlugin.js +2 -3
- package/lib/node/transformers/getTransformers.js +2 -2
- package/lib/node/transformers/vuepressTransformers.d.ts +1 -0
- package/lib/node/transformers/vuepressTransformers.js +6 -0
- package/lib/node/types.d.ts +42 -21
- package/lib/node/utils.d.ts +3 -0
- package/lib/node/utils.js +3 -0
- package/package.json +7 -6
- package/lib/node/resolveHighlight.d.ts +0 -2
- package/lib/node/resolveHighlight.js +0 -60
package/lib/node/index.d.ts
CHANGED
package/lib/node/index.js
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const createMarkdownFilePathGetter = (md) => {
|
|
2
|
+
const store = {};
|
|
3
|
+
const rawRender = md.render;
|
|
4
|
+
// we need to store file path before each render
|
|
5
|
+
md.render = (src, env) => {
|
|
6
|
+
store.path = env.filePathRelative;
|
|
7
|
+
return rawRender(src, env);
|
|
8
|
+
};
|
|
9
|
+
return () => store.path || 'dynamic pages';
|
|
10
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { isSpecialLang } from 'shiki';
|
|
2
|
+
import { colors } from 'vuepress/utils';
|
|
3
|
+
import { logger, resolveLanguage } from '../../utils.js';
|
|
4
|
+
const WARNED_LANGS = new Set();
|
|
5
|
+
export const getLanguage = (lang, loadedLanguages, defaultLang, logLevel, getMarkdownFilePath) => {
|
|
6
|
+
let result = resolveLanguage(lang);
|
|
7
|
+
if (result && !loadedLanguages.includes(result) && !isSpecialLang(result)) {
|
|
8
|
+
// warn for unknown languages only once
|
|
9
|
+
if (logLevel !== 'silent' && !WARNED_LANGS.has(result)) {
|
|
10
|
+
logger.warn(`Missing ${colors.cyan(lang)} highlighter, use ${colors.cyan(defaultLang)} to highlight instead.`);
|
|
11
|
+
WARNED_LANGS.add(result);
|
|
12
|
+
}
|
|
13
|
+
// log file path if unknown language is found
|
|
14
|
+
if (logLevel === 'debug') {
|
|
15
|
+
logger.info(`Unknown language ${colors.cyan(result)} found in ${colors.cyan(getMarkdownFilePath())}`);
|
|
16
|
+
}
|
|
17
|
+
result = defaultLang;
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const handleMustache: (str: string, highlight: (str: string) => string) => string;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { nanoid } from '../../utils.js';
|
|
2
|
+
const MUSTACHE_REG = /\{\{[^]*?\}\}/g;
|
|
3
|
+
/**
|
|
4
|
+
* Replace mustache with unique markers
|
|
5
|
+
* @param str content
|
|
6
|
+
* @param store mustache store
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
const removeMustache = (str, store) => str.replace(MUSTACHE_REG, (match) => {
|
|
10
|
+
let marker = store.get(match);
|
|
11
|
+
if (!marker) {
|
|
12
|
+
marker = nanoid();
|
|
13
|
+
store.set(match, marker);
|
|
14
|
+
}
|
|
15
|
+
return marker;
|
|
16
|
+
});
|
|
17
|
+
const restoreMustache = (str, store) => {
|
|
18
|
+
store.forEach((marker, match) => {
|
|
19
|
+
str = str.replaceAll(marker, match);
|
|
20
|
+
});
|
|
21
|
+
return str;
|
|
22
|
+
};
|
|
23
|
+
export const handleMustache = (str, highlight) => {
|
|
24
|
+
const store = new Map();
|
|
25
|
+
return restoreMustache(highlight(removeMustache(str, store).trimEnd()), store);
|
|
26
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type MarkdownIt from 'markdown-it';
|
|
2
|
+
import type { App } from 'vuepress';
|
|
3
|
+
import type { ShikiHighlightOptions } from '../../types.js';
|
|
4
|
+
export declare const applyHighlighter: (md: MarkdownIt, app: App, { langs, langAlias, defaultLang, transformers: userTransformers, ...options }?: ShikiHighlightOptions) => Promise<void>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { transformerCompactLineOptions } from '@shikijs/transformers';
|
|
2
|
+
import { getHighlighter } from 'shiki';
|
|
3
|
+
import { bundledLanguageNames } from '../../shiki.js';
|
|
4
|
+
import { getTransformers } from '../../transformers/getTransformers.js';
|
|
5
|
+
import { attrsToLines } from '../../utils.js';
|
|
6
|
+
import { createMarkdownFilePathGetter } from './createMarkdownFilePathGetter.js';
|
|
7
|
+
import { getLanguage } from './getLanguage.js';
|
|
8
|
+
import { handleMustache } from './handleMustache.js';
|
|
9
|
+
export const applyHighlighter = async (md, app, { langs = bundledLanguageNames, langAlias = {}, defaultLang = 'plain', transformers: userTransformers = [], ...options } = {}) => {
|
|
10
|
+
const logLevel = options.logLevel ?? (app.env.isDebug ? 'debug' : 'warn');
|
|
11
|
+
const getMarkdownFilePath = logLevel === 'debug' ? createMarkdownFilePathGetter(md) : null;
|
|
12
|
+
const highlighter = await getHighlighter({
|
|
13
|
+
langs,
|
|
14
|
+
langAlias,
|
|
15
|
+
themes: 'themes' in options
|
|
16
|
+
? Object.values(options.themes)
|
|
17
|
+
: [options.theme ?? 'nord'],
|
|
18
|
+
});
|
|
19
|
+
await options.shikiSetup?.(highlighter);
|
|
20
|
+
const transformers = getTransformers(options);
|
|
21
|
+
const loadedLanguages = highlighter.getLoadedLanguages();
|
|
22
|
+
md.options.highlight = (str, language, attrs) => handleMustache(str, (str) => highlighter.codeToHtml(str, {
|
|
23
|
+
lang: getLanguage(language, loadedLanguages, defaultLang, logLevel, getMarkdownFilePath),
|
|
24
|
+
meta: {
|
|
25
|
+
/**
|
|
26
|
+
* Custom `transformers` passed by users may require `attrs`.
|
|
27
|
+
* e.g. [transformerNotationWordHighlight](https://shiki.style/packages/transformers#transformernotationwordhighlight)
|
|
28
|
+
*/
|
|
29
|
+
__raw: attrs,
|
|
30
|
+
},
|
|
31
|
+
transformers: [
|
|
32
|
+
...transformers,
|
|
33
|
+
...(options.highlightLines ?? true
|
|
34
|
+
? [transformerCompactLineOptions(attrsToLines(attrs))]
|
|
35
|
+
: []),
|
|
36
|
+
...userTransformers,
|
|
37
|
+
],
|
|
38
|
+
...('themes' in options
|
|
39
|
+
? {
|
|
40
|
+
themes: options.themes,
|
|
41
|
+
defaultColor: false,
|
|
42
|
+
}
|
|
43
|
+
: { theme: options.theme ?? 'nord' }),
|
|
44
|
+
}));
|
|
45
|
+
};
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// It depends on preWrapper plugin.
|
|
3
3
|
const LINE_NUMBERS_REGEXP = /:line-numbers\b/;
|
|
4
4
|
const NO_LINE_NUMBERS_REGEXP = /:no-line-numbers\b/;
|
|
5
|
+
const LINE_NUMBERS_START_REGEXP = /:line-numbers=(\d+)\b/;
|
|
5
6
|
export const lineNumberPlugin = (md, { lineNumbers = true } = {}) => {
|
|
6
7
|
const rawFence = md.renderer.rules.fence;
|
|
7
8
|
md.renderer.rules.fence = (...args) => {
|
|
@@ -21,10 +22,12 @@ export const lineNumberPlugin = (md, { lineNumbers = true } = {}) => {
|
|
|
21
22
|
!enableLineNumbers) {
|
|
22
23
|
return rawCode;
|
|
23
24
|
}
|
|
25
|
+
const startNumbers = Number(info.match(LINE_NUMBERS_START_REGEXP)?.[1] ?? 1) - 1;
|
|
26
|
+
const lineNumbersStyle = `style="counter-reset:line-number ${startNumbers}"`;
|
|
24
27
|
const lineNumbersCode = [...Array(lines.length)]
|
|
25
28
|
.map(() => `<div class="line-number"></div>`)
|
|
26
29
|
.join('');
|
|
27
|
-
const lineNumbersWrapperCode = `<div class="line-numbers" aria-hidden="true">${lineNumbersCode}</div>`;
|
|
30
|
+
const lineNumbersWrapperCode = `<div class="line-numbers" aria-hidden="true" ${lineNumbersStyle}>${lineNumbersCode}</div>`;
|
|
28
31
|
const finalCode = rawCode
|
|
29
32
|
.replace(/<\/div>$/, `${lineNumbersWrapperCode}</div>`)
|
|
30
33
|
.replace(/"(language-[^"]*?)"/, '"$1 line-numbers-mode"');
|
|
@@ -1,21 +1,48 @@
|
|
|
1
1
|
import { resolveAttr, resolveLanguage } from '../utils.js';
|
|
2
|
+
const PRE_ATTRS_REGEXP = /<pre([\s\S]*?)style="([^"]*)"([^>]*)>/;
|
|
2
3
|
export const preWrapperPlugin = (md, { preWrapper = true } = {}) => {
|
|
3
4
|
const rawFence = md.renderer.rules.fence;
|
|
4
5
|
md.renderer.rules.fence = (...args) => {
|
|
6
|
+
let result = rawFence(...args);
|
|
7
|
+
if (!result.startsWith('<pre')) {
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
5
10
|
const [tokens, idx, options] = args;
|
|
6
11
|
const token = tokens[idx];
|
|
7
12
|
// get token info
|
|
8
13
|
const info = token.info ? md.utils.unescapeAll(token.info).trim() : '';
|
|
9
14
|
const lang = resolveLanguage(info);
|
|
10
|
-
const title = resolveAttr(info, 'title') || lang;
|
|
11
15
|
const languageClass = `${options.langPrefix}${lang}`;
|
|
12
|
-
let result = rawFence(...args);
|
|
13
16
|
if (!preWrapper) {
|
|
14
|
-
|
|
17
|
+
/**
|
|
18
|
+
* remove `<code>` attributes
|
|
19
|
+
*
|
|
20
|
+
* In the source code of `markdown-it fence line 71, line 74`,
|
|
21
|
+
* `fence` writes `class="language-*"` onto the `code` element,
|
|
22
|
+
* whereas in past versions, `vuepress` wrote it on the `pre` element.
|
|
23
|
+
* Therefore, this behavior needs to be reset.
|
|
24
|
+
*
|
|
25
|
+
* Even though `shiki` directly returns the contents within `<pre>`
|
|
26
|
+
* at `line 48` of `markdown-it`, I believe it is still prudent to make this adjustment.
|
|
27
|
+
*
|
|
28
|
+
* @see https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.mjs
|
|
29
|
+
*/
|
|
15
30
|
result = result.replace(/<code[^]*?>/, '<code>');
|
|
16
31
|
result = `<pre class="${languageClass}"${result.slice('<pre'.length)}`;
|
|
17
32
|
return result;
|
|
18
33
|
}
|
|
19
|
-
|
|
34
|
+
const title = resolveAttr(info, 'title') || lang;
|
|
35
|
+
let styles = '';
|
|
36
|
+
// before: maybe `v-pre class="shiki *"`
|
|
37
|
+
// after: style="*" tab-index="*"
|
|
38
|
+
result = result.replace(PRE_ATTRS_REGEXP, (_, before, style, after) => {
|
|
39
|
+
styles = style.trim();
|
|
40
|
+
// Keep `v-pre class="*"`, remove the rest.
|
|
41
|
+
return `<pre ${before.trim()}${after.trimEnd()}>`;
|
|
42
|
+
});
|
|
43
|
+
/**
|
|
44
|
+
* Add information to dataset for current code block.
|
|
45
|
+
*/
|
|
46
|
+
return `<div class="${languageClass}" data-highlighter="shiki" data-ext="${lang}" data-title="${title}" style="${styles}">${result}</div>`;
|
|
20
47
|
};
|
|
21
48
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,7 +1,3 @@
|
|
|
1
1
|
import type { Plugin } from 'vuepress/core';
|
|
2
|
-
import type {
|
|
3
|
-
/**
|
|
4
|
-
* Options of @vuepress/plugin-shiki
|
|
5
|
-
*/
|
|
6
|
-
export type ShikiPluginOptions = ShikiHighlightOptions & PreWrapperOptions & LineNumberOptions;
|
|
2
|
+
import type { ShikiPluginOptions } from './options.js';
|
|
7
3
|
export declare const shikiPlugin: ({ preWrapper, lineNumbers, ...options }?: ShikiPluginOptions) => Plugin;
|
package/lib/node/shikiPlugin.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { isPlainObject } from 'vuepress/shared';
|
|
2
|
-
import { highlightLinesPlugin, lineNumberPlugin, preWrapperPlugin, } from './markdown/index.js';
|
|
3
|
-
import { resolveHighlight } from './resolveHighlight.js';
|
|
2
|
+
import { applyHighlighter, highlightLinesPlugin, lineNumberPlugin, preWrapperPlugin, } from './markdown/index.js';
|
|
4
3
|
export const shikiPlugin = ({ preWrapper = true, lineNumbers = true, ...options } = {}) => ({
|
|
5
4
|
name: '@vuepress/plugin-shiki',
|
|
6
5
|
extendsMarkdown: async (md, app) => {
|
|
7
6
|
const { code } = app.options.markdown;
|
|
8
|
-
md
|
|
7
|
+
await applyHighlighter(md, app, {
|
|
9
8
|
...(isPlainObject(code) ? code : {}),
|
|
10
9
|
...options,
|
|
11
10
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, } from '@shikijs/transformers';
|
|
2
|
-
import { addClassTransformer, emptyLineTransformer, removeEscapeTransformer, } from './vuepressTransformers.js';
|
|
2
|
+
import { addClassTransformer, cleanupTransformer, emptyLineTransformer, removeEscapeTransformer, } from './vuepressTransformers.js';
|
|
3
3
|
export const getTransformers = (options) => {
|
|
4
4
|
const transformers = [];
|
|
5
5
|
if (options.notationDiff) {
|
|
@@ -17,6 +17,6 @@ export const getTransformers = (options) => {
|
|
|
17
17
|
if (options.notationErrorLevel) {
|
|
18
18
|
transformers.push(transformerNotationErrorLevel());
|
|
19
19
|
}
|
|
20
|
-
transformers.push(addClassTransformer, removeEscapeTransformer, emptyLineTransformer);
|
|
20
|
+
transformers.push(addClassTransformer, cleanupTransformer, removeEscapeTransformer, emptyLineTransformer);
|
|
21
21
|
return transformers;
|
|
22
22
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ShikiTransformer } from 'shiki';
|
|
2
2
|
export declare const addClassTransformer: ShikiTransformer;
|
|
3
|
+
export declare const cleanupTransformer: ShikiTransformer;
|
|
3
4
|
/**
|
|
4
5
|
* This `transformer` is primarily for the usage instructions of themes.
|
|
5
6
|
* When developers need to provide an example like `// [!code xxx]`,
|
|
@@ -5,6 +5,12 @@ export const addClassTransformer = {
|
|
|
5
5
|
this.addClassToHast(node, 'vp-code');
|
|
6
6
|
},
|
|
7
7
|
};
|
|
8
|
+
export const cleanupTransformer = {
|
|
9
|
+
name: 'vuepress:cleanup',
|
|
10
|
+
pre(node) {
|
|
11
|
+
delete node.properties.tabindex;
|
|
12
|
+
},
|
|
13
|
+
};
|
|
8
14
|
/**
|
|
9
15
|
* This `transformer` is primarily for the usage instructions of themes.
|
|
10
16
|
* When developers need to provide an example like `// [!code xxx]`,
|
package/lib/node/types.d.ts
CHANGED
|
@@ -1,7 +1,29 @@
|
|
|
1
|
-
import type { BundledLanguage, BundledTheme, Highlighter, LanguageInput, ShikiTransformer, SpecialLanguage, StringLiteralUnion,
|
|
1
|
+
import type { BundledLanguage, BundledTheme, Highlighter, LanguageInput, ShikiTransformer, SpecialLanguage, StringLiteralUnion, ThemeRegistrationAny } from 'shiki';
|
|
2
2
|
export type ShikiLang = LanguageInput | StringLiteralUnion<BundledLanguage> | SpecialLanguage;
|
|
3
|
-
export type ShikiTheme =
|
|
4
|
-
export interface
|
|
3
|
+
export type ShikiTheme = ThemeRegistrationAny | StringLiteralUnion<BundledTheme>;
|
|
4
|
+
export interface ShikiSingleThemeOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The single theme to use
|
|
7
|
+
*
|
|
8
|
+
* @see https://shiki.style/themes
|
|
9
|
+
*
|
|
10
|
+
* @default 'nord'
|
|
11
|
+
*/
|
|
12
|
+
theme?: ShikiTheme;
|
|
13
|
+
}
|
|
14
|
+
export interface ShikiDualThemeOptions {
|
|
15
|
+
/**
|
|
16
|
+
* The dark and light themes to use
|
|
17
|
+
*
|
|
18
|
+
* @see https://shiki.style/themes
|
|
19
|
+
*/
|
|
20
|
+
themes: {
|
|
21
|
+
dark: ShikiTheme;
|
|
22
|
+
light: ShikiTheme;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export type ShikiThemeOptions = ShikiDualThemeOptions | ShikiSingleThemeOptions;
|
|
26
|
+
export type ShikiHighlightOptions = ShikiThemeOptions & {
|
|
5
27
|
/**
|
|
6
28
|
* Languages to be loaded.
|
|
7
29
|
*
|
|
@@ -12,34 +34,25 @@ export interface ShikiHighlightOptions {
|
|
|
12
34
|
*/
|
|
13
35
|
langs?: ShikiLang[];
|
|
14
36
|
/**
|
|
15
|
-
*
|
|
16
|
-
*/
|
|
17
|
-
defaultHighlightLang?: string;
|
|
18
|
-
/**
|
|
19
|
-
* The single theme to use
|
|
37
|
+
* Language alias
|
|
20
38
|
*
|
|
21
|
-
* @see https://shiki.style/
|
|
39
|
+
* @see https://shiki.style/guide/load-lang#custom-language-aliases
|
|
22
40
|
*/
|
|
23
|
-
|
|
41
|
+
langAlias?: Record<string, StringLiteralUnion<BundledLanguage>>;
|
|
24
42
|
/**
|
|
25
|
-
*
|
|
43
|
+
* Fallback language when the specified language is not available.
|
|
26
44
|
*
|
|
27
|
-
* @
|
|
45
|
+
* @default 'plain'
|
|
28
46
|
*/
|
|
29
|
-
|
|
30
|
-
dark: ShikiTheme;
|
|
31
|
-
light: ShikiTheme;
|
|
32
|
-
};
|
|
47
|
+
defaultLang?: string;
|
|
33
48
|
/**
|
|
34
49
|
* Function to customize Shiki highlighter instance.
|
|
35
50
|
*/
|
|
36
51
|
shikiSetup?: (shiki: Highlighter) => void | Promise<void>;
|
|
37
|
-
transformers?: ShikiTransformer[];
|
|
38
52
|
/**
|
|
39
|
-
*
|
|
40
|
-
* The rest of the themes are applied via CSS variables, and toggled by CSS overrides.
|
|
53
|
+
* Shiki transformers
|
|
41
54
|
*/
|
|
42
|
-
|
|
55
|
+
transformers?: ShikiTransformer[];
|
|
43
56
|
/**
|
|
44
57
|
* Enable highlight lines or not
|
|
45
58
|
*
|
|
@@ -78,7 +91,15 @@ export interface ShikiHighlightOptions {
|
|
|
78
91
|
* @see https://shiki.style/packages/transformers#transformernotationerrorlevel
|
|
79
92
|
*/
|
|
80
93
|
notationErrorLevel?: boolean;
|
|
81
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Log level Highlighter language detecter
|
|
96
|
+
*
|
|
97
|
+
* @description defaults to `'debug'` when `--debug` flag is enabled
|
|
98
|
+
*
|
|
99
|
+
* @default 'warn'
|
|
100
|
+
*/
|
|
101
|
+
logLevel?: 'silent' | 'warn' | 'debug';
|
|
102
|
+
};
|
|
82
103
|
export interface LineNumberOptions {
|
|
83
104
|
/**
|
|
84
105
|
* Show line numbers in code blocks
|
package/lib/node/utils.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import type { TransformerCompactLineOption } from '@shikijs/transformers';
|
|
2
|
+
import { Logger } from '@vuepress/helper';
|
|
3
|
+
export declare const PLUGIN_NAME = "@vuepress/plugin-shiki";
|
|
4
|
+
export declare const logger: Logger;
|
|
2
5
|
export declare const nanoid: (size?: number | undefined) => string;
|
|
3
6
|
export declare const resolveLanguage: (info: string) => string;
|
|
4
7
|
export declare const resolveAttr: (info: string, attr: string) => string | null;
|
package/lib/node/utils.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { Logger } from '@vuepress/helper';
|
|
1
2
|
import { customAlphabet } from 'nanoid';
|
|
2
3
|
const VUE_RE = /-vue$/;
|
|
4
|
+
export const PLUGIN_NAME = '@vuepress/plugin-shiki';
|
|
5
|
+
export const logger = new Logger(PLUGIN_NAME);
|
|
3
6
|
export const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10);
|
|
4
7
|
export const resolveLanguage = (info) => info
|
|
5
8
|
.match(/^([^ :[{]+)/)?.[1]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vuepress/plugin-shiki",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.33",
|
|
4
4
|
"description": "VuePress plugin - shiki",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vuepress-plugin",
|
|
@@ -9,14 +9,14 @@
|
|
|
9
9
|
"shiki",
|
|
10
10
|
"highlight"
|
|
11
11
|
],
|
|
12
|
-
"homepage": "https://ecosystem.vuejs.press/plugins/shiki.html",
|
|
12
|
+
"homepage": "https://ecosystem.vuejs.press/plugins/markdown/shiki.html",
|
|
13
13
|
"bugs": {
|
|
14
14
|
"url": "https://github.com/vuepress/ecosystem/issues"
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
18
18
|
"url": "git+https://github.com/vuepress/ecosystem.git",
|
|
19
|
-
"directory": "plugins/plugin-shiki"
|
|
19
|
+
"directory": "plugins/markdown/plugin-shiki"
|
|
20
20
|
},
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"author": "meteorlxy",
|
|
@@ -35,9 +35,10 @@
|
|
|
35
35
|
"clean": "rimraf --glob ./lib ./*.tsbuildinfo"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@shikijs/transformers": "^1.6.
|
|
38
|
+
"@shikijs/transformers": "^1.6.1",
|
|
39
|
+
"@vuepress/helper": "2.0.0-rc.33",
|
|
39
40
|
"nanoid": "^5.0.7",
|
|
40
|
-
"shiki": "^1.6.
|
|
41
|
+
"shiki": "^1.6.1"
|
|
41
42
|
},
|
|
42
43
|
"peerDependencies": {
|
|
43
44
|
"vuepress": "2.0.0-rc.12"
|
|
@@ -49,5 +50,5 @@
|
|
|
49
50
|
"@types/markdown-it": "^14.1.1",
|
|
50
51
|
"markdown-it": "^14.1.0"
|
|
51
52
|
},
|
|
52
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "6e9565ef71b7eb41f8bf8736dc9f6544f1925c58"
|
|
53
54
|
}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import type { ShikiHighlightOptions } from './types.js';
|
|
2
|
-
export declare const resolveHighlight: ({ langs, theme, themes, defaultHighlightLang, transformers: userTransformers, ...options }?: ShikiHighlightOptions) => Promise<(str: string, lang: string, attrs: string) => string>;
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { transformerCompactLineOptions } from '@shikijs/transformers';
|
|
2
|
-
import { bundledLanguages, getHighlighter, isPlainLang, isSpecialLang, } from 'shiki';
|
|
3
|
-
import { colors, logger } from 'vuepress/utils';
|
|
4
|
-
import { getTransformers } from './transformers/getTransformers.js';
|
|
5
|
-
import { attrsToLines, nanoid, resolveLanguage } from './utils.js';
|
|
6
|
-
const DEFAULT_LANGS = Object.keys(bundledLanguages);
|
|
7
|
-
const MUSTACHE_REG = /\{\{[^]*?\}\}/g;
|
|
8
|
-
export const resolveHighlight = async ({ langs = DEFAULT_LANGS, theme = 'nord', themes, defaultHighlightLang = '', transformers: userTransformers = [], ...options } = {}) => {
|
|
9
|
-
const highlighter = await getHighlighter({
|
|
10
|
-
langs,
|
|
11
|
-
themes: themes ? [themes.dark, themes.light] : [theme],
|
|
12
|
-
});
|
|
13
|
-
await options.shikiSetup?.(highlighter);
|
|
14
|
-
const transformers = getTransformers(options);
|
|
15
|
-
return (str, language, attrs) => {
|
|
16
|
-
let lang = resolveLanguage(language);
|
|
17
|
-
if (lang) {
|
|
18
|
-
const langLoaded = highlighter.getLoadedLanguages().includes(lang);
|
|
19
|
-
if (!langLoaded && !isPlainLang(lang) && !isSpecialLang(lang)) {
|
|
20
|
-
logger.warn(colors.yellow(`\nThe language '${lang}' is not loaded, falling back to '${defaultHighlightLang || 'txt'}' for syntax highlighting.`));
|
|
21
|
-
lang = defaultHighlightLang;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
const codeMustaches = new Map();
|
|
25
|
-
const removeMustache = (str) => str.replace(MUSTACHE_REG, (match) => {
|
|
26
|
-
let marker = codeMustaches.get(match);
|
|
27
|
-
if (!marker) {
|
|
28
|
-
marker = nanoid();
|
|
29
|
-
codeMustaches.set(match, marker);
|
|
30
|
-
}
|
|
31
|
-
return marker;
|
|
32
|
-
});
|
|
33
|
-
const restoreMustache = (str) => {
|
|
34
|
-
codeMustaches.forEach((marker, match) => {
|
|
35
|
-
str = str.replaceAll(marker, match);
|
|
36
|
-
});
|
|
37
|
-
return str;
|
|
38
|
-
};
|
|
39
|
-
str = removeMustache(str).trimEnd();
|
|
40
|
-
const highlighted = highlighter.codeToHtml(str, {
|
|
41
|
-
lang,
|
|
42
|
-
meta: {
|
|
43
|
-
/**
|
|
44
|
-
* Custom `transformers` passed by users may require `attrs`.
|
|
45
|
-
* e.g. [transformerNotationWordHighlight](https://shiki.style/packages/transformers#transformernotationwordhighlight)
|
|
46
|
-
*/
|
|
47
|
-
__raw: attrs,
|
|
48
|
-
},
|
|
49
|
-
transformers: [
|
|
50
|
-
...transformers,
|
|
51
|
-
...(options.highlightLines ?? true
|
|
52
|
-
? [transformerCompactLineOptions(attrsToLines(attrs))]
|
|
53
|
-
: []),
|
|
54
|
-
...userTransformers,
|
|
55
|
-
],
|
|
56
|
-
...(themes ? { themes, defaultColor: options.defaultColor } : { theme }),
|
|
57
|
-
});
|
|
58
|
-
return restoreMustache(highlighted);
|
|
59
|
-
};
|
|
60
|
-
};
|