@vuepress/plugin-shiki 2.0.0-rc.3 → 2.0.0-rc.31

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.
@@ -1,3 +1,5 @@
1
1
  import { shikiPlugin } from './shikiPlugin.js';
2
2
  export * from './shikiPlugin.js';
3
+ export * from './types.js';
4
+ /** @deprecated Use named export instead */
3
5
  export default shikiPlugin;
package/lib/node/index.js CHANGED
@@ -1,3 +1,5 @@
1
1
  import { shikiPlugin } from './shikiPlugin.js';
2
2
  export * from './shikiPlugin.js';
3
+ export * from './types.js';
4
+ /** @deprecated Use named export instead */
3
5
  export default shikiPlugin;
@@ -0,0 +1,2 @@
1
+ import type { Markdown } from 'vuepress/markdown';
2
+ export declare const highlightLinesPlugin: (md: Markdown) => void;
@@ -0,0 +1,22 @@
1
+ // Modified from https://github.com/egoist/markdown-it-highlight-lines
2
+ // Now this plugin is only used to normalize line attrs.
3
+ // The else part of line highlights logic is in '../highlight.ts'.
4
+ const HIGHLIGHT_LINES_REGEXP = /{([\d,-]+)}/;
5
+ export const highlightLinesPlugin = (md) => {
6
+ const rawFence = md.renderer.rules.fence;
7
+ md.renderer.rules.fence = (...args) => {
8
+ const [tokens, idx] = args;
9
+ const token = tokens[idx];
10
+ let lines = null;
11
+ const rawInfo = token.info;
12
+ const result = rawInfo?.match(HIGHLIGHT_LINES_REGEXP);
13
+ if (!result) {
14
+ return rawFence(...args);
15
+ }
16
+ // ensure the next plugin get the correct lang
17
+ token.info = rawInfo.replace(HIGHLIGHT_LINES_REGEXP, '').trim();
18
+ lines = result[1];
19
+ token.info += ' ' + lines;
20
+ return rawFence(...args);
21
+ };
22
+ };
@@ -0,0 +1,3 @@
1
+ export * from './highlightLinesPlugin.js';
2
+ export * from './lineNumberPlugin.js';
3
+ export * from './preWrapperPlugin.js';
@@ -0,0 +1,3 @@
1
+ export * from './highlightLinesPlugin.js';
2
+ export * from './lineNumberPlugin.js';
3
+ export * from './preWrapperPlugin.js';
@@ -0,0 +1,3 @@
1
+ import type { Markdown } from 'vuepress/markdown';
2
+ import type { LineNumberOptions } from '../types.js';
3
+ export declare const lineNumberPlugin: (md: Markdown, { lineNumbers }?: LineNumberOptions) => void;
@@ -0,0 +1,33 @@
1
+ // markdown-it plugin for generating line numbers.
2
+ // It depends on preWrapper plugin.
3
+ const LINE_NUMBERS_REGEXP = /:line-numbers\b/;
4
+ const NO_LINE_NUMBERS_REGEXP = /:no-line-numbers\b/;
5
+ export const lineNumberPlugin = (md, { lineNumbers = true } = {}) => {
6
+ const rawFence = md.renderer.rules.fence;
7
+ md.renderer.rules.fence = (...args) => {
8
+ const rawCode = rawFence(...args);
9
+ const [tokens, idx] = args;
10
+ const info = tokens[idx].info;
11
+ const enableLineNumbers = LINE_NUMBERS_REGEXP.test(info);
12
+ const disableLineNumbers = NO_LINE_NUMBERS_REGEXP.test(info);
13
+ if ((!lineNumbers && !enableLineNumbers) ||
14
+ (lineNumbers && disableLineNumbers)) {
15
+ return rawCode;
16
+ }
17
+ const code = rawCode.slice(rawCode.indexOf('<code>'), rawCode.indexOf('</code>'));
18
+ const lines = code.split('\n');
19
+ if (typeof lineNumbers === 'number' &&
20
+ lines.length < lineNumbers &&
21
+ !enableLineNumbers) {
22
+ return rawCode;
23
+ }
24
+ const lineNumbersCode = [...Array(lines.length)]
25
+ .map(() => `<div class="line-number"></div>`)
26
+ .join('');
27
+ const lineNumbersWrapperCode = `<div class="line-numbers" aria-hidden="true">${lineNumbersCode}</div>`;
28
+ const finalCode = rawCode
29
+ .replace(/<\/div>$/, `${lineNumbersWrapperCode}</div>`)
30
+ .replace(/"(language-[^"]*?)"/, '"$1 line-numbers-mode"');
31
+ return finalCode;
32
+ };
33
+ };
@@ -0,0 +1,3 @@
1
+ import type { Markdown } from 'vuepress/markdown';
2
+ import type { PreWrapperOptions } from '../types.js';
3
+ export declare const preWrapperPlugin: (md: Markdown, { preWrapper }?: PreWrapperOptions) => void;
@@ -0,0 +1,21 @@
1
+ import { resolveAttr, resolveLanguage } from '../utils.js';
2
+ export const preWrapperPlugin = (md, { preWrapper = true } = {}) => {
3
+ const rawFence = md.renderer.rules.fence;
4
+ md.renderer.rules.fence = (...args) => {
5
+ const [tokens, idx, options] = args;
6
+ const token = tokens[idx];
7
+ // get token info
8
+ const info = token.info ? md.utils.unescapeAll(token.info).trim() : '';
9
+ const lang = resolveLanguage(info);
10
+ const title = resolveAttr(info, 'title') || lang;
11
+ const languageClass = `${options.langPrefix}${lang}`;
12
+ let result = rawFence(...args);
13
+ if (!preWrapper) {
14
+ // remove `<code>` attributes
15
+ result = result.replace(/<code[^]*?>/, '<code>');
16
+ result = `<pre class="${languageClass}"${result.slice('<pre'.length)}`;
17
+ return result;
18
+ }
19
+ return `<div class="${languageClass}" data-ext="${lang}" data-title="${title}">${result}</div>`;
20
+ };
21
+ };
@@ -0,0 +1,2 @@
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>;
@@ -0,0 +1,60 @@
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
+ };
@@ -1,34 +1,7 @@
1
- import type { BundledLanguage, BundledTheme, LanguageInput, SpecialLanguage, StringLiteralUnion, ThemeRegistration, ThemeRegistrationRaw } from 'shikiji';
2
1
  import type { Plugin } from 'vuepress/core';
3
- export type ShikiLang = LanguageInput | StringLiteralUnion<BundledLanguage> | SpecialLanguage;
4
- export type ShikiTheme = ThemeRegistration | ThemeRegistrationRaw | StringLiteralUnion<BundledTheme>;
2
+ import type { LineNumberOptions, PreWrapperOptions, ShikiHighlightOptions } from './types.js';
5
3
  /**
6
4
  * Options of @vuepress/plugin-shiki
7
5
  */
8
- export interface ShikiPluginOptions {
9
- /**
10
- * Languages to be loaded.
11
- *
12
- * Shikiji does not preload any languages to avoid extra overhead. So you need
13
- * to specify the languages you want to use.
14
- *
15
- * @see https://shikiji.netlify.app/languages
16
- */
17
- langs?: ShikiLang[];
18
- /**
19
- * The single theme to use
20
- *
21
- * @see https://shikiji.netlify.app/themes
22
- */
23
- theme?: ShikiTheme;
24
- /**
25
- * The dark and light themes to use
26
- *
27
- * @see https://shikiji.netlify.app/themes
28
- */
29
- themes?: {
30
- dark: ShikiTheme;
31
- light: ShikiTheme;
32
- };
33
- }
34
- export declare const shikiPlugin: ({ langs, theme, themes, }?: ShikiPluginOptions) => Plugin;
6
+ export type ShikiPluginOptions = ShikiHighlightOptions & PreWrapperOptions & LineNumberOptions;
7
+ export declare const shikiPlugin: ({ preWrapper, lineNumbers, ...options }?: ShikiPluginOptions) => Plugin;
@@ -1,14 +1,18 @@
1
- import { getHighlighter } from 'shikiji';
2
- export const shikiPlugin = ({ langs, theme = 'nord', themes, } = {}) => ({
1
+ import { isPlainObject } from 'vuepress/shared';
2
+ import { highlightLinesPlugin, lineNumberPlugin, preWrapperPlugin, } from './markdown/index.js';
3
+ import { resolveHighlight } from './resolveHighlight.js';
4
+ export const shikiPlugin = ({ preWrapper = true, lineNumbers = true, ...options } = {}) => ({
3
5
  name: '@vuepress/plugin-shiki',
4
- extendsMarkdown: async (md) => {
5
- const highlighter = await getHighlighter({
6
- langs,
7
- themes: themes ? [themes.dark, themes.light] : [theme],
8
- });
9
- md.options.highlight = (code, lang) => highlighter.codeToHtml(code, {
10
- lang,
11
- ...(themes ? { themes } : { theme }),
6
+ extendsMarkdown: async (md, app) => {
7
+ const { code } = app.options.markdown;
8
+ md.options.highlight = await resolveHighlight({
9
+ ...(isPlainObject(code) ? code : {}),
10
+ ...options,
12
11
  });
12
+ md.use(highlightLinesPlugin);
13
+ md.use(preWrapperPlugin, { preWrapper });
14
+ if (preWrapper) {
15
+ md.use(lineNumberPlugin, { lineNumbers });
16
+ }
13
17
  },
14
18
  });
@@ -0,0 +1,3 @@
1
+ import type { ShikiTransformer } from 'shiki';
2
+ import type { ShikiHighlightOptions } from '../types.js';
3
+ export declare const getTransformers: (options: ShikiHighlightOptions) => ShikiTransformer[];
@@ -0,0 +1,22 @@
1
+ import { transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, } from '@shikijs/transformers';
2
+ import { addClassTransformer, emptyLineTransformer, removeEscapeTransformer, } from './vuepressTransformers.js';
3
+ export const getTransformers = (options) => {
4
+ const transformers = [];
5
+ if (options.notationDiff) {
6
+ transformers.push(transformerNotationDiff());
7
+ }
8
+ if (options.notationFocus) {
9
+ transformers.push(transformerNotationFocus({
10
+ classActiveLine: 'has-focus',
11
+ classActivePre: 'has-focused-lines',
12
+ }));
13
+ }
14
+ if (options.notationHighlight) {
15
+ transformers.push(transformerNotationHighlight());
16
+ }
17
+ if (options.notationErrorLevel) {
18
+ transformers.push(transformerNotationErrorLevel());
19
+ }
20
+ transformers.push(addClassTransformer, removeEscapeTransformer, emptyLineTransformer);
21
+ return transformers;
22
+ };
@@ -0,0 +1,2 @@
1
+ export * from './getTransformers.js';
2
+ export * from './vuepressTransformers.js';
@@ -0,0 +1,2 @@
1
+ export * from './getTransformers.js';
2
+ export * from './vuepressTransformers.js';
@@ -0,0 +1,11 @@
1
+ import type { ShikiTransformer } from 'shiki';
2
+ export declare const addClassTransformer: ShikiTransformer;
3
+ /**
4
+ * This `transformer` is primarily for the usage instructions of themes.
5
+ * When developers need to provide an example like `// [!code xxx]`,
6
+ * they can use `// [\!code xxx]` to avoid being processed by `shiki`.
7
+ * After `shiki` completes the processing,
8
+ * replace `[\!code` back with `[!code` to display the correct text.
9
+ */
10
+ export declare const removeEscapeTransformer: ShikiTransformer;
11
+ export declare const emptyLineTransformer: ShikiTransformer;
@@ -0,0 +1,39 @@
1
+ const CODE_ESCAPE_RE = /\[\\!code/g;
2
+ export const addClassTransformer = {
3
+ name: 'vuepress:add-class',
4
+ pre(node) {
5
+ this.addClassToHast(node, 'vp-code');
6
+ },
7
+ };
8
+ /**
9
+ * This `transformer` is primarily for the usage instructions of themes.
10
+ * When developers need to provide an example like `// [!code xxx]`,
11
+ * they can use `// [\!code xxx]` to avoid being processed by `shiki`.
12
+ * After `shiki` completes the processing,
13
+ * replace `[\!code` back with `[!code` to display the correct text.
14
+ */
15
+ export const removeEscapeTransformer = {
16
+ name: 'vuepress:remove-escape',
17
+ postprocess(code) {
18
+ return code.replace(CODE_ESCAPE_RE, '[!code');
19
+ },
20
+ };
21
+ export const emptyLineTransformer = {
22
+ name: 'vuepress:empty-line',
23
+ code(hast) {
24
+ hast.children.forEach((span) => {
25
+ if (span.type === 'element' &&
26
+ span.tagName === 'span' &&
27
+ Array.isArray(span.properties.class) &&
28
+ span.properties.class.includes('line') &&
29
+ span.children.length === 0) {
30
+ span.children.push({
31
+ type: 'element',
32
+ tagName: 'wbr',
33
+ properties: {},
34
+ children: [],
35
+ });
36
+ }
37
+ });
38
+ },
39
+ };
@@ -0,0 +1,98 @@
1
+ import type { BundledLanguage, BundledTheme, Highlighter, LanguageInput, ShikiTransformer, SpecialLanguage, StringLiteralUnion, ThemeRegistration, ThemeRegistrationRaw } from 'shiki';
2
+ export type ShikiLang = LanguageInput | StringLiteralUnion<BundledLanguage> | SpecialLanguage;
3
+ export type ShikiTheme = ThemeRegistration | ThemeRegistrationRaw | StringLiteralUnion<BundledTheme>;
4
+ export interface ShikiHighlightOptions {
5
+ /**
6
+ * Languages to be loaded.
7
+ *
8
+ * Shiki does not preload any languages to avoid extra overhead. So you need
9
+ * to specify the languages you want to use.
10
+ *
11
+ * @see https://shiki.style/languages
12
+ */
13
+ langs?: ShikiLang[];
14
+ /**
15
+ * Fallback language when the specified language is not available.
16
+ */
17
+ defaultHighlightLang?: string;
18
+ /**
19
+ * The single theme to use
20
+ *
21
+ * @see https://shiki.style/themes
22
+ */
23
+ theme?: ShikiTheme;
24
+ /**
25
+ * The dark and light themes to use
26
+ *
27
+ * @see https://shiki.style/themes
28
+ */
29
+ themes?: {
30
+ dark: ShikiTheme;
31
+ light: ShikiTheme;
32
+ };
33
+ /**
34
+ * Function to customize Shiki highlighter instance.
35
+ */
36
+ shikiSetup?: (shiki: Highlighter) => void | Promise<void>;
37
+ transformers?: ShikiTransformer[];
38
+ /**
39
+ * The default theme applied to the code (via inline color style).
40
+ * The rest of the themes are applied via CSS variables, and toggled by CSS overrides.
41
+ */
42
+ defaultColor?: false | StringLiteralUnion<'light' | 'dark'>;
43
+ /**
44
+ * Enable highlight lines or not
45
+ *
46
+ * @default true
47
+ */
48
+ highlightLines?: boolean;
49
+ /**
50
+ * Enable notation diff transformer
51
+ *
52
+ * @default false
53
+ *
54
+ * @see https://shiki.style/packages/transformers#transformernotationdiff
55
+ */
56
+ notationDiff?: boolean;
57
+ /**
58
+ * Enable notation focus transformer
59
+ *
60
+ * @default false
61
+ *
62
+ * @see https://shiki.style/packages/transformers#transformernotationfocus
63
+ */
64
+ notationFocus?: boolean;
65
+ /**
66
+ * Enable notation highlight transformer
67
+ *
68
+ * @default false
69
+ *
70
+ * @see https://shiki.style/packages/transformers#transformernotationhighlight
71
+ */
72
+ notationHighlight?: boolean;
73
+ /**
74
+ * Enable notation error level transformer
75
+ *
76
+ * @default false
77
+ *
78
+ * @see https://shiki.style/packages/transformers#transformernotationerrorlevel
79
+ */
80
+ notationErrorLevel?: boolean;
81
+ }
82
+ export interface LineNumberOptions {
83
+ /**
84
+ * Show line numbers in code blocks
85
+ * @default true
86
+ */
87
+ lineNumbers?: boolean | number;
88
+ }
89
+ export interface PreWrapperOptions {
90
+ /**
91
+ * Wrap the `<pre>` tag with an extra `<div>` or not. Do not disable it unless you
92
+ * understand what's it for
93
+ *
94
+ * - Required for `lineNumbers`
95
+ * - Required for title display of default theme
96
+ */
97
+ preWrapper?: boolean;
98
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import type { TransformerCompactLineOption } from '@shikijs/transformers';
2
+ export declare const nanoid: (size?: number | undefined) => string;
3
+ export declare const resolveLanguage: (info: string) => string;
4
+ export declare const resolveAttr: (info: string, attr: string) => string | null;
5
+ /**
6
+ * 2 steps:
7
+ *
8
+ * 1. convert attrs into line numbers:
9
+ * {4,7-13,16,23-27,40} -> [4,7,8,9,10,11,12,13,16,23,24,25,26,27,40]
10
+ * 2. convert line numbers into line options:
11
+ * [{ line: number, classes: string[] }]
12
+ */
13
+ export declare const attrsToLines: (attrs: string) => TransformerCompactLineOption[];
@@ -0,0 +1,45 @@
1
+ import { customAlphabet } from 'nanoid';
2
+ const VUE_RE = /-vue$/;
3
+ export const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10);
4
+ export const resolveLanguage = (info) => info
5
+ .match(/^([^ :[{]+)/)?.[1]
6
+ ?.replace(VUE_RE, '')
7
+ .toLowerCase() ?? '';
8
+ export const resolveAttr = (info, attr) => {
9
+ // try to match specified attr mark
10
+ const pattern = `\\b${attr}\\s*=\\s*(?<quote>['"])(?<content>.+?)\\k<quote>(\\s|$)`;
11
+ const regex = new RegExp(pattern, 'i');
12
+ const match = info.match(regex);
13
+ // return content if matched, null if not specified
14
+ return match?.groups?.content ?? null;
15
+ };
16
+ /**
17
+ * 2 steps:
18
+ *
19
+ * 1. convert attrs into line numbers:
20
+ * {4,7-13,16,23-27,40} -> [4,7,8,9,10,11,12,13,16,23,24,25,26,27,40]
21
+ * 2. convert line numbers into line options:
22
+ * [{ line: number, classes: string[] }]
23
+ */
24
+ export const attrsToLines = (attrs) => {
25
+ attrs = attrs.replace(/^(?:\[.*?\])?.*?([\d,-]+).*/, '$1').trim();
26
+ const result = [];
27
+ if (!attrs) {
28
+ return [];
29
+ }
30
+ attrs
31
+ .split(',')
32
+ .map((v) => v.split('-').map((v) => parseInt(v, 10)))
33
+ .forEach(([start, end]) => {
34
+ if (start && end) {
35
+ result.push(...Array.from({ length: end - start + 1 }, (_, i) => start + i));
36
+ }
37
+ else {
38
+ result.push(start);
39
+ }
40
+ });
41
+ return result.map((line) => ({
42
+ line,
43
+ classes: ['highlighted'],
44
+ }));
45
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vuepress/plugin-shiki",
3
- "version": "2.0.0-rc.3",
3
+ "version": "2.0.0-rc.31",
4
4
  "description": "VuePress plugin - shiki",
5
5
  "keywords": [
6
6
  "vuepress-plugin",
@@ -9,7 +9,7 @@
9
9
  "shiki",
10
10
  "highlight"
11
11
  ],
12
- "homepage": "https://github.com/vuepress",
12
+ "homepage": "https://ecosystem.vuejs.press/plugins/shiki.html",
13
13
  "bugs": {
14
14
  "url": "https://github.com/vuepress/ecosystem/issues"
15
15
  },
@@ -35,13 +35,19 @@
35
35
  "clean": "rimraf --glob ./lib ./*.tsbuildinfo"
36
36
  },
37
37
  "dependencies": {
38
- "shikiji": "^0.10.2"
38
+ "@shikijs/transformers": "^1.6.0",
39
+ "nanoid": "^5.0.7",
40
+ "shiki": "^1.6.0"
39
41
  },
40
42
  "peerDependencies": {
41
- "vuepress": "2.0.0-rc.2"
43
+ "vuepress": "2.0.0-rc.12"
42
44
  },
43
45
  "publishConfig": {
44
46
  "access": "public"
45
47
  },
46
- "gitHead": "7247c765f50c3b2d5a622e0f7cf22bfb13a463c4"
48
+ "devDependencies": {
49
+ "@types/markdown-it": "^14.1.1",
50
+ "markdown-it": "^14.1.0"
51
+ },
52
+ "gitHead": "c3d22eb10914f5a6bbb9cfb039498761ca94b524"
47
53
  }