@scalar/code-highlight 0.2.0 → 0.2.1
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 +8 -0
- package/dist/code/highlight.d.ts.map +1 -1
- package/dist/code/highlight.js.map +2 -2
- package/dist/code/line-numbers.d.ts +1 -5
- package/dist/code/line-numbers.d.ts.map +1 -1
- package/dist/code/line-numbers.js +1 -5
- package/dist/code/line-numbers.js.map +2 -2
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +2 -2
- package/dist/languages/index.d.ts +1 -1
- package/dist/languages/index.d.ts.map +1 -1
- package/dist/languages/index.js +1 -1
- package/dist/languages/index.js.map +2 -2
- package/dist/languages/standard.d.ts +1 -15
- package/dist/languages/standard.d.ts.map +1 -1
- package/dist/languages/standard.js +1 -86
- package/dist/languages/standard.js.map +2 -2
- package/dist/markdown/markdown.d.ts.map +1 -1
- package/dist/markdown/markdown.js.map +2 -2
- package/package.json +9 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @scalar/code-highlight
|
|
2
2
|
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#7441](https://github.com/scalar/scalar/pull/7441) [`781f264`](https://github.com/scalar/scalar/commit/781f2648bc758a251435c3a1e2b126a37f20e44f) Thanks [@marcalexiei](https://github.com/marcalexiei)! - fix(code-highlight): remove `highlightjs-vue` unused dependency
|
|
8
|
+
|
|
9
|
+
- [#7441](https://github.com/scalar/scalar/pull/7441) [`781f264`](https://github.com/scalar/scalar/commit/781f2648bc758a251435c3a1e2b126a37f20e44f) Thanks [@marcalexiei](https://github.com/marcalexiei)! - fix(code-highlight): remove unused languages
|
|
10
|
+
|
|
3
11
|
## 0.2.0
|
|
4
12
|
|
|
5
13
|
### Minor Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"highlight.d.ts","sourceRoot":"","sources":["../../src/code/highlight.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAW9C;;GAEG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE;IACP,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IACrC,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CACpC,
|
|
1
|
+
{"version":3,"file":"highlight.d.ts","sourceRoot":"","sources":["../../src/code/highlight.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAW9C;;GAEG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE;IACP,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IACrC,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CACpC,UAgDF"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/code/highlight.ts"],
|
|
4
|
-
"sourcesContent": ["import type { Element, Root } from 'hast'\nimport type { LanguageFn } from 'highlight.js'\nimport rehypeParse from 'rehype-parse'\nimport rehypeStringify from 'rehype-stringify'\nimport {
|
|
5
|
-
"mappings": "AAEA,OAAO,iBAAiB;AACxB,OAAO,qBAAqB;AAC5B,
|
|
4
|
+
"sourcesContent": ["import type { Element, Root } from 'hast'\nimport type { LanguageFn } from 'highlight.js'\nimport rehypeParse from 'rehype-parse'\nimport rehypeStringify from 'rehype-stringify'\nimport { type Plugin, unified } from 'unified'\nimport { visit } from 'unist-util-visit'\n\nimport { lowlightLanguageMappings } from '@/constants'\nimport { rehypeHighlight } from '@/rehype-highlight'\n\nimport { codeBlockLinesPlugin } from './line-numbers'\n\n/**\n * Syntax highlights a code string using the `rehype-highlight` library.\n */\nexport function syntaxHighlight(\n codeString: string,\n options: {\n lang: string\n languages: Record<string, LanguageFn>\n lineNumbers?: boolean\n maskCredentials?: string | string[]\n },\n) {\n // Simple restriction on credentials to prevent unexpected behavior\n const credentials = (\n typeof options?.maskCredentials === 'string' ? [options.maskCredentials] : (options?.maskCredentials ?? [])\n ).filter((c) => {\n // Credentials must be at least 3 characters to mask.\n if (c.length < 3) {\n return false\n }\n\n return true\n })\n\n // Classname is used by lowlight to select the language model\n const className = `language-${lowlightLanguageMappings[options.lang] ?? options.lang}`\n\n // biome-ignore lint/suspicious/noEmptyBlockStatements: empty plugin\n const nullPlugin = (() => {}) satisfies Plugin\n\n const html = unified()\n // Parses markdown\n .use(rehypeParse, { fragment: true })\n // Raw code string must be injected after initial hast parsing\n // so that HTML code is not parsed into the hast tree\n .use(injectRawCodeStringPlugin(codeString))\n // Syntax highlighting\n .use(rehypeHighlight, {\n languages: options.languages,\n })\n .use(options?.lineNumbers ? codeBlockLinesPlugin : nullPlugin)\n // Converts the HTML AST to a string\n .use(rehypeStringify)\n // Run the pipeline\n .processSync(`<pre><code class=\"${className}\"></code></pre>`)\n\n const htmlString = html.toString()\n\n // Replace any credentials with a wrapper element\n return credentials.length\n ? credentials.reduce(\n (acc, credential) =>\n acc\n .split(credential)\n .join(`<span class=\"credential\"><span class=\"credential-value\">${credential}</span></span>`),\n htmlString,\n )\n : htmlString\n}\n\n/**\n * To prevent unified from parsing any content of the code string we inject\n * it as a raw text node into the AST tree as a child of the code element\n */\nfunction injectRawCodeStringPlugin(rawCodeString: string) {\n return () => (tree: Root) => {\n visit(tree, 'element', (node: Element) => {\n if (node.tagName === 'code') {\n node.children.push({\n type: 'text',\n value: rawCodeString,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
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare function isText(element?: ElementContent): element is Text;
|
|
3
|
-
export declare function isElement(node?: ElementContent): node is Element;
|
|
4
|
-
export declare function textElement(value: string): Text;
|
|
5
|
-
export declare function lineBreak(): Text;
|
|
1
|
+
import type { Root } from 'hast';
|
|
6
2
|
/**
|
|
7
3
|
* Adds lines to code blocks
|
|
8
4
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"line-numbers.d.ts","sourceRoot":"","sources":["../../src/code/line-numbers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"line-numbers.d.ts","sourceRoot":"","sources":["../../src/code/line-numbers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA2B,IAAI,EAAQ,MAAM,MAAM,CAAA;AAsB/D;;GAEG;AACH,wBAAgB,oBAAoB,KAC1B,MAAM,IAAI,UA2BnB"}
|
|
@@ -64,10 +64,6 @@ function hasLineBreak(node) {
|
|
|
64
64
|
return isText(node) && /\r?\n/.test(node.value) || isElement(node) && node.children.some(hasLineBreak);
|
|
65
65
|
}
|
|
66
66
|
export {
|
|
67
|
-
codeBlockLinesPlugin
|
|
68
|
-
isElement,
|
|
69
|
-
isText,
|
|
70
|
-
lineBreak,
|
|
71
|
-
textElement
|
|
67
|
+
codeBlockLinesPlugin
|
|
72
68
|
};
|
|
73
69
|
//# sourceMappingURL=line-numbers.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/code/line-numbers.ts"],
|
|
4
|
-
"sourcesContent": ["import type { Element, ElementContent, Root, Text } from 'hast'\nimport { visit } from 'unist-util-visit'\n\n// ---------------------------------------------------------------------------\n// Line Numbering plugin\n\
|
|
5
|
-
"mappings": "AACA,SAAS,aAAa;
|
|
4
|
+
"sourcesContent": ["import type { Element, ElementContent, Root, Text } from 'hast'\nimport { visit } from 'unist-util-visit'\n\n// ---------------------------------------------------------------------------\n// Line Numbering plugin\n\nfunction isText(element?: ElementContent): element is Text {\n return element?.type === 'text'\n}\n\nfunction isElement(node?: ElementContent): node is Element {\n return node?.type === 'element'\n}\n\nfunction textElement(value: string): Text {\n return { type: 'text', value }\n}\n\nfunction lineBreak(): Text {\n return { type: 'text', value: '\\n' }\n}\n\n/**\n * Adds lines to code blocks\n */\nexport function codeBlockLinesPlugin() {\n return (tree: Root) => {\n visit(tree, 'element', (node: Element, _i, parent: Root | Element | null) => {\n if (parent?.type === 'element' && parent.tagName === 'pre' && node.tagName === 'code') {\n let numLines = 0\n\n // Wraps each line in a span\n node.children = addLines(node)\n\n // Adds a line break to the end of each line\n node.children.forEach((child: ElementContent) => {\n if (child.type === 'element' && child.tagName === 'span') {\n const lastChild: ElementContent | undefined = child.children[child.children.length - 1]\n\n if (lastChild && (!isText(lastChild) || (isText(lastChild) && !hasLineBreak(lastChild)))) {\n child.children.push(lineBreak())\n numLines++\n }\n }\n })\n\n // We need to maintain a count of the total lines to allow space for the labels\n node.properties.style = [`--line-count: ${numLines};`, `--line-digits: ${numLines.toString().length};`]\n }\n })\n\n // console.log('NUMBER OF LINES IS: ', numLines)\n }\n}\n\n/**\n * Adds lines to a node recursively and returns them\n *\n * @param node - The node to add lines to\n * @param lines - The current lines\n * @param copyParent - Whether to copy the parent node to save the original node styles\n */\nfunction addLines(node: Element, lines: Element[] = [], copyParent?: boolean): Element[] {\n const line = () => lines[lines.length - 1] ?? ((lines.push(createLine()) && lines[lines.length - 1]) || undefined)\n\n node.children.forEach((child: ElementContent) => {\n if (isText(child) && hasLineBreak(child)) {\n const split: string[] = child.value.split(/\\n/)\n\n split.forEach((content: string, i: number) => {\n if (copyParent) {\n line()?.children.push({ ...node, children: [textElement(content)] })\n } else {\n line()?.children.push(textElement(content))\n }\n\n i !== split.length - 1 && lines.push(createLine())\n })\n } else if (isElement(child) && child.children.some(hasLineBreak)) {\n addLines(child, lines, true)\n } else {\n line()?.children.push(child)\n }\n })\n\n return lines\n}\n\n/**\n * Creates a new line element\n *\n * @param children - The children the line should have initially\n */\nfunction createLine(...children: ElementContent[]): Element {\n return {\n type: 'element',\n tagName: 'span',\n properties: { class: ['line'] },\n children,\n }\n}\n\n/**\n * Checks if a node has a line break\n *\n * @param node - The node to check\n */\nfunction hasLineBreak(node: ElementContent): boolean {\n return (isText(node) && /\\r?\\n/.test(node.value)) || (isElement(node) && node.children.some(hasLineBreak))\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,aAAa;AAKtB,SAAS,OAAO,SAA2C;AACzD,SAAO,SAAS,SAAS;AAC3B;AAEA,SAAS,UAAU,MAAwC;AACzD,SAAO,MAAM,SAAS;AACxB;AAEA,SAAS,YAAY,OAAqB;AACxC,SAAO,EAAE,MAAM,QAAQ,MAAM;AAC/B;AAEA,SAAS,YAAkB;AACzB,SAAO,EAAE,MAAM,QAAQ,OAAO,KAAK;AACrC;AAKO,SAAS,uBAAuB;AACrC,SAAO,CAAC,SAAe;AACrB,UAAM,MAAM,WAAW,CAAC,MAAe,IAAI,WAAkC;AAC3E,UAAI,QAAQ,SAAS,aAAa,OAAO,YAAY,SAAS,KAAK,YAAY,QAAQ;AACrF,YAAI,WAAW;AAGf,aAAK,WAAW,SAAS,IAAI;AAG7B,aAAK,SAAS,QAAQ,CAAC,UAA0B;AAC/C,cAAI,MAAM,SAAS,aAAa,MAAM,YAAY,QAAQ;AACxD,kBAAM,YAAwC,MAAM,SAAS,MAAM,SAAS,SAAS,CAAC;AAEtF,gBAAI,cAAc,CAAC,OAAO,SAAS,KAAM,OAAO,SAAS,KAAK,CAAC,aAAa,SAAS,IAAK;AACxF,oBAAM,SAAS,KAAK,UAAU,CAAC;AAC/B;AAAA,YACF;AAAA,UACF;AAAA,QACF,CAAC;AAGD,aAAK,WAAW,QAAQ,CAAC,iBAAiB,QAAQ,KAAK,kBAAkB,SAAS,SAAS,EAAE,MAAM,GAAG;AAAA,MACxG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';
|
|
2
|
-
export { lowlightLanguageMappings } from './constants.js';
|
|
3
|
-
export { htmlFromMarkdown } from './markdown/index.js';
|
|
4
1
|
export { syntaxHighlight } from './code/index.js';
|
|
2
|
+
export { lowlightLanguageMappings } from './constants.js';
|
|
5
3
|
export * from './languages/index.js';
|
|
4
|
+
export { htmlFromMarkdown } from './markdown/index.js';
|
|
5
|
+
export { rehypeHighlight } from './rehype-highlight/index.js';
|
|
6
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAA;AACxC,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAA;AACtD,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { rehypeHighlight } from "./rehype-highlight/index.js";
|
|
2
|
-
import { lowlightLanguageMappings } from "./constants.js";
|
|
3
|
-
import { htmlFromMarkdown } from "./markdown/index.js";
|
|
4
1
|
import { syntaxHighlight } from "./code/index.js";
|
|
2
|
+
import { lowlightLanguageMappings } from "./constants.js";
|
|
5
3
|
export * from "./languages/index.js";
|
|
4
|
+
import { htmlFromMarkdown } from "./markdown/index.js";
|
|
5
|
+
import { rehypeHighlight } from "./rehype-highlight/index.js";
|
|
6
6
|
export {
|
|
7
7
|
htmlFromMarkdown,
|
|
8
8
|
lowlightLanguageMappings,
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["export {
|
|
5
|
-
"mappings": "AAAA,SAAS,uBAAuB;AAChC,SAAS,gCAAgC;AACzC,SAAS,wBAAwB;AACjC,SAAS,uBAAuB;
|
|
4
|
+
"sourcesContent": ["export { syntaxHighlight } from './code'\nexport { lowlightLanguageMappings } from './constants'\nexport * from './languages'\nexport { htmlFromMarkdown } from './markdown'\nexport { rehypeHighlight } from './rehype-highlight'\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,uBAAuB;AAChC,SAAS,gCAAgC;AACzC,cAAc;AACd,SAAS,wBAAwB;AACjC,SAAS,uBAAuB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/languages/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/languages/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AACxC,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA"}
|
package/dist/languages/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/languages/index.ts"],
|
|
4
|
-
"sourcesContent": ["export {
|
|
5
|
-
"mappings": "AAAA,SAAS,
|
|
4
|
+
"sourcesContent": ["export { basicLanguages } from './basic'\nexport { jsonYamlLanguages } from './json-yaml'\nexport { standardLanguages } from './standard'\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,sBAAsB;AAC/B,SAAS,yBAAyB;AAClC,SAAS,yBAAyB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -17,19 +17,5 @@ import type { LanguageFn } from 'highlight.js';
|
|
|
17
17
|
/**
|
|
18
18
|
* These are the most popular languages that cover the majority of use cases.
|
|
19
19
|
*/
|
|
20
|
-
declare const standardLanguages: Record<string, LanguageFn>;
|
|
21
|
-
/**
|
|
22
|
-
* These languages are not as popular as the ones in `standardLanguages`, but
|
|
23
|
-
* still have a decent amount of users.
|
|
24
|
-
*/
|
|
25
|
-
declare const mediumLanguages: Record<string, LanguageFn>;
|
|
26
|
-
/**
|
|
27
|
-
* These languages are more specialized and have a smaller user base.
|
|
28
|
-
*/
|
|
29
|
-
declare const specializedLanguages: Record<string, LanguageFn>;
|
|
30
|
-
/**
|
|
31
|
-
* A combined list of all languages.
|
|
32
|
-
*/
|
|
33
|
-
declare const allLanguages: Record<string, LanguageFn>;
|
|
34
|
-
export { standardLanguages, mediumLanguages, specializedLanguages, allLanguages };
|
|
20
|
+
export declare const standardLanguages: Record<string, LanguageFn>;
|
|
35
21
|
//# sourceMappingURL=standard.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"standard.d.ts","sourceRoot":"","sources":["../../src/languages/standard.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"standard.d.ts","sourceRoot":"","sources":["../../src/languages/standard.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAiD9C;;;;;;;;;;;;;;GAcG;AAEH;;GAEG;AACH,eAAO,MAAM,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAkDxD,CAAA"}
|
|
@@ -1,78 +1,46 @@
|
|
|
1
|
-
import ada from "highlight.js/lib/languages/ada";
|
|
2
|
-
import apache from "highlight.js/lib/languages/apache";
|
|
3
|
-
import applescript from "highlight.js/lib/languages/applescript";
|
|
4
|
-
import asciidoc from "highlight.js/lib/languages/asciidoc";
|
|
5
|
-
import awk from "highlight.js/lib/languages/awk";
|
|
6
1
|
import bash from "highlight.js/lib/languages/bash";
|
|
7
2
|
import c from "highlight.js/lib/languages/c";
|
|
8
3
|
import clojure from "highlight.js/lib/languages/clojure";
|
|
9
|
-
import cmake from "highlight.js/lib/languages/cmake";
|
|
10
|
-
import coffeescript from "highlight.js/lib/languages/coffeescript";
|
|
11
|
-
import coq from "highlight.js/lib/languages/coq";
|
|
12
4
|
import cpp from "highlight.js/lib/languages/cpp";
|
|
13
|
-
import crystal from "highlight.js/lib/languages/crystal";
|
|
14
5
|
import csharp from "highlight.js/lib/languages/csharp";
|
|
15
6
|
import css from "highlight.js/lib/languages/css";
|
|
16
|
-
import d from "highlight.js/lib/languages/d";
|
|
17
7
|
import dart from "highlight.js/lib/languages/dart";
|
|
18
|
-
import delphi from "highlight.js/lib/languages/delphi";
|
|
19
8
|
import diff from "highlight.js/lib/languages/diff";
|
|
20
9
|
import dockerfile from "highlight.js/lib/languages/dockerfile";
|
|
21
10
|
import elixir from "highlight.js/lib/languages/elixir";
|
|
22
|
-
import elm from "highlight.js/lib/languages/elm";
|
|
23
|
-
import erlang from "highlight.js/lib/languages/erlang";
|
|
24
11
|
import fsharp from "highlight.js/lib/languages/fsharp";
|
|
25
|
-
import glsl from "highlight.js/lib/languages/glsl";
|
|
26
12
|
import go from "highlight.js/lib/languages/go";
|
|
27
13
|
import graphql from "highlight.js/lib/languages/graphql";
|
|
28
|
-
import groovy from "highlight.js/lib/languages/groovy";
|
|
29
|
-
import haml from "highlight.js/lib/languages/haml";
|
|
30
|
-
import handlebars from "highlight.js/lib/languages/handlebars";
|
|
31
14
|
import haskell from "highlight.js/lib/languages/haskell";
|
|
32
15
|
import http from "highlight.js/lib/languages/http";
|
|
33
16
|
import ini from "highlight.js/lib/languages/ini";
|
|
34
17
|
import java from "highlight.js/lib/languages/java";
|
|
35
18
|
import javascript from "highlight.js/lib/languages/javascript";
|
|
36
19
|
import json from "highlight.js/lib/languages/json";
|
|
37
|
-
import julia from "highlight.js/lib/languages/julia";
|
|
38
20
|
import kotlin from "highlight.js/lib/languages/kotlin";
|
|
39
|
-
import latex from "highlight.js/lib/languages/latex";
|
|
40
21
|
import less from "highlight.js/lib/languages/less";
|
|
41
|
-
import lisp from "highlight.js/lib/languages/lisp";
|
|
42
22
|
import lua from "highlight.js/lib/languages/lua";
|
|
43
23
|
import makefile from "highlight.js/lib/languages/makefile";
|
|
44
24
|
import markdown from "highlight.js/lib/languages/markdown";
|
|
45
25
|
import matlab from "highlight.js/lib/languages/matlab";
|
|
46
26
|
import nginx from "highlight.js/lib/languages/nginx";
|
|
47
|
-
import nim from "highlight.js/lib/languages/nim";
|
|
48
|
-
import nix from "highlight.js/lib/languages/nix";
|
|
49
27
|
import objectivec from "highlight.js/lib/languages/objectivec";
|
|
50
28
|
import ocaml from "highlight.js/lib/languages/ocaml";
|
|
51
29
|
import perl from "highlight.js/lib/languages/perl";
|
|
52
30
|
import php from "highlight.js/lib/languages/php";
|
|
53
31
|
import plaintext from "highlight.js/lib/languages/plaintext";
|
|
54
32
|
import powershell from "highlight.js/lib/languages/powershell";
|
|
55
|
-
import prolog from "highlight.js/lib/languages/prolog";
|
|
56
33
|
import properties from "highlight.js/lib/languages/properties";
|
|
57
|
-
import protobuf from "highlight.js/lib/languages/protobuf";
|
|
58
34
|
import python from "highlight.js/lib/languages/python";
|
|
59
35
|
import r from "highlight.js/lib/languages/r";
|
|
60
36
|
import ruby from "highlight.js/lib/languages/ruby";
|
|
61
37
|
import rust from "highlight.js/lib/languages/rust";
|
|
62
38
|
import scala from "highlight.js/lib/languages/scala";
|
|
63
|
-
import scheme from "highlight.js/lib/languages/scheme";
|
|
64
39
|
import scss from "highlight.js/lib/languages/scss";
|
|
65
40
|
import shell from "highlight.js/lib/languages/shell";
|
|
66
|
-
import smalltalk from "highlight.js/lib/languages/smalltalk";
|
|
67
41
|
import sql from "highlight.js/lib/languages/sql";
|
|
68
|
-
import stylus from "highlight.js/lib/languages/stylus";
|
|
69
42
|
import swift from "highlight.js/lib/languages/swift";
|
|
70
|
-
import tcl from "highlight.js/lib/languages/tcl";
|
|
71
|
-
import twig from "highlight.js/lib/languages/twig";
|
|
72
43
|
import typescript from "highlight.js/lib/languages/typescript";
|
|
73
|
-
import verilog from "highlight.js/lib/languages/verilog";
|
|
74
|
-
import vhdl from "highlight.js/lib/languages/vhdl";
|
|
75
|
-
import vim from "highlight.js/lib/languages/vim";
|
|
76
44
|
import xml from "highlight.js/lib/languages/xml";
|
|
77
45
|
import yaml from "highlight.js/lib/languages/yaml";
|
|
78
46
|
import curl from "highlightjs-curl";
|
|
@@ -89,6 +57,7 @@ const standardLanguages = {
|
|
|
89
57
|
docker: dockerfile,
|
|
90
58
|
dockerfile,
|
|
91
59
|
elixir,
|
|
60
|
+
fsharp,
|
|
92
61
|
go,
|
|
93
62
|
graphql,
|
|
94
63
|
haskell,
|
|
@@ -126,61 +95,7 @@ const standardLanguages = {
|
|
|
126
95
|
xml,
|
|
127
96
|
yaml
|
|
128
97
|
};
|
|
129
|
-
const mediumLanguages = {
|
|
130
|
-
cmake,
|
|
131
|
-
crystal,
|
|
132
|
-
elm,
|
|
133
|
-
erl: erlang,
|
|
134
|
-
erlang,
|
|
135
|
-
fsharp,
|
|
136
|
-
glsl,
|
|
137
|
-
groovy,
|
|
138
|
-
handlebars,
|
|
139
|
-
hbs: handlebars,
|
|
140
|
-
jl: julia,
|
|
141
|
-
julia,
|
|
142
|
-
nim,
|
|
143
|
-
nix,
|
|
144
|
-
proto: protobuf,
|
|
145
|
-
protobuf,
|
|
146
|
-
styl: stylus,
|
|
147
|
-
stylus,
|
|
148
|
-
twig,
|
|
149
|
-
verilog,
|
|
150
|
-
vhdl
|
|
151
|
-
};
|
|
152
|
-
const specializedLanguages = {
|
|
153
|
-
ada,
|
|
154
|
-
adoc: asciidoc,
|
|
155
|
-
apache,
|
|
156
|
-
applescript,
|
|
157
|
-
asciidoc,
|
|
158
|
-
awk,
|
|
159
|
-
coffee: coffeescript,
|
|
160
|
-
coffeescript,
|
|
161
|
-
coq,
|
|
162
|
-
d,
|
|
163
|
-
haml,
|
|
164
|
-
hs: haskell,
|
|
165
|
-
lisp,
|
|
166
|
-
latex,
|
|
167
|
-
pascal: delphi,
|
|
168
|
-
prolog,
|
|
169
|
-
scheme,
|
|
170
|
-
smalltalk,
|
|
171
|
-
tcl,
|
|
172
|
-
tex: latex,
|
|
173
|
-
vim
|
|
174
|
-
};
|
|
175
|
-
const allLanguages = {
|
|
176
|
-
...standardLanguages,
|
|
177
|
-
...mediumLanguages,
|
|
178
|
-
...specializedLanguages
|
|
179
|
-
};
|
|
180
98
|
export {
|
|
181
|
-
allLanguages,
|
|
182
|
-
mediumLanguages,
|
|
183
|
-
specializedLanguages,
|
|
184
99
|
standardLanguages
|
|
185
100
|
};
|
|
186
101
|
//# sourceMappingURL=standard.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/languages/standard.ts"],
|
|
4
|
-
"sourcesContent": ["import type { LanguageFn } from 'highlight.js'\nimport
|
|
5
|
-
"mappings": "AACA,OAAO,
|
|
4
|
+
"sourcesContent": ["import type { LanguageFn } from 'highlight.js'\nimport bash from 'highlight.js/lib/languages/bash'\nimport c from 'highlight.js/lib/languages/c'\nimport clojure from 'highlight.js/lib/languages/clojure'\nimport cpp from 'highlight.js/lib/languages/cpp'\nimport csharp from 'highlight.js/lib/languages/csharp'\nimport css from 'highlight.js/lib/languages/css'\nimport dart from 'highlight.js/lib/languages/dart'\nimport diff from 'highlight.js/lib/languages/diff'\nimport dockerfile from 'highlight.js/lib/languages/dockerfile'\nimport elixir from 'highlight.js/lib/languages/elixir'\nimport fsharp from 'highlight.js/lib/languages/fsharp'\nimport go from 'highlight.js/lib/languages/go'\nimport graphql from 'highlight.js/lib/languages/graphql'\nimport haskell from 'highlight.js/lib/languages/haskell'\nimport http from 'highlight.js/lib/languages/http'\nimport ini from 'highlight.js/lib/languages/ini'\nimport java from 'highlight.js/lib/languages/java'\nimport javascript from 'highlight.js/lib/languages/javascript'\nimport json from 'highlight.js/lib/languages/json'\nimport kotlin from 'highlight.js/lib/languages/kotlin'\nimport less from 'highlight.js/lib/languages/less'\nimport lua from 'highlight.js/lib/languages/lua'\nimport makefile from 'highlight.js/lib/languages/makefile'\nimport markdown from 'highlight.js/lib/languages/markdown'\nimport matlab from 'highlight.js/lib/languages/matlab'\nimport nginx from 'highlight.js/lib/languages/nginx'\nimport objectivec from 'highlight.js/lib/languages/objectivec'\nimport ocaml from 'highlight.js/lib/languages/ocaml'\nimport perl from 'highlight.js/lib/languages/perl'\nimport php from 'highlight.js/lib/languages/php'\nimport plaintext from 'highlight.js/lib/languages/plaintext'\nimport powershell from 'highlight.js/lib/languages/powershell'\nimport properties from 'highlight.js/lib/languages/properties'\nimport python from 'highlight.js/lib/languages/python'\nimport r from 'highlight.js/lib/languages/r'\nimport ruby from 'highlight.js/lib/languages/ruby'\nimport rust from 'highlight.js/lib/languages/rust'\nimport scala from 'highlight.js/lib/languages/scala'\nimport scss from 'highlight.js/lib/languages/scss'\nimport shell from 'highlight.js/lib/languages/shell'\nimport sql from 'highlight.js/lib/languages/sql'\nimport swift from 'highlight.js/lib/languages/swift'\nimport typescript from 'highlight.js/lib/languages/typescript'\nimport xml from 'highlight.js/lib/languages/xml'\nimport yaml from 'highlight.js/lib/languages/yaml'\n// @ts-expect-error No types available\nimport curl from 'highlightjs-curl'\n\n/**\n * We group languages into three categories based on their popularity and usage.\n * This helps in optimizing the bundle size by allowing users to include only\n * the languages they need.\n *\n * 1. Standard Languages: These are the most popular languages that cover the\n * majority of use cases.\n * 2. Medium Languages: These languages are not as popular as the ones in\n * `standardLanguages`, but still have a decent amount of users.\n * 3. Specialized Languages: These languages are more specialized and have a\n * smaller user base.\n *\n * Each category is represented as a separate object, making it easy to import\n * only the languages you need.\n */\n\n/**\n * These are the most popular languages that cover the majority of use cases.\n */\nexport const standardLanguages: Record<string, LanguageFn> = {\n bash,\n c,\n clojure,\n cpp,\n csharp,\n css,\n curl,\n dart,\n diff,\n docker: dockerfile,\n dockerfile,\n elixir,\n fsharp,\n go,\n graphql,\n haskell,\n html: xml,\n http,\n ini,\n java,\n javascript,\n json,\n kotlin,\n less,\n lua,\n makefile,\n markdown,\n matlab,\n nginx,\n objectivec,\n ocaml,\n perl,\n php,\n plaintext,\n powershell,\n properties,\n python,\n r,\n ruby,\n rust,\n scala,\n scss,\n shell,\n sql,\n swift,\n toml: ini,\n typescript,\n xml,\n yaml,\n}\n"],
|
|
5
|
+
"mappings": "AACA,OAAO,UAAU;AACjB,OAAO,OAAO;AACd,OAAO,aAAa;AACpB,OAAO,SAAS;AAChB,OAAO,YAAY;AACnB,OAAO,SAAS;AAChB,OAAO,UAAU;AACjB,OAAO,UAAU;AACjB,OAAO,gBAAgB;AACvB,OAAO,YAAY;AACnB,OAAO,YAAY;AACnB,OAAO,QAAQ;AACf,OAAO,aAAa;AACpB,OAAO,aAAa;AACpB,OAAO,UAAU;AACjB,OAAO,SAAS;AAChB,OAAO,UAAU;AACjB,OAAO,gBAAgB;AACvB,OAAO,UAAU;AACjB,OAAO,YAAY;AACnB,OAAO,UAAU;AACjB,OAAO,SAAS;AAChB,OAAO,cAAc;AACrB,OAAO,cAAc;AACrB,OAAO,YAAY;AACnB,OAAO,WAAW;AAClB,OAAO,gBAAgB;AACvB,OAAO,WAAW;AAClB,OAAO,UAAU;AACjB,OAAO,SAAS;AAChB,OAAO,eAAe;AACtB,OAAO,gBAAgB;AACvB,OAAO,gBAAgB;AACvB,OAAO,YAAY;AACnB,OAAO,OAAO;AACd,OAAO,UAAU;AACjB,OAAO,UAAU;AACjB,OAAO,WAAW;AAClB,OAAO,UAAU;AACjB,OAAO,WAAW;AAClB,OAAO,SAAS;AAChB,OAAO,WAAW;AAClB,OAAO,gBAAgB;AACvB,OAAO,SAAS;AAChB,OAAO,UAAU;AAEjB,OAAO,UAAU;AAqBV,MAAM,oBAAgD;AAAA,EAC3D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,QAAQ;AAAA,EACR;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,MAAM;AAAA,EACN;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,MAAM;AAAA,EACN;AAAA,EACA;AAAA,EACA;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/markdown/markdown.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/markdown/markdown.ts"],"names":[],"mappings":"AA0CA;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE;IACR,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;IACrB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IACpB,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC9D,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB,UAqDF;AASD;;GAEG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,EAChB,KAAK,GAAE,MAAU,GAChB;IACD,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;CACd,EAAE,CAiBF;AAuBD;;GAEG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,YAgC5C"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/markdown/markdown.ts"],
|
|
4
|
-
"sourcesContent": ["import type { Heading, PhrasingContent, Root, RootContent, Text } from 'mdast'\nimport rehypeExternalLinks from 'rehype-external-links'\nimport rehypeFormat from 'rehype-format'\nimport rehypeRaw from 'rehype-raw'\nimport rehypeSanitize, { defaultSchema } from 'rehype-sanitize'\nimport rehypeStringify from 'rehype-stringify'\nimport remarkGfm from 'remark-gfm'\nimport remarkParse from 'remark-parse'\nimport remarkRehype from 'remark-rehype'\nimport remarkStringify from 'remark-stringify'\nimport { unified } from 'unified'\nimport type { Node } from 'unist'\nimport { SKIP, visit } from 'unist-util-visit'\n\nimport { standardLanguages } from '@/languages'\nimport { rehypeAlert } from '@/rehype-alert'\nimport { rehypeHighlight } from '@/rehype-highlight'\ntype Options = {\n transform?: (node: Record<string, any>) => Record<string, any>\n type?: string\n}\n\n/**\n * Plugin to transform nodes in a Markdown AST\n */\nconst transformNodes =\n (options?: Readonly<Options> | null | undefined, ..._ignored: any[]) =>\n (tree: Node) => {\n if (!options?.transform || !options?.type) {\n return\n }\n\n visit(tree, options?.type, (node) => {\n options?.transform ? options?.transform(node) : node\n\n return SKIP\n })\n\n return\n }\n\n/**\n * Take a Markdown string and generate HTML from it\n */\nexport function htmlFromMarkdown(\n markdown: string,\n options?: {\n removeTags?: string[]\n allowTags?: string[]\n transform?: (node: Record<string, any>) => Record<string, any>\n transformType?: string\n },\n) {\n // Add permitted tags and remove stripped ones\n const removeTags = options?.removeTags ?? []\n const tagNames = [...(defaultSchema.tagNames ?? []), ...(options?.allowTags ?? [])].filter(\n (t) => !removeTags.includes(t),\n )\n\n const html = unified()\n // Parses markdown\n .use(remarkParse)\n // Support autolink literals, footnotes, strikethrough, tables and tasklists\n .use(remarkGfm)\n .use(transformNodes, {\n transform: options?.transform,\n type: options?.transformType,\n })\n // Allows any HTML tags\n .use(remarkRehype, { allowDangerousHtml: true })\n // Adds GitHub alerts\n .use(rehypeAlert)\n // Creates a HTML AST\n .use(rehypeRaw)\n // Removes disallowed tags\n .use(rehypeSanitize, {\n ...defaultSchema,\n // Don't prefix the heading ids\n clobberPrefix: '',\n // Makes it even more strict\n tagNames,\n attributes: {\n ...defaultSchema.attributes,\n abbr: ['title'],\n // Allow alert classes\n div: ['class', ['className', /^markdown-alert(-.*)?$/]],\n },\n })\n // Syntax highlighting\n .use(rehypeHighlight, {\n languages: standardLanguages,\n // Enable auto detection\n detect: true,\n })\n // Adds target=\"_blank\" to external links\n .use(rehypeExternalLinks, { target: '_blank' })\n // Formats the HTML\n .use(rehypeFormat)\n // Converts the HTML AST to a string\n .use(rehypeStringify)\n // Run the pipeline\n .processSync(markdown)\n\n return html.toString()\n}\n\n/**\n * Create a Markdown AST from a string.\n */\nfunction getMarkdownAst(markdown: string): Root {\n return unified().use(remarkParse).use(remarkGfm).parse(markdown)\n}\n\n/**\n * Find all headings of a specific type in a Markdown AST.\n */\nexport function getHeadings(\n markdown: string,\n depth: number = 1,\n): {\n depth: number\n value: string\n}[] {\n const tree = getMarkdownAst(markdown)\n\n const nodes: {\n depth: number\n value: string\n }[] = []\n\n visit(tree, 'heading', (node) => {\n const text = findTextInHeading(node)\n\n if (text) {\n nodes.push({ depth: node.depth ?? depth, value: text.value })\n }\n })\n\n return nodes\n}\n\n/**\n * Find the text in a Markdown node (recursively).\n */\nfunction findTextInHeading(node: Heading | PhrasingContent): Text | null {\n if (node.type === 'text') {\n return node as Text\n }\n\n if ('children' in node && node.children) {\n for (const child of node.children) {\n const text = findTextInHeading(child)\n\n if (text) {\n return text\n }\n }\n }\n\n return null\n}\n\n/**\n * Return multiple Markdown documents. Every heading should be its own document.\n */\nexport function splitContent(markdown: string) {\n const tree = getMarkdownAst(markdown)\n\n /** Sections */\n const sections: RootContent[][] = []\n\n /** Nodes inside a section */\n let nodes: RootContent[] = []\n\n tree.children?.forEach((node) => {\n // If the node is a heading, start a new section\n if (node.type === 'heading') {\n if (nodes.length) {\n sections.push(nodes)\n }\n\n sections.push([node])\n\n nodes = []\n }\n // Otherwise, add the node to the current section\n else {\n nodes.push(node)\n }\n })\n\n // Add any remaining nodes\n if (nodes.length) {\n sections.push(nodes)\n }\n\n return sections.map((section) => createDocument(section))\n}\n\n/**\n * Use remark to create a Markdown document from a list of nodes.\n */\nfunction createDocument(nodes: RootContent[]) {\n // Create the Markdown string\n const markdown = unified().use(remarkStringify).use(remarkGfm).stringify({\n type: 'root',\n children: nodes,\n })\n\n // Remove the whitespace\n return markdown.trim()\n}\n"],
|
|
5
|
-
"mappings": "AACA,OAAO,yBAAyB;AAChC,OAAO,kBAAkB;AACzB,OAAO,eAAe;AACtB,OAAO,kBAAkB,qBAAqB;AAC9C,OAAO,qBAAqB;AAC5B,OAAO,eAAe;AACtB,OAAO,iBAAiB;AACxB,OAAO,kBAAkB;AACzB,OAAO,qBAAqB;AAC5B,SAAS,eAAe;AAExB,SAAS,MAAM,aAAa;AAE5B,SAAS,yBAAyB;AAClC,SAAS,mBAAmB;AAC5B,SAAS,uBAAuB;
|
|
4
|
+
"sourcesContent": ["import type { Heading, PhrasingContent, Root, RootContent, Text } from 'mdast'\nimport rehypeExternalLinks from 'rehype-external-links'\nimport rehypeFormat from 'rehype-format'\nimport rehypeRaw from 'rehype-raw'\nimport rehypeSanitize, { defaultSchema } from 'rehype-sanitize'\nimport rehypeStringify from 'rehype-stringify'\nimport remarkGfm from 'remark-gfm'\nimport remarkParse from 'remark-parse'\nimport remarkRehype from 'remark-rehype'\nimport remarkStringify from 'remark-stringify'\nimport { unified } from 'unified'\nimport type { Node } from 'unist'\nimport { SKIP, visit } from 'unist-util-visit'\n\nimport { standardLanguages } from '@/languages'\nimport { rehypeAlert } from '@/rehype-alert'\nimport { rehypeHighlight } from '@/rehype-highlight'\n\ntype Options = {\n transform?: (node: Record<string, any>) => Record<string, any>\n type?: string\n}\n\n/**\n * Plugin to transform nodes in a Markdown AST\n */\nconst transformNodes =\n (options?: Readonly<Options> | null | undefined, ..._ignored: any[]) =>\n (tree: Node) => {\n if (!options?.transform || !options?.type) {\n return\n }\n\n visit(tree, options?.type, (node) => {\n options?.transform ? options?.transform(node) : node\n\n return SKIP\n })\n\n return\n }\n\n/**\n * Take a Markdown string and generate HTML from it\n */\nexport function htmlFromMarkdown(\n markdown: string,\n options?: {\n removeTags?: string[]\n allowTags?: string[]\n transform?: (node: Record<string, any>) => Record<string, any>\n transformType?: string\n },\n) {\n // Add permitted tags and remove stripped ones\n const removeTags = options?.removeTags ?? []\n const tagNames = [...(defaultSchema.tagNames ?? []), ...(options?.allowTags ?? [])].filter(\n (t) => !removeTags.includes(t),\n )\n\n const html = unified()\n // Parses markdown\n .use(remarkParse)\n // Support autolink literals, footnotes, strikethrough, tables and tasklists\n .use(remarkGfm)\n .use(transformNodes, {\n transform: options?.transform,\n type: options?.transformType,\n })\n // Allows any HTML tags\n .use(remarkRehype, { allowDangerousHtml: true })\n // Adds GitHub alerts\n .use(rehypeAlert)\n // Creates a HTML AST\n .use(rehypeRaw)\n // Removes disallowed tags\n .use(rehypeSanitize, {\n ...defaultSchema,\n // Don't prefix the heading ids\n clobberPrefix: '',\n // Makes it even more strict\n tagNames,\n attributes: {\n ...defaultSchema.attributes,\n abbr: ['title'],\n // Allow alert classes\n div: ['class', ['className', /^markdown-alert(-.*)?$/]],\n },\n })\n // Syntax highlighting\n .use(rehypeHighlight, {\n languages: standardLanguages,\n // Enable auto detection\n detect: true,\n })\n // Adds target=\"_blank\" to external links\n .use(rehypeExternalLinks, { target: '_blank' })\n // Formats the HTML\n .use(rehypeFormat)\n // Converts the HTML AST to a string\n .use(rehypeStringify)\n // Run the pipeline\n .processSync(markdown)\n\n return html.toString()\n}\n\n/**\n * Create a Markdown AST from a string.\n */\nfunction getMarkdownAst(markdown: string): Root {\n return unified().use(remarkParse).use(remarkGfm).parse(markdown)\n}\n\n/**\n * Find all headings of a specific type in a Markdown AST.\n */\nexport function getHeadings(\n markdown: string,\n depth: number = 1,\n): {\n depth: number\n value: string\n}[] {\n const tree = getMarkdownAst(markdown)\n\n const nodes: {\n depth: number\n value: string\n }[] = []\n\n visit(tree, 'heading', (node) => {\n const text = findTextInHeading(node)\n\n if (text) {\n nodes.push({ depth: node.depth ?? depth, value: text.value })\n }\n })\n\n return nodes\n}\n\n/**\n * Find the text in a Markdown node (recursively).\n */\nfunction findTextInHeading(node: Heading | PhrasingContent): Text | null {\n if (node.type === 'text') {\n return node as Text\n }\n\n if ('children' in node && node.children) {\n for (const child of node.children) {\n const text = findTextInHeading(child)\n\n if (text) {\n return text\n }\n }\n }\n\n return null\n}\n\n/**\n * Return multiple Markdown documents. Every heading should be its own document.\n */\nexport function splitContent(markdown: string) {\n const tree = getMarkdownAst(markdown)\n\n /** Sections */\n const sections: RootContent[][] = []\n\n /** Nodes inside a section */\n let nodes: RootContent[] = []\n\n tree.children?.forEach((node) => {\n // If the node is a heading, start a new section\n if (node.type === 'heading') {\n if (nodes.length) {\n sections.push(nodes)\n }\n\n sections.push([node])\n\n nodes = []\n }\n // Otherwise, add the node to the current section\n else {\n nodes.push(node)\n }\n })\n\n // Add any remaining nodes\n if (nodes.length) {\n sections.push(nodes)\n }\n\n return sections.map((section) => createDocument(section))\n}\n\n/**\n * Use remark to create a Markdown document from a list of nodes.\n */\nfunction createDocument(nodes: RootContent[]) {\n // Create the Markdown string\n const markdown = unified().use(remarkStringify).use(remarkGfm).stringify({\n type: 'root',\n children: nodes,\n })\n\n // Remove the whitespace\n return markdown.trim()\n}\n"],
|
|
5
|
+
"mappings": "AACA,OAAO,yBAAyB;AAChC,OAAO,kBAAkB;AACzB,OAAO,eAAe;AACtB,OAAO,kBAAkB,qBAAqB;AAC9C,OAAO,qBAAqB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AAe,OAAO,CAAC;AAC1D;AAKA,SAAS,eAAe,OAAsB;AAE5C,QAAM,WAAW,QAAQ,EAAE,IAAI,eAAe,EAAE,IAAI,SAAS,EAAE,UAAU;AAAA,IACvE,MAAM;AAAA,IACN,UAAU;AAAA,EACZ,CAAC;AAGD,SAAO,SAAS,KAAK;AACvB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -16,12 +16,13 @@
|
|
|
16
16
|
"lowlight",
|
|
17
17
|
"highlightjs"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.2.
|
|
19
|
+
"version": "0.2.1",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=20"
|
|
22
22
|
},
|
|
23
23
|
"type": "module",
|
|
24
24
|
"main": "dist/index.js",
|
|
25
|
+
"module": "dist/index.js",
|
|
25
26
|
"exports": {
|
|
26
27
|
".": {
|
|
27
28
|
"import": "./dist/index.js",
|
|
@@ -68,13 +69,11 @@
|
|
|
68
69
|
"dist",
|
|
69
70
|
"CHANGELOG.md"
|
|
70
71
|
],
|
|
71
|
-
"module": "dist/index.js",
|
|
72
72
|
"sideEffects": false,
|
|
73
73
|
"dependencies": {
|
|
74
74
|
"hast-util-to-text": "^4.0.2",
|
|
75
75
|
"highlight.js": "^11.9.0",
|
|
76
76
|
"highlightjs-curl": "^1.3.0",
|
|
77
|
-
"highlightjs-vue": "^1.0.0",
|
|
78
77
|
"lowlight": "^3.1.0",
|
|
79
78
|
"rehype-external-links": "^3.0.0",
|
|
80
79
|
"rehype-format": "^5.0.0",
|
|
@@ -90,22 +89,21 @@
|
|
|
90
89
|
"unist-util-visit": "^5.0.0"
|
|
91
90
|
},
|
|
92
91
|
"devDependencies": {
|
|
93
|
-
"@rollup/plugin-typescript": "^12.1.2",
|
|
94
92
|
"@types/hast": "^3.0.4",
|
|
95
93
|
"@types/mdast": "^4.0.4",
|
|
96
94
|
"@types/unist": "^3.0.2",
|
|
97
95
|
"vfile": "^6.0.1",
|
|
98
|
-
"vite": "7.1.
|
|
99
|
-
"
|
|
100
|
-
"@scalar/
|
|
101
|
-
"@scalar/themes": "0.13.18"
|
|
96
|
+
"vite": "7.1.11",
|
|
97
|
+
"@scalar/build-tooling": "0.3.1",
|
|
98
|
+
"@scalar/themes": "0.13.25"
|
|
102
99
|
},
|
|
103
100
|
"scripts": {
|
|
104
101
|
"build": "scalar-build-esbuild",
|
|
105
102
|
"dev": "vite",
|
|
106
|
-
"format": "scalar-format
|
|
107
|
-
"
|
|
108
|
-
"lint:
|
|
103
|
+
"format": "scalar-format",
|
|
104
|
+
"format:check": "scalar-format-check",
|
|
105
|
+
"lint:check": "scalar-lint-check",
|
|
106
|
+
"lint:fix": "scalar-lint-fix",
|
|
109
107
|
"test": "vitest",
|
|
110
108
|
"types:build": "scalar-types-build",
|
|
111
109
|
"types:check": "scalar-types-check"
|