@scalar/code-highlight 0.3.4 → 0.3.5
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/CHANGELOG.md +6 -0
- package/dist/chunks/code.js +124 -0
- package/dist/chunks/code.js.map +1 -0
- package/dist/chunks/languages.js +12720 -0
- package/dist/chunks/languages.js.map +1 -0
- package/dist/chunks/lib.js +13688 -0
- package/dist/chunks/lib.js.map +1 -0
- package/dist/chunks/lib2.js +731 -0
- package/dist/chunks/lib2.js.map +1 -0
- package/dist/chunks/markdown.js +20921 -0
- package/dist/chunks/markdown.js.map +1 -0
- package/dist/chunks/rehype-alert.js +74 -0
- package/dist/chunks/rehype-alert.js.map +1 -0
- package/dist/chunks/rehype-highlight.js +2987 -0
- package/dist/chunks/rehype-highlight.js.map +1 -0
- package/dist/code/index.d.ts +1 -1
- package/dist/code/index.js +3 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6 -5
- package/dist/languages/index.d.ts +3 -3
- package/dist/languages/index.js +2 -3
- package/dist/markdown/index.d.ts +1 -1
- package/dist/markdown/index.js +3 -1
- package/dist/rehype-alert/index.d.ts +1 -1
- package/dist/rehype-alert/index.js +2 -1
- package/dist/rehype-highlight/index.d.ts +1 -1
- package/dist/rehype-highlight/index.js +2 -1
- package/package.json +4 -4
- package/dist/code/highlight.js +0 -62
- package/dist/code/line-numbers.js +0 -94
- package/dist/constants.js +0 -11
- package/dist/languages/basic.js +0 -30
- package/dist/languages/curl.js +0 -86
- package/dist/languages/json-yaml.js +0 -9
- package/dist/languages/standard.js +0 -115
- package/dist/markdown/markdown.js +0 -228
- package/dist/rehype-alert/rehype-alert.js +0 -102
- package/dist/rehype-highlight/rehype-highlight.js +0 -98
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { toText } from 'hast-util-to-text';
|
|
2
|
-
import { createLowlight } from 'lowlight';
|
|
3
|
-
import { visit } from 'unist-util-visit';
|
|
4
|
-
import { lowlightLanguageMappings } from '../constants.js';
|
|
5
|
-
const emptyOptions = {};
|
|
6
|
-
/**
|
|
7
|
-
* Lowlight syntax highlighting plugin for rehype pipelines
|
|
8
|
-
*
|
|
9
|
-
* Derived from: @url https://github.com/rehypejs/rehype-highlight/blob/main/lib/index.js
|
|
10
|
-
*/
|
|
11
|
-
export function rehypeHighlight(options) {
|
|
12
|
-
const settings = options || emptyOptions;
|
|
13
|
-
const aliases = settings.aliases;
|
|
14
|
-
const detect = options?.detect ?? false;
|
|
15
|
-
const languages = settings.languages;
|
|
16
|
-
const plainText = settings.plainText;
|
|
17
|
-
const prefix = settings.prefix;
|
|
18
|
-
const subset = settings.subset;
|
|
19
|
-
let name = 'hljs';
|
|
20
|
-
// Create a lowlight instance if not provided
|
|
21
|
-
const lowlight = options?.lowlight ?? createLowlight(languages);
|
|
22
|
-
if (aliases) {
|
|
23
|
-
lowlight.registerAlias(aliases);
|
|
24
|
-
}
|
|
25
|
-
if (prefix) {
|
|
26
|
-
const pos = prefix.indexOf('-');
|
|
27
|
-
name = pos > -1 ? prefix.slice(0, pos) : prefix;
|
|
28
|
-
}
|
|
29
|
-
/** Transform.*/
|
|
30
|
-
return (tree, file) => {
|
|
31
|
-
visit(tree, 'element', (node, _, parent) => {
|
|
32
|
-
if (node.tagName !== 'code' || !parent || parent.type !== 'element' || parent.tagName !== 'pre') {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
const lang = language(node);
|
|
36
|
-
if (lang === 'no-highlight' || (!lang && !detect) || (lang && plainText?.includes(lang))) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
if (!Array.isArray(node.properties.className)) {
|
|
40
|
-
node.properties.className = [];
|
|
41
|
-
}
|
|
42
|
-
if (!node.properties.className.includes(name)) {
|
|
43
|
-
node.properties.className.unshift(name);
|
|
44
|
-
}
|
|
45
|
-
let result;
|
|
46
|
-
try {
|
|
47
|
-
result = lang
|
|
48
|
-
? lowlight.highlight(lang, toText(parent), { prefix })
|
|
49
|
-
: lowlight.highlightAuto(toText(parent), { prefix, subset });
|
|
50
|
-
}
|
|
51
|
-
catch (error) {
|
|
52
|
-
const cause = error;
|
|
53
|
-
if (lang && /Unknown language/.test(cause.message)) {
|
|
54
|
-
file.message(`Cannot highlight as \`${lang}\`, it's not registered`, {
|
|
55
|
-
ancestors: [parent, node],
|
|
56
|
-
cause,
|
|
57
|
-
place: node.position,
|
|
58
|
-
ruleId: 'missing-language',
|
|
59
|
-
source: 'rehype-highlight',
|
|
60
|
-
});
|
|
61
|
-
/* c8 ignore next 5 -- throw arbitrary hljs errors */
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
throw cause;
|
|
65
|
-
}
|
|
66
|
-
if (!lang && result.data?.language) {
|
|
67
|
-
node.properties.className.push('language-' + result.data.language);
|
|
68
|
-
}
|
|
69
|
-
if (result.children.length > 0) {
|
|
70
|
-
node.children = result.children;
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/** Get the programming language of `node` or an empty string */
|
|
76
|
-
function language(node) {
|
|
77
|
-
const list = node.properties.className;
|
|
78
|
-
if (!Array.isArray(list)) {
|
|
79
|
-
return '';
|
|
80
|
-
}
|
|
81
|
-
const name = list.reduce((result, _item) => {
|
|
82
|
-
if (result) {
|
|
83
|
-
return result;
|
|
84
|
-
}
|
|
85
|
-
const item = String(_item);
|
|
86
|
-
if (item === 'no-highlight' || item === 'nohighlight') {
|
|
87
|
-
return 'no-highlight';
|
|
88
|
-
}
|
|
89
|
-
if (item.slice(0, 5) === 'lang-') {
|
|
90
|
-
return item.slice(5);
|
|
91
|
-
}
|
|
92
|
-
if (item.slice(0, 9) === 'language-') {
|
|
93
|
-
return item.slice(9);
|
|
94
|
-
}
|
|
95
|
-
return result;
|
|
96
|
-
}, '');
|
|
97
|
-
return lowlightLanguageMappings[name || ''] || name;
|
|
98
|
-
}
|