@scalar/code-highlight 0.3.0 → 0.3.2
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 +12 -0
- package/dist/code/highlight.js +58 -38
- package/dist/code/index.js +1 -5
- package/dist/code/line-numbers.js +76 -51
- package/dist/constants.js +10 -12
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -14
- package/dist/languages/basic.js +28 -31
- package/dist/languages/index.js +3 -9
- package/dist/languages/json-yaml.js +7 -10
- package/dist/languages/standard.js +115 -100
- package/dist/markdown/index.d.ts +1 -1
- package/dist/markdown/index.d.ts.map +1 -1
- package/dist/markdown/index.js +1 -2
- package/dist/markdown/markdown.js +148 -100
- package/dist/rehype-alert/index.js +1 -5
- package/dist/rehype-alert/rehype-alert.js +99 -91
- package/dist/rehype-highlight/index.js +1 -5
- package/dist/rehype-highlight/rehype-highlight.js +93 -85
- package/package.json +11 -17
- package/dist/code/highlight.js.map +0 -7
- package/dist/code/index.js.map +0 -7
- package/dist/code/line-numbers.js.map +0 -7
- package/dist/constants.js.map +0 -7
- package/dist/index.js.map +0 -7
- package/dist/languages/basic.js.map +0 -7
- package/dist/languages/index.js.map +0 -7
- package/dist/languages/json-yaml.js.map +0 -7
- package/dist/languages/standard.js.map +0 -7
- package/dist/markdown/index.js.map +0 -7
- package/dist/markdown/markdown.js.map +0 -7
- package/dist/rehype-alert/index.js.map +0 -7
- package/dist/rehype-alert/rehype-alert.js.map +0 -7
- package/dist/rehype-highlight/index.js.map +0 -7
- package/dist/rehype-highlight/rehype-highlight.js.map +0 -7
|
@@ -1,101 +1,116 @@
|
|
|
1
|
-
import bash from
|
|
2
|
-
import c from
|
|
3
|
-
import clojure from
|
|
4
|
-
import cpp from
|
|
5
|
-
import csharp from
|
|
6
|
-
import css from
|
|
7
|
-
import dart from
|
|
8
|
-
import diff from
|
|
9
|
-
import dockerfile from
|
|
10
|
-
import elixir from
|
|
11
|
-
import fsharp from
|
|
12
|
-
import go from
|
|
13
|
-
import graphql from
|
|
14
|
-
import haskell from
|
|
15
|
-
import http from
|
|
16
|
-
import ini from
|
|
17
|
-
import java from
|
|
18
|
-
import javascript from
|
|
19
|
-
import json from
|
|
20
|
-
import kotlin from
|
|
21
|
-
import less from
|
|
22
|
-
import lua from
|
|
23
|
-
import makefile from
|
|
24
|
-
import markdown from
|
|
25
|
-
import matlab from
|
|
26
|
-
import nginx from
|
|
27
|
-
import objectivec from
|
|
28
|
-
import ocaml from
|
|
29
|
-
import perl from
|
|
30
|
-
import php from
|
|
31
|
-
import plaintext from
|
|
32
|
-
import powershell from
|
|
33
|
-
import properties from
|
|
34
|
-
import python from
|
|
35
|
-
import r from
|
|
36
|
-
import ruby from
|
|
37
|
-
import rust from
|
|
38
|
-
import scala from
|
|
39
|
-
import scss from
|
|
40
|
-
import shell from
|
|
41
|
-
import sql from
|
|
42
|
-
import swift from
|
|
43
|
-
import typescript from
|
|
44
|
-
import xml from
|
|
45
|
-
import yaml from
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
1
|
+
import bash from 'highlight.js/lib/languages/bash';
|
|
2
|
+
import c from 'highlight.js/lib/languages/c';
|
|
3
|
+
import clojure from 'highlight.js/lib/languages/clojure';
|
|
4
|
+
import cpp from 'highlight.js/lib/languages/cpp';
|
|
5
|
+
import csharp from 'highlight.js/lib/languages/csharp';
|
|
6
|
+
import css from 'highlight.js/lib/languages/css';
|
|
7
|
+
import dart from 'highlight.js/lib/languages/dart';
|
|
8
|
+
import diff from 'highlight.js/lib/languages/diff';
|
|
9
|
+
import dockerfile from 'highlight.js/lib/languages/dockerfile';
|
|
10
|
+
import elixir from 'highlight.js/lib/languages/elixir';
|
|
11
|
+
import fsharp from 'highlight.js/lib/languages/fsharp';
|
|
12
|
+
import go from 'highlight.js/lib/languages/go';
|
|
13
|
+
import graphql from 'highlight.js/lib/languages/graphql';
|
|
14
|
+
import haskell from 'highlight.js/lib/languages/haskell';
|
|
15
|
+
import http from 'highlight.js/lib/languages/http';
|
|
16
|
+
import ini from 'highlight.js/lib/languages/ini';
|
|
17
|
+
import java from 'highlight.js/lib/languages/java';
|
|
18
|
+
import javascript from 'highlight.js/lib/languages/javascript';
|
|
19
|
+
import json from 'highlight.js/lib/languages/json';
|
|
20
|
+
import kotlin from 'highlight.js/lib/languages/kotlin';
|
|
21
|
+
import less from 'highlight.js/lib/languages/less';
|
|
22
|
+
import lua from 'highlight.js/lib/languages/lua';
|
|
23
|
+
import makefile from 'highlight.js/lib/languages/makefile';
|
|
24
|
+
import markdown from 'highlight.js/lib/languages/markdown';
|
|
25
|
+
import matlab from 'highlight.js/lib/languages/matlab';
|
|
26
|
+
import nginx from 'highlight.js/lib/languages/nginx';
|
|
27
|
+
import objectivec from 'highlight.js/lib/languages/objectivec';
|
|
28
|
+
import ocaml from 'highlight.js/lib/languages/ocaml';
|
|
29
|
+
import perl from 'highlight.js/lib/languages/perl';
|
|
30
|
+
import php from 'highlight.js/lib/languages/php';
|
|
31
|
+
import plaintext from 'highlight.js/lib/languages/plaintext';
|
|
32
|
+
import powershell from 'highlight.js/lib/languages/powershell';
|
|
33
|
+
import properties from 'highlight.js/lib/languages/properties';
|
|
34
|
+
import python from 'highlight.js/lib/languages/python';
|
|
35
|
+
import r from 'highlight.js/lib/languages/r';
|
|
36
|
+
import ruby from 'highlight.js/lib/languages/ruby';
|
|
37
|
+
import rust from 'highlight.js/lib/languages/rust';
|
|
38
|
+
import scala from 'highlight.js/lib/languages/scala';
|
|
39
|
+
import scss from 'highlight.js/lib/languages/scss';
|
|
40
|
+
import shell from 'highlight.js/lib/languages/shell';
|
|
41
|
+
import sql from 'highlight.js/lib/languages/sql';
|
|
42
|
+
import swift from 'highlight.js/lib/languages/swift';
|
|
43
|
+
import typescript from 'highlight.js/lib/languages/typescript';
|
|
44
|
+
import xml from 'highlight.js/lib/languages/xml';
|
|
45
|
+
import yaml from 'highlight.js/lib/languages/yaml';
|
|
46
|
+
// @ts-expect-error No types available
|
|
47
|
+
import curl from 'highlightjs-curl';
|
|
48
|
+
/**
|
|
49
|
+
* We group languages into three categories based on their popularity and usage.
|
|
50
|
+
* This helps in optimizing the bundle size by allowing users to include only
|
|
51
|
+
* the languages they need.
|
|
52
|
+
*
|
|
53
|
+
* 1. Standard Languages: These are the most popular languages that cover the
|
|
54
|
+
* majority of use cases.
|
|
55
|
+
* 2. Medium Languages: These languages are not as popular as the ones in
|
|
56
|
+
* `standardLanguages`, but still have a decent amount of users.
|
|
57
|
+
* 3. Specialized Languages: These languages are more specialized and have a
|
|
58
|
+
* smaller user base.
|
|
59
|
+
*
|
|
60
|
+
* Each category is represented as a separate object, making it easy to import
|
|
61
|
+
* only the languages you need.
|
|
62
|
+
*/
|
|
63
|
+
/**
|
|
64
|
+
* These are the most popular languages that cover the majority of use cases.
|
|
65
|
+
*/
|
|
66
|
+
export const standardLanguages = {
|
|
67
|
+
bash,
|
|
68
|
+
c,
|
|
69
|
+
clojure,
|
|
70
|
+
cpp,
|
|
71
|
+
csharp,
|
|
72
|
+
css,
|
|
73
|
+
curl,
|
|
74
|
+
dart,
|
|
75
|
+
diff,
|
|
76
|
+
docker: dockerfile,
|
|
77
|
+
dockerfile,
|
|
78
|
+
elixir,
|
|
79
|
+
fsharp,
|
|
80
|
+
go,
|
|
81
|
+
graphql,
|
|
82
|
+
haskell,
|
|
83
|
+
html: xml,
|
|
84
|
+
http,
|
|
85
|
+
ini,
|
|
86
|
+
java,
|
|
87
|
+
javascript,
|
|
88
|
+
json,
|
|
89
|
+
kotlin,
|
|
90
|
+
less,
|
|
91
|
+
lua,
|
|
92
|
+
makefile,
|
|
93
|
+
markdown,
|
|
94
|
+
matlab,
|
|
95
|
+
nginx,
|
|
96
|
+
objectivec,
|
|
97
|
+
ocaml,
|
|
98
|
+
perl,
|
|
99
|
+
php,
|
|
100
|
+
plaintext,
|
|
101
|
+
powershell,
|
|
102
|
+
properties,
|
|
103
|
+
python,
|
|
104
|
+
r,
|
|
105
|
+
ruby,
|
|
106
|
+
rust,
|
|
107
|
+
scala,
|
|
108
|
+
scss,
|
|
109
|
+
shell,
|
|
110
|
+
sql,
|
|
111
|
+
swift,
|
|
112
|
+
toml: ini,
|
|
113
|
+
typescript,
|
|
114
|
+
xml,
|
|
115
|
+
yaml,
|
|
97
116
|
};
|
|
98
|
-
export {
|
|
99
|
-
standardLanguages
|
|
100
|
-
};
|
|
101
|
-
//# sourceMappingURL=standard.js.map
|
package/dist/markdown/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { getHeadings, htmlFromMarkdown, isHeading, splitContent, textFromNode, type Node } from './markdown.js';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/markdown/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/markdown/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,SAAS,EAAE,YAAY,EAAE,YAAY,EAAE,KAAK,IAAI,EAAE,MAAM,YAAY,CAAA"}
|
package/dist/markdown/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export { getHeadings, htmlFromMarkdown, isHeading, splitContent, textFromNode } from './markdown.js';
|
|
@@ -1,114 +1,162 @@
|
|
|
1
|
-
import rehypeExternalLinks from
|
|
2
|
-
import rehypeFormat from
|
|
3
|
-
import rehypeRaw from
|
|
4
|
-
import rehypeSanitize, { defaultSchema } from
|
|
5
|
-
import rehypeStringify from
|
|
6
|
-
import remarkGfm from
|
|
7
|
-
import remarkParse from
|
|
8
|
-
import remarkRehype from
|
|
9
|
-
import remarkStringify from
|
|
10
|
-
import { unified } from
|
|
11
|
-
import { SKIP, visit } from
|
|
12
|
-
import { standardLanguages } from
|
|
13
|
-
import { rehypeAlert } from
|
|
14
|
-
import { rehypeHighlight } from
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
import rehypeExternalLinks from 'rehype-external-links';
|
|
2
|
+
import rehypeFormat from 'rehype-format';
|
|
3
|
+
import rehypeRaw from 'rehype-raw';
|
|
4
|
+
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
|
|
5
|
+
import rehypeStringify from 'rehype-stringify';
|
|
6
|
+
import remarkGfm from 'remark-gfm';
|
|
7
|
+
import remarkParse from 'remark-parse';
|
|
8
|
+
import remarkRehype from 'remark-rehype';
|
|
9
|
+
import remarkStringify from 'remark-stringify';
|
|
10
|
+
import { unified } from 'unified';
|
|
11
|
+
import { SKIP, visit } from 'unist-util-visit';
|
|
12
|
+
import { standardLanguages } from '../languages/index.js';
|
|
13
|
+
import { rehypeAlert } from '../rehype-alert/index.js';
|
|
14
|
+
import { rehypeHighlight } from '../rehype-highlight/index.js';
|
|
15
|
+
/**
|
|
16
|
+
* Type-guard to check if a node is a heading.
|
|
17
|
+
*/
|
|
18
|
+
export const isHeading = (node) => {
|
|
19
|
+
return node.type === 'heading' && 'depth' in node && 'children' in node;
|
|
17
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Plugin to transform nodes in a Markdown AST
|
|
23
|
+
*/
|
|
18
24
|
const transformNodes = (options, ..._ignored) => (tree) => {
|
|
19
|
-
|
|
25
|
+
if (!options?.transform || !options?.type) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
visit(tree, options?.type, (node) => {
|
|
29
|
+
options?.transform ? options?.transform(node) : node;
|
|
30
|
+
return SKIP;
|
|
31
|
+
});
|
|
20
32
|
return;
|
|
21
|
-
}
|
|
22
|
-
visit(tree, options?.type, (node) => {
|
|
23
|
-
options?.transform ? options?.transform(node) : node;
|
|
24
|
-
return SKIP;
|
|
25
|
-
});
|
|
26
|
-
return;
|
|
27
33
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Take a Markdown string and generate HTML from it
|
|
36
|
+
*/
|
|
37
|
+
export function htmlFromMarkdown(markdown, options) {
|
|
38
|
+
// Add permitted tags and remove stripped ones
|
|
39
|
+
const removeTags = options?.removeTags ?? [];
|
|
40
|
+
const tagNames = [...(defaultSchema.tagNames ?? []), ...(options?.allowTags ?? [])].filter((t) => !removeTags.includes(t));
|
|
41
|
+
const html = unified()
|
|
42
|
+
// Parses markdown
|
|
43
|
+
.use(remarkParse)
|
|
44
|
+
// Support autolink literals, footnotes, strikethrough, tables and tasklists
|
|
45
|
+
.use(remarkGfm)
|
|
46
|
+
.use(transformNodes, {
|
|
47
|
+
transform: options?.transform,
|
|
48
|
+
type: options?.transformType,
|
|
49
|
+
})
|
|
50
|
+
// Allows any HTML tags
|
|
51
|
+
.use(remarkRehype, { allowDangerousHtml: true })
|
|
52
|
+
// Adds GitHub alerts
|
|
53
|
+
.use(rehypeAlert)
|
|
54
|
+
// Creates a HTML AST
|
|
55
|
+
.use(rehypeRaw)
|
|
56
|
+
// Removes disallowed tags
|
|
57
|
+
.use(rehypeSanitize, {
|
|
58
|
+
...defaultSchema,
|
|
59
|
+
// Don't prefix the heading ids
|
|
60
|
+
clobberPrefix: '',
|
|
61
|
+
// Makes it even more strict
|
|
62
|
+
tagNames,
|
|
63
|
+
attributes: {
|
|
64
|
+
...defaultSchema.attributes,
|
|
65
|
+
abbr: ['title'],
|
|
66
|
+
// Allow all class names while preserving the existing default attributes
|
|
67
|
+
'*': [...(defaultSchema.attributes?.['*'] ?? []), 'className'],
|
|
68
|
+
},
|
|
69
|
+
// Strip content of dangerous elements, not just the tags
|
|
70
|
+
strip: ['script', 'style', 'object', 'embed', 'form'],
|
|
71
|
+
})
|
|
72
|
+
// Syntax highlighting
|
|
73
|
+
.use(rehypeHighlight, {
|
|
74
|
+
languages: standardLanguages,
|
|
75
|
+
// Enable auto detection
|
|
76
|
+
detect: true,
|
|
77
|
+
})
|
|
78
|
+
// Adds target="_blank" to external links
|
|
79
|
+
.use(rehypeExternalLinks, { target: '_blank' })
|
|
80
|
+
// Formats the HTML
|
|
81
|
+
.use(rehypeFormat)
|
|
82
|
+
// Converts the HTML AST to a string
|
|
83
|
+
.use(rehypeStringify)
|
|
84
|
+
// Run the pipeline
|
|
85
|
+
.processSync(markdown);
|
|
86
|
+
return html.toString();
|
|
56
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Create a Markdown AST from a string.
|
|
90
|
+
*/
|
|
57
91
|
function getMarkdownAst(markdown) {
|
|
58
|
-
|
|
92
|
+
return unified().use(remarkParse).use(remarkGfm).parse(markdown);
|
|
59
93
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Find all headings of a specific type in a Markdown AST.
|
|
96
|
+
*/
|
|
97
|
+
export function getHeadings(markdown, depth = 1) {
|
|
98
|
+
const tree = getMarkdownAst(markdown);
|
|
99
|
+
const nodes = [];
|
|
100
|
+
visit(tree, 'heading', (node) => {
|
|
101
|
+
const text = textFromNode(node);
|
|
102
|
+
if (text) {
|
|
103
|
+
nodes.push({ depth: node.depth ?? depth, value: text });
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return nodes;
|
|
70
107
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Extract plain text from a Markdown AST node (recursively).
|
|
110
|
+
*
|
|
111
|
+
* Handles headings with nested phrasing content such as links.
|
|
112
|
+
*/
|
|
113
|
+
export function textFromNode(node) {
|
|
114
|
+
if (node.type === 'text') {
|
|
115
|
+
return node.value ?? '';
|
|
116
|
+
}
|
|
117
|
+
if ('children' in node && Array.isArray(node.children)) {
|
|
118
|
+
return node.children.map((child) => textFromNode(child)).join('');
|
|
119
|
+
}
|
|
120
|
+
return '';
|
|
79
121
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Return multiple Markdown documents. Every heading should be its own document.
|
|
124
|
+
*/
|
|
125
|
+
export function splitContent(markdown) {
|
|
126
|
+
const tree = getMarkdownAst(markdown);
|
|
127
|
+
/** Sections */
|
|
128
|
+
const sections = [];
|
|
129
|
+
/** Nodes inside a section */
|
|
130
|
+
let nodes = [];
|
|
131
|
+
tree.children?.forEach((node) => {
|
|
132
|
+
// If the node is a heading, start a new section
|
|
133
|
+
if (node.type === 'heading') {
|
|
134
|
+
if (nodes.length) {
|
|
135
|
+
sections.push(nodes);
|
|
136
|
+
}
|
|
137
|
+
sections.push([node]);
|
|
138
|
+
nodes = [];
|
|
139
|
+
}
|
|
140
|
+
// Otherwise, add the node to the current section
|
|
141
|
+
else {
|
|
142
|
+
nodes.push(node);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
// Add any remaining nodes
|
|
146
|
+
if (nodes.length) {
|
|
87
147
|
sections.push(nodes);
|
|
88
|
-
}
|
|
89
|
-
sections.push([node]);
|
|
90
|
-
nodes = [];
|
|
91
|
-
} else {
|
|
92
|
-
nodes.push(node);
|
|
93
148
|
}
|
|
94
|
-
|
|
95
|
-
if (nodes.length) {
|
|
96
|
-
sections.push(nodes);
|
|
97
|
-
}
|
|
98
|
-
return sections.map((section) => createDocument(section));
|
|
149
|
+
return sections.map((section) => createDocument(section));
|
|
99
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Use remark to create a Markdown document from a list of nodes.
|
|
153
|
+
*/
|
|
100
154
|
function createDocument(nodes) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
155
|
+
// Create the Markdown string
|
|
156
|
+
const markdown = unified().use(remarkStringify).use(remarkGfm).stringify({
|
|
157
|
+
type: 'root',
|
|
158
|
+
children: nodes,
|
|
159
|
+
});
|
|
160
|
+
// Remove the whitespace
|
|
161
|
+
return markdown.trim();
|
|
106
162
|
}
|
|
107
|
-
export {
|
|
108
|
-
getHeadings,
|
|
109
|
-
htmlFromMarkdown,
|
|
110
|
-
isHeading,
|
|
111
|
-
splitContent,
|
|
112
|
-
textFromNode
|
|
113
|
-
};
|
|
114
|
-
//# sourceMappingURL=markdown.js.map
|