@scalar/code-highlight 0.3.4 → 0.3.6
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 +13 -0
- package/dist/chunks/code.js +124 -0
- package/dist/chunks/code.js.map +1 -0
- package/dist/chunks/languages.js +12762 -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 +20922 -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 +2989 -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/css/code.css +0 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6 -5
- package/dist/languages/basic.d.ts.map +1 -1
- package/dist/languages/index.d.ts +3 -3
- package/dist/languages/index.js +2 -3
- package/dist/languages/python.d.ts +22 -0
- package/dist/languages/python.d.ts.map +1 -0
- package/dist/languages/standard.d.ts.map +1 -1
- package/dist/markdown/index.d.ts +1 -1
- package/dist/markdown/index.js +3 -1
- package/dist/markdown/markdown.d.ts.map +1 -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/dist/rehype-highlight/rehype-highlight.d.ts +2 -0
- package/dist/rehype-highlight/rehype-highlight.d.ts.map +1 -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,102 +0,0 @@
|
|
|
1
|
-
import { visit } from 'unist-util-visit';
|
|
2
|
-
const ALERT_TYPES = ['note', 'tip', 'important', 'warning', 'caution', 'success'];
|
|
3
|
-
// Simple whitespace check function
|
|
4
|
-
function isWhitespace(node) {
|
|
5
|
-
return node.type === 'text' && typeof node.value === 'string' && /^\s*$/.test(node.value);
|
|
6
|
-
}
|
|
7
|
-
export function rehypeAlert() {
|
|
8
|
-
return (tree) => {
|
|
9
|
-
visit(tree, 'element', (node, index, parent) => {
|
|
10
|
-
if (node.tagName !== 'blockquote' || typeof index !== 'number' || !parent || parent.type !== 'root') {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
// Find the first non-whitespace child
|
|
14
|
-
const headIndex = node.children.findIndex((child) => !isWhitespace(child));
|
|
15
|
-
if (headIndex === -1) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
const head = node.children[headIndex];
|
|
19
|
-
if (!head || head.type !== 'element' || head.tagName !== 'p') {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
const text = head.children[0];
|
|
23
|
-
if (!text || text.type !== 'text' || !text.value.startsWith('[!')) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
const end = text.value.indexOf(']');
|
|
27
|
-
if (end === -1) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
// Extract the alert type
|
|
31
|
-
const alertType = text.value.slice(2, end).toLowerCase();
|
|
32
|
-
if (!ALERT_TYPES.includes(alertType)) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
// Remove the blockquote if it's empty
|
|
36
|
-
if (end + 1 === text.value.length) {
|
|
37
|
-
const next = head.children[1];
|
|
38
|
-
if (next) {
|
|
39
|
-
if (next.type !== 'element' || next.tagName !== 'br') {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (!head.children[2]) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
head.children = head.children.slice(2);
|
|
46
|
-
const node = head.children[0];
|
|
47
|
-
if (node && node.type === 'text' && node.value.charAt(0) === '\n') {
|
|
48
|
-
node.value = node.value.slice(1);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
const skipped = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1]);
|
|
53
|
-
const nextIndex = skipped ? headIndex + 2 : headIndex + 1;
|
|
54
|
-
if (nextIndex >= node.children.length || node.children[nextIndex]?.type !== 'element') {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
node.children = node.children.slice(nextIndex);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
else if (text.value.charAt(end + 1) === '\n' &&
|
|
61
|
-
// Check if the next character is a newline or a non-whitespace character
|
|
62
|
-
(end + 2 === text.value.length || !/^\s*$/.test(text.value.slice(end + 2)))) {
|
|
63
|
-
text.value = text.value.slice(end + 2);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
// Remove the alert marker if it's not followed by a newline or a non-whitespace character
|
|
67
|
-
text.value = text.value.replace(/^\s*\[!.*?\]\s*/, '');
|
|
68
|
-
}
|
|
69
|
-
// Extract content from paragraphs to avoid wrapping in <p> tags
|
|
70
|
-
const contentChildren = [];
|
|
71
|
-
for (let i = headIndex; i < node.children.length; i++) {
|
|
72
|
-
const child = node.children[i];
|
|
73
|
-
if (child?.type === 'element' && child.tagName === 'p' && child.children) {
|
|
74
|
-
contentChildren.push(...child.children);
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
contentChildren.push(child);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
// Replace the blockquote with a div containing the alert
|
|
81
|
-
parent.children[index] = {
|
|
82
|
-
type: 'element',
|
|
83
|
-
tagName: 'div',
|
|
84
|
-
properties: { className: ['markdown-alert', `markdown-alert-${alertType}`] },
|
|
85
|
-
children: [
|
|
86
|
-
{
|
|
87
|
-
type: 'element',
|
|
88
|
-
tagName: 'div',
|
|
89
|
-
properties: { className: ['markdown-alert-icon'] },
|
|
90
|
-
children: [],
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
type: 'element',
|
|
94
|
-
tagName: 'div',
|
|
95
|
-
properties: { className: ['markdown-alert-content'] },
|
|
96
|
-
children: [{ type: 'text', value: ' ' }, ...contentChildren],
|
|
97
|
-
},
|
|
98
|
-
],
|
|
99
|
-
};
|
|
100
|
-
});
|
|
101
|
-
};
|
|
102
|
-
}
|
|
@@ -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
|
-
}
|