@shikijs/rehype 1.0.0-beta.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/LICENSE +22 -0
- package/README.md +9 -0
- package/dist/core.d.mts +52 -0
- package/dist/core.d.ts +52 -0
- package/dist/core.mjs +79 -0
- package/dist/index.d.mts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.mjs +25 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Pine Wu
|
|
4
|
+
Copyright (c) 2023 Anthony Fu <https://github.com/antfu>
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/core.d.mts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { CodeOptionsThemes, TransformerOptions, CodeOptionsMeta, HighlighterGeneric } from 'shiki/core';
|
|
2
|
+
import { Element, Root } from 'hast';
|
|
3
|
+
import { BuiltinTheme } from 'shiki';
|
|
4
|
+
import { Plugin } from 'unified';
|
|
5
|
+
|
|
6
|
+
interface MapLike<K = any, V = any> {
|
|
7
|
+
get(key: K): V | undefined;
|
|
8
|
+
set(key: K, value: V): this;
|
|
9
|
+
}
|
|
10
|
+
interface RehypeShikiExtraOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Add `highlighted` class to lines defined in after codeblock
|
|
13
|
+
*
|
|
14
|
+
* @deprecated Use [transformerNotationHighlight](https://shiki.style/packages/transformers#transformernotationhighlight) instead
|
|
15
|
+
* @default false
|
|
16
|
+
*/
|
|
17
|
+
highlightLines?: boolean | string;
|
|
18
|
+
/**
|
|
19
|
+
* Add `language-*` class to code element
|
|
20
|
+
*
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
23
|
+
addLanguageClass?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Custom meta string parser
|
|
26
|
+
* Return an object to merge with `meta`
|
|
27
|
+
*/
|
|
28
|
+
parseMetaString?: (metaString: string, node: Element, tree: Root) => Record<string, any> | undefined | null;
|
|
29
|
+
/**
|
|
30
|
+
* Custom map to cache transformed codeToHast result
|
|
31
|
+
*
|
|
32
|
+
* @default undefined
|
|
33
|
+
*/
|
|
34
|
+
cache?: MapLike;
|
|
35
|
+
/**
|
|
36
|
+
* Chance to handle the error
|
|
37
|
+
* If not provided, the error will be thrown
|
|
38
|
+
*/
|
|
39
|
+
onError?: (error: unknown) => void;
|
|
40
|
+
}
|
|
41
|
+
type RehypeShikiCoreOptions = CodeOptionsThemes<BuiltinTheme> & TransformerOptions & CodeOptionsMeta & RehypeShikiExtraOptions;
|
|
42
|
+
declare module 'hast' {
|
|
43
|
+
interface Data {
|
|
44
|
+
meta?: string;
|
|
45
|
+
}
|
|
46
|
+
interface Properties {
|
|
47
|
+
metastring?: string;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
declare const rehypeShikiFromHighlighter: Plugin<[HighlighterGeneric<any, any>, RehypeShikiCoreOptions], Root>;
|
|
51
|
+
|
|
52
|
+
export { type MapLike, type RehypeShikiCoreOptions, type RehypeShikiExtraOptions, rehypeShikiFromHighlighter as default };
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { CodeOptionsThemes, TransformerOptions, CodeOptionsMeta, HighlighterGeneric } from 'shiki/core';
|
|
2
|
+
import { Element, Root } from 'hast';
|
|
3
|
+
import { BuiltinTheme } from 'shiki';
|
|
4
|
+
import { Plugin } from 'unified';
|
|
5
|
+
|
|
6
|
+
interface MapLike<K = any, V = any> {
|
|
7
|
+
get(key: K): V | undefined;
|
|
8
|
+
set(key: K, value: V): this;
|
|
9
|
+
}
|
|
10
|
+
interface RehypeShikiExtraOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Add `highlighted` class to lines defined in after codeblock
|
|
13
|
+
*
|
|
14
|
+
* @deprecated Use [transformerNotationHighlight](https://shiki.style/packages/transformers#transformernotationhighlight) instead
|
|
15
|
+
* @default false
|
|
16
|
+
*/
|
|
17
|
+
highlightLines?: boolean | string;
|
|
18
|
+
/**
|
|
19
|
+
* Add `language-*` class to code element
|
|
20
|
+
*
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
23
|
+
addLanguageClass?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Custom meta string parser
|
|
26
|
+
* Return an object to merge with `meta`
|
|
27
|
+
*/
|
|
28
|
+
parseMetaString?: (metaString: string, node: Element, tree: Root) => Record<string, any> | undefined | null;
|
|
29
|
+
/**
|
|
30
|
+
* Custom map to cache transformed codeToHast result
|
|
31
|
+
*
|
|
32
|
+
* @default undefined
|
|
33
|
+
*/
|
|
34
|
+
cache?: MapLike;
|
|
35
|
+
/**
|
|
36
|
+
* Chance to handle the error
|
|
37
|
+
* If not provided, the error will be thrown
|
|
38
|
+
*/
|
|
39
|
+
onError?: (error: unknown) => void;
|
|
40
|
+
}
|
|
41
|
+
type RehypeShikiCoreOptions = CodeOptionsThemes<BuiltinTheme> & TransformerOptions & CodeOptionsMeta & RehypeShikiExtraOptions;
|
|
42
|
+
declare module 'hast' {
|
|
43
|
+
interface Data {
|
|
44
|
+
meta?: string;
|
|
45
|
+
}
|
|
46
|
+
interface Properties {
|
|
47
|
+
metastring?: string;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
declare const rehypeShikiFromHighlighter: Plugin<[HighlighterGeneric<any, any>, RehypeShikiCoreOptions], Root>;
|
|
51
|
+
|
|
52
|
+
export { type MapLike, type RehypeShikiCoreOptions, type RehypeShikiExtraOptions, rehypeShikiFromHighlighter as default };
|
package/dist/core.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { addClassToHast } from 'shiki/core';
|
|
2
|
+
import { toString } from 'hast-util-to-string';
|
|
3
|
+
import { visit } from 'unist-util-visit';
|
|
4
|
+
import { transformerMetaHighlight } from '@shikijs/transformers';
|
|
5
|
+
|
|
6
|
+
const rehypeShikiFromHighlighter = function(highlighter, options) {
|
|
7
|
+
const {
|
|
8
|
+
highlightLines = false,
|
|
9
|
+
addLanguageClass = false,
|
|
10
|
+
parseMetaString,
|
|
11
|
+
cache,
|
|
12
|
+
...rest
|
|
13
|
+
} = options;
|
|
14
|
+
const prefix = "language-";
|
|
15
|
+
return function(tree) {
|
|
16
|
+
visit(tree, "element", (node, index, parent) => {
|
|
17
|
+
if (!parent || index == null || node.tagName !== "pre")
|
|
18
|
+
return;
|
|
19
|
+
const head = node.children[0];
|
|
20
|
+
if (!head || head.type !== "element" || head.tagName !== "code" || !head.properties)
|
|
21
|
+
return;
|
|
22
|
+
const classes = head.properties.className;
|
|
23
|
+
if (!Array.isArray(classes))
|
|
24
|
+
return;
|
|
25
|
+
const language = classes.find(
|
|
26
|
+
(d) => typeof d === "string" && d.startsWith(prefix)
|
|
27
|
+
);
|
|
28
|
+
if (typeof language !== "string")
|
|
29
|
+
return;
|
|
30
|
+
const code = toString(head);
|
|
31
|
+
const cachedValue = cache?.get(code);
|
|
32
|
+
if (cachedValue) {
|
|
33
|
+
parent.children.splice(index, 1, ...cachedValue);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const metaString = head.data?.meta ?? head.properties.metastring ?? "";
|
|
37
|
+
const meta = parseMetaString?.(metaString, node, tree) || {};
|
|
38
|
+
const codeOptions = {
|
|
39
|
+
...rest,
|
|
40
|
+
lang: language.slice(prefix.length),
|
|
41
|
+
meta: {
|
|
42
|
+
...rest.meta,
|
|
43
|
+
...meta,
|
|
44
|
+
__raw: metaString
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
if (addLanguageClass) {
|
|
48
|
+
codeOptions.transformers || (codeOptions.transformers = []);
|
|
49
|
+
codeOptions.transformers.push({
|
|
50
|
+
name: "rehype-shiki:code-language-class",
|
|
51
|
+
code(node2) {
|
|
52
|
+
addClassToHast(node2, language);
|
|
53
|
+
return node2;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (highlightLines && typeof metaString === "string") {
|
|
58
|
+
codeOptions.transformers || (codeOptions.transformers = []);
|
|
59
|
+
codeOptions.transformers.push(
|
|
60
|
+
transformerMetaHighlight({
|
|
61
|
+
className: highlightLines === true ? "highlighted" : highlightLines
|
|
62
|
+
})
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
const fragment = highlighter.codeToHast(code, codeOptions);
|
|
67
|
+
cache?.set(code, fragment.children);
|
|
68
|
+
parent.children.splice(index, 1, ...fragment.children);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (options.onError)
|
|
71
|
+
options.onError(error);
|
|
72
|
+
else
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export { rehypeShikiFromHighlighter as default };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { LanguageInput } from 'shiki/core';
|
|
2
|
+
import { BuiltinLanguage } from 'shiki';
|
|
3
|
+
import { Plugin } from 'unified';
|
|
4
|
+
import { Root } from 'hast';
|
|
5
|
+
import { RehypeShikiCoreOptions } from './core.mjs';
|
|
6
|
+
|
|
7
|
+
type RehypeShikiOptions = RehypeShikiCoreOptions & {
|
|
8
|
+
/**
|
|
9
|
+
* Language names to include.
|
|
10
|
+
*
|
|
11
|
+
* @default Object.keys(bundledLanguages)
|
|
12
|
+
*/
|
|
13
|
+
langs?: Array<LanguageInput | BuiltinLanguage>;
|
|
14
|
+
};
|
|
15
|
+
declare const rehypeShiki: Plugin<[RehypeShikiOptions], Root>;
|
|
16
|
+
|
|
17
|
+
export { type RehypeShikiOptions, rehypeShiki as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { LanguageInput } from 'shiki/core';
|
|
2
|
+
import { BuiltinLanguage } from 'shiki';
|
|
3
|
+
import { Plugin } from 'unified';
|
|
4
|
+
import { Root } from 'hast';
|
|
5
|
+
import { RehypeShikiCoreOptions } from './core.js';
|
|
6
|
+
|
|
7
|
+
type RehypeShikiOptions = RehypeShikiCoreOptions & {
|
|
8
|
+
/**
|
|
9
|
+
* Language names to include.
|
|
10
|
+
*
|
|
11
|
+
* @default Object.keys(bundledLanguages)
|
|
12
|
+
*/
|
|
13
|
+
langs?: Array<LanguageInput | BuiltinLanguage>;
|
|
14
|
+
};
|
|
15
|
+
declare const rehypeShiki: Plugin<[RehypeShikiOptions], Root>;
|
|
16
|
+
|
|
17
|
+
export { type RehypeShikiOptions, rehypeShiki as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { bundledLanguages, getHighlighter } from 'shiki';
|
|
2
|
+
import rehypeShikiFromHighlighter from './core.mjs';
|
|
3
|
+
import 'shiki/core';
|
|
4
|
+
import 'hast-util-to-string';
|
|
5
|
+
import 'unist-util-visit';
|
|
6
|
+
import '@shikijs/transformers';
|
|
7
|
+
|
|
8
|
+
const rehypeShiki = function(options = {}) {
|
|
9
|
+
const themeNames = ("themes" in options ? Object.values(options.themes) : [options.theme]).filter(Boolean);
|
|
10
|
+
const langs = options.langs || Object.keys(bundledLanguages);
|
|
11
|
+
const ctx = this;
|
|
12
|
+
let promise;
|
|
13
|
+
return async function(tree) {
|
|
14
|
+
if (!promise) {
|
|
15
|
+
promise = getHighlighter({
|
|
16
|
+
themes: themeNames,
|
|
17
|
+
langs
|
|
18
|
+
}).then((highlighter) => rehypeShikiFromHighlighter.call(ctx, highlighter, options));
|
|
19
|
+
}
|
|
20
|
+
const handler = await promise;
|
|
21
|
+
return handler(tree);
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { rehypeShiki as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shikijs/rehype",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0-beta.0",
|
|
5
|
+
"description": "rehype integration for shiki",
|
|
6
|
+
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/shikijs/shiki#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/shikijs/shiki.git",
|
|
12
|
+
"directory": "packages/rehype"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/shikijs/shiki/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"shiki",
|
|
17
|
+
"rehype"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.mts",
|
|
23
|
+
"default": "./dist/index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./core": {
|
|
26
|
+
"types": "./dist/core.d.mts",
|
|
27
|
+
"default": "./dist/core.mjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/index.mjs",
|
|
31
|
+
"module": "./dist/index.mjs",
|
|
32
|
+
"types": "./dist/index.d.mts",
|
|
33
|
+
"typesVersions": {
|
|
34
|
+
"*": {
|
|
35
|
+
"core": [
|
|
36
|
+
"./dist/core.d.mts"
|
|
37
|
+
],
|
|
38
|
+
"*": [
|
|
39
|
+
"./dist/*",
|
|
40
|
+
"./*"
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist"
|
|
46
|
+
],
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@types/hast": "^3.0.3",
|
|
49
|
+
"hast-util-to-string": "^3.0.0",
|
|
50
|
+
"unified": "^11.0.4",
|
|
51
|
+
"unist-util-visit": "^5.0.0",
|
|
52
|
+
"@shikijs/transformers": "1.0.0-beta.0",
|
|
53
|
+
"shiki": "1.0.0-beta.0"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"rehype-raw": "^7.0.0",
|
|
57
|
+
"rehype-stringify": "^10.0.0",
|
|
58
|
+
"remark-parse": "^11.0.0",
|
|
59
|
+
"remark-rehype": "^11.1.0"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "unbuild",
|
|
63
|
+
"dev": "unbuild --stub"
|
|
64
|
+
}
|
|
65
|
+
}
|