@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.
Files changed (36) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/code/highlight.js +58 -38
  3. package/dist/code/index.js +1 -5
  4. package/dist/code/line-numbers.js +76 -51
  5. package/dist/constants.js +10 -12
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +5 -14
  9. package/dist/languages/basic.js +28 -31
  10. package/dist/languages/index.js +3 -9
  11. package/dist/languages/json-yaml.js +7 -10
  12. package/dist/languages/standard.js +115 -100
  13. package/dist/markdown/index.d.ts +1 -1
  14. package/dist/markdown/index.d.ts.map +1 -1
  15. package/dist/markdown/index.js +1 -2
  16. package/dist/markdown/markdown.js +148 -100
  17. package/dist/rehype-alert/index.js +1 -5
  18. package/dist/rehype-alert/rehype-alert.js +99 -91
  19. package/dist/rehype-highlight/index.js +1 -5
  20. package/dist/rehype-highlight/rehype-highlight.js +93 -85
  21. package/package.json +11 -17
  22. package/dist/code/highlight.js.map +0 -7
  23. package/dist/code/index.js.map +0 -7
  24. package/dist/code/line-numbers.js.map +0 -7
  25. package/dist/constants.js.map +0 -7
  26. package/dist/index.js.map +0 -7
  27. package/dist/languages/basic.js.map +0 -7
  28. package/dist/languages/index.js.map +0 -7
  29. package/dist/languages/json-yaml.js.map +0 -7
  30. package/dist/languages/standard.js.map +0 -7
  31. package/dist/markdown/index.js.map +0 -7
  32. package/dist/markdown/markdown.js.map +0 -7
  33. package/dist/rehype-alert/index.js.map +0 -7
  34. package/dist/rehype-alert/rehype-alert.js.map +0 -7
  35. package/dist/rehype-highlight/index.js.map +0 -7
  36. package/dist/rehype-highlight/rehype-highlight.js.map +0 -7
@@ -1,101 +1,116 @@
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
- import curl from "highlightjs-curl";
47
- const standardLanguages = {
48
- bash,
49
- c,
50
- clojure,
51
- cpp,
52
- csharp,
53
- css,
54
- curl,
55
- dart,
56
- diff,
57
- docker: dockerfile,
58
- dockerfile,
59
- elixir,
60
- fsharp,
61
- go,
62
- graphql,
63
- haskell,
64
- html: xml,
65
- http,
66
- ini,
67
- java,
68
- javascript,
69
- json,
70
- kotlin,
71
- less,
72
- lua,
73
- makefile,
74
- markdown,
75
- matlab,
76
- nginx,
77
- objectivec,
78
- ocaml,
79
- perl,
80
- php,
81
- plaintext,
82
- powershell,
83
- properties,
84
- python,
85
- r,
86
- ruby,
87
- rust,
88
- scala,
89
- scss,
90
- shell,
91
- sql,
92
- swift,
93
- toml: ini,
94
- typescript,
95
- xml,
96
- yaml
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
@@ -1,2 +1,2 @@
1
- export * from './markdown.js';
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,cAAc,YAAY,CAAA"}
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"}
@@ -1,2 +1 @@
1
- export * from "./markdown.js";
2
- //# sourceMappingURL=index.js.map
1
+ export { getHeadings, htmlFromMarkdown, isHeading, splitContent, textFromNode } from './markdown.js';
@@ -1,114 +1,162 @@
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
- const isHeading = (node) => {
16
- return node.type === "heading" && "depth" in node && "children" in node;
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
- if (!options?.transform || !options?.type) {
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
- function htmlFromMarkdown(markdown, options) {
29
- const removeTags = options?.removeTags ?? [];
30
- const tagNames = [...defaultSchema.tagNames ?? [], ...options?.allowTags ?? []].filter(
31
- (t) => !removeTags.includes(t)
32
- );
33
- const html = unified().use(remarkParse).use(remarkGfm).use(transformNodes, {
34
- transform: options?.transform,
35
- type: options?.transformType
36
- }).use(remarkRehype, { allowDangerousHtml: true }).use(rehypeAlert).use(rehypeRaw).use(rehypeSanitize, {
37
- ...defaultSchema,
38
- // Don't prefix the heading ids
39
- clobberPrefix: "",
40
- // Makes it even more strict
41
- tagNames,
42
- attributes: {
43
- ...defaultSchema.attributes,
44
- abbr: ["title"],
45
- // Allow all class names while preserving the existing default attributes
46
- "*": [...defaultSchema.attributes?.["*"] ?? [], "className"]
47
- },
48
- // Strip content of dangerous elements, not just the tags
49
- strip: ["script", "style", "object", "embed", "form"]
50
- }).use(rehypeHighlight, {
51
- languages: standardLanguages,
52
- // Enable auto detection
53
- detect: true
54
- }).use(rehypeExternalLinks, { target: "_blank" }).use(rehypeFormat).use(rehypeStringify).processSync(markdown);
55
- return html.toString();
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
- return unified().use(remarkParse).use(remarkGfm).parse(markdown);
92
+ return unified().use(remarkParse).use(remarkGfm).parse(markdown);
59
93
  }
60
- function getHeadings(markdown, depth = 1) {
61
- const tree = getMarkdownAst(markdown);
62
- const nodes = [];
63
- visit(tree, "heading", (node) => {
64
- const text = textFromNode(node);
65
- if (text) {
66
- nodes.push({ depth: node.depth ?? depth, value: text });
67
- }
68
- });
69
- return nodes;
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
- function textFromNode(node) {
72
- if (node.type === "text") {
73
- return node.value ?? "";
74
- }
75
- if ("children" in node && Array.isArray(node.children)) {
76
- return node.children.map((child) => textFromNode(child)).join("");
77
- }
78
- return "";
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
- function splitContent(markdown) {
81
- const tree = getMarkdownAst(markdown);
82
- const sections = [];
83
- let nodes = [];
84
- tree.children?.forEach((node) => {
85
- if (node.type === "heading") {
86
- if (nodes.length) {
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
- const markdown = unified().use(remarkStringify).use(remarkGfm).stringify({
102
- type: "root",
103
- children: nodes
104
- });
105
- return markdown.trim();
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
@@ -1,5 +1 @@
1
- import { rehypeAlert } from "./rehype-alert.js";
2
- export {
3
- rehypeAlert
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { rehypeAlert } from './rehype-alert.js';