@scalar/code-highlight 0.4.16 → 0.4.17
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 +9 -0
- package/dist/chunks/code.js +13611 -2
- package/dist/chunks/code.js.map +1 -1
- package/dist/chunks/lib.js +1 -6
- package/dist/chunks/lib.js.map +1 -1
- package/dist/chunks/markdown.js +7 -21586
- package/dist/chunks/markdown.js.map +1 -1
- package/dist/chunks/rehype-highlight.js +55 -94
- package/dist/chunks/rehype-highlight.js.map +1 -1
- package/dist/chunks/shared-lowlight.js +2 -13611
- package/dist/chunks/shared-lowlight.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/markdown/index.d.ts +2 -1
- package/dist/markdown/index.d.ts.map +1 -1
- package/dist/markdown/index.js +1 -1
- package/dist/markdown/markdown.d.ts +4 -30
- package/dist/markdown/markdown.d.ts.map +1 -1
- package/dist/rehype-alert/index.js +73 -1
- package/dist/rehype-alert/index.js.map +1 -0
- package/package.json +6 -11
- package/dist/chunks/rehype-alert.js +0 -73
- package/dist/chunks/rehype-alert.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as syntaxHighlight } from "./chunks/code.js";
|
|
2
2
|
import { r as lowlightLanguageMappings, t as rehypeHighlight } from "./chunks/rehype-highlight.js";
|
|
3
3
|
import { n as jsonYamlLanguages, r as basicLanguages, t as standardLanguages } from "./chunks/languages.js";
|
|
4
|
-
import { a as
|
|
4
|
+
import { a as htmlFromMarkdown, i as textFromNode, n as isHeading } from "./chunks/markdown.js";
|
|
5
5
|
export { basicLanguages, htmlFromMarkdown, isHeading, jsonYamlLanguages, lowlightLanguageMappings, rehypeHighlight, standardLanguages, syntaxHighlight, textFromNode };
|
package/dist/markdown/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export { type Node, getHeadings,
|
|
1
|
+
export { type Node, getHeadings, isHeading, splitContent, textFromNode } from '@scalar/markdown/renderer';
|
|
2
|
+
export { htmlFromMarkdown } from './markdown';
|
|
2
3
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/markdown/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,EAAE,WAAW,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/markdown/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,EAAE,WAAW,EAAE,SAAS,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAEzG,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA"}
|
package/dist/markdown/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as getHeadings,
|
|
1
|
+
export { t as getHeadings, a as htmlFromMarkdown, n as isHeading, r as splitContent, i as textFromNode } from "../chunks/markdown.js";
|
|
@@ -1,34 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
/**
|
|
4
|
-
* Type-guard to check if a node is a heading.
|
|
5
|
-
*/
|
|
6
|
-
export declare const isHeading: (node: Node) => node is Heading;
|
|
7
|
-
type HtmlFromMarkdownOptions = {
|
|
1
|
+
/** Render Markdown with Lowlight and retain automatic language detection for unlabelled code fences. */
|
|
2
|
+
export declare const htmlFromMarkdown: (markdown: string, options?: {
|
|
8
3
|
removeTags?: string[];
|
|
9
4
|
allowTags?: string[];
|
|
10
|
-
transform?: (node: Node) => Node;
|
|
5
|
+
transform?: (node: import("mdast").Node) => import("mdast").Node;
|
|
11
6
|
transformType?: string;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Take a Markdown string and generate HTML from it
|
|
15
|
-
*/
|
|
16
|
-
export declare function htmlFromMarkdown(markdown: string, options?: HtmlFromMarkdownOptions): string;
|
|
17
|
-
/**
|
|
18
|
-
* Find all headings of a specific type in a Markdown AST.
|
|
19
|
-
*/
|
|
20
|
-
export declare function getHeadings(markdown: string, depth?: number): {
|
|
21
|
-
depth: number;
|
|
22
|
-
value: string;
|
|
23
|
-
}[];
|
|
24
|
-
/**
|
|
25
|
-
* Extract plain text from a Markdown AST node (recursively).
|
|
26
|
-
*
|
|
27
|
-
* Handles headings with nested phrasing content such as links.
|
|
28
|
-
*/
|
|
29
|
-
export declare function textFromNode(node: Heading | PhrasingContent): string;
|
|
30
|
-
/**
|
|
31
|
-
* Return multiple Markdown documents. Every heading should be its own document.
|
|
32
|
-
*/
|
|
33
|
-
export declare function splitContent(markdown: string): string[];
|
|
7
|
+
}) => string;
|
|
34
8
|
//# sourceMappingURL=markdown.d.ts.map
|
|
@@ -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":"AAMA,wGAAwG;AACxG,eAAO,MAAM,gBAAgB;;;;;YAM5B,CAAA"}
|
|
@@ -1 +1,73 @@
|
|
|
1
|
-
|
|
1
|
+
import { t as visit } from "../chunks/lib.js";
|
|
2
|
+
// src/rehype-alert/rehype-alert.ts
|
|
3
|
+
var ALERT_TYPES = [
|
|
4
|
+
"note",
|
|
5
|
+
"tip",
|
|
6
|
+
"important",
|
|
7
|
+
"warning",
|
|
8
|
+
"caution",
|
|
9
|
+
"success"
|
|
10
|
+
];
|
|
11
|
+
function isWhitespace(node) {
|
|
12
|
+
return node.type === "text" && typeof node.value === "string" && /^\s*$/.test(node.value);
|
|
13
|
+
}
|
|
14
|
+
function rehypeAlert() {
|
|
15
|
+
return (tree) => {
|
|
16
|
+
visit(tree, "element", (node, index, parent) => {
|
|
17
|
+
if (node.tagName !== "blockquote" || typeof index !== "number" || !parent || parent.type !== "root") return;
|
|
18
|
+
const headIndex = node.children.findIndex((child) => !isWhitespace(child));
|
|
19
|
+
if (headIndex === -1) return;
|
|
20
|
+
const head = node.children[headIndex];
|
|
21
|
+
if (head?.type !== "element" || head.tagName !== "p") return;
|
|
22
|
+
const text = head.children[0];
|
|
23
|
+
if (text?.type !== "text" || !text.value.startsWith("[!")) return;
|
|
24
|
+
const end = text.value.indexOf("]");
|
|
25
|
+
if (end === -1) return;
|
|
26
|
+
const alertType = text.value.slice(2, end).toLowerCase();
|
|
27
|
+
if (!ALERT_TYPES.includes(alertType)) return;
|
|
28
|
+
if (end + 1 === text.value.length) {
|
|
29
|
+
const next = head.children[1];
|
|
30
|
+
if (next) {
|
|
31
|
+
if (next.type !== "element" || next.tagName !== "br") return;
|
|
32
|
+
if (!head.children[2]) return;
|
|
33
|
+
head.children = head.children.slice(2);
|
|
34
|
+
const node = head.children[0];
|
|
35
|
+
if (node && node.type === "text" && node.value.charAt(0) === "\n") node.value = node.value.slice(1);
|
|
36
|
+
} else {
|
|
37
|
+
const nextIndex = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1]) ? headIndex + 2 : headIndex + 1;
|
|
38
|
+
if (nextIndex >= node.children.length || node.children[nextIndex]?.type !== "element") return;
|
|
39
|
+
node.children = node.children.slice(nextIndex);
|
|
40
|
+
}
|
|
41
|
+
} else if (text.value.charAt(end + 1) === "\n" && (end + 2 === text.value.length || !/^\s*$/.test(text.value.slice(end + 2)))) text.value = text.value.slice(end + 2);
|
|
42
|
+
else text.value = text.value.replace(/^\s*\[!.*?\]\s*/, "");
|
|
43
|
+
const contentChildren = [];
|
|
44
|
+
for (let i = headIndex; i < node.children.length; i++) {
|
|
45
|
+
const child = node.children[i];
|
|
46
|
+
if (child?.type === "element" && child.tagName === "p" && child.children) contentChildren.push(...child.children);
|
|
47
|
+
else contentChildren.push(child);
|
|
48
|
+
}
|
|
49
|
+
parent.children[index] = {
|
|
50
|
+
type: "element",
|
|
51
|
+
tagName: "div",
|
|
52
|
+
properties: { className: ["markdown-alert", `markdown-alert-${alertType}`] },
|
|
53
|
+
children: [{
|
|
54
|
+
type: "element",
|
|
55
|
+
tagName: "div",
|
|
56
|
+
properties: { className: ["markdown-alert-icon"] },
|
|
57
|
+
children: []
|
|
58
|
+
}, {
|
|
59
|
+
type: "element",
|
|
60
|
+
tagName: "div",
|
|
61
|
+
properties: { className: ["markdown-alert-content"] },
|
|
62
|
+
children: [{
|
|
63
|
+
type: "text",
|
|
64
|
+
value: " "
|
|
65
|
+
}, ...contentChildren]
|
|
66
|
+
}]
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export { rehypeAlert };
|
|
72
|
+
|
|
73
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/rehype-alert/rehype-alert.ts"],"sourcesContent":["import type { Element } from 'hast'\nimport type { Root } from 'mdast'\nimport { visit } from 'unist-util-visit'\n\nconst ALERT_TYPES = ['note', 'tip', 'important', 'warning', 'caution', 'success'] as const\n\n// Simple whitespace check function\nfunction isWhitespace(node: any): boolean {\n return node.type === 'text' && typeof node.value === 'string' && /^\\s*$/.test(node.value)\n}\n\nexport function rehypeAlert() {\n return (tree: Root) => {\n visit(tree, 'element', (node: Element, index, parent: any) => {\n if (node.tagName !== 'blockquote' || typeof index !== 'number' || !parent || parent.type !== 'root') {\n return\n }\n\n // Find the first non-whitespace child\n const headIndex = node.children.findIndex((child) => !isWhitespace(child))\n if (headIndex === -1) {\n return\n }\n\n const head = node.children[headIndex]\n\n if (head?.type !== 'element' || head.tagName !== 'p') {\n return\n }\n\n const text = head.children[0]\n if (text?.type !== 'text' || !text.value.startsWith('[!')) {\n return\n }\n\n const end = text.value.indexOf(']')\n if (end === -1) {\n return\n }\n\n // Extract the alert type\n const alertType = text.value.slice(2, end).toLowerCase() as (typeof ALERT_TYPES)[number]\n if (!ALERT_TYPES.includes(alertType)) {\n return\n }\n\n // Remove the blockquote if it's empty\n if (end + 1 === text.value.length) {\n const next = head.children[1]\n if (next) {\n if (next.type !== 'element' || next.tagName !== 'br') {\n return\n }\n if (!head.children[2]) {\n return\n }\n head.children = head.children.slice(2)\n const node = head.children[0]\n if (node && node.type === 'text' && node.value.charAt(0) === '\\n') {\n node.value = node.value.slice(1)\n }\n } else {\n const skipped = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1])\n const nextIndex = skipped ? headIndex + 2 : headIndex + 1\n if (nextIndex >= node.children.length || node.children[nextIndex]?.type !== 'element') {\n return\n }\n\n node.children = node.children.slice(nextIndex)\n }\n } else if (\n text.value.charAt(end + 1) === '\\n' &&\n // Check if the next character is a newline or a non-whitespace character\n (end + 2 === text.value.length || !/^\\s*$/.test(text.value.slice(end + 2)))\n ) {\n text.value = text.value.slice(end + 2)\n } else {\n // Remove the alert marker if it's not followed by a newline or a non-whitespace character\n text.value = text.value.replace(/^\\s*\\[!.*?\\]\\s*/, '')\n }\n\n // Extract content from paragraphs to avoid wrapping in <p> tags\n const contentChildren = []\n for (let i = headIndex; i < node.children.length; i++) {\n const child = node.children[i]\n if (child?.type === 'element' && child.tagName === 'p' && child.children) {\n contentChildren.push(...child.children)\n } else {\n contentChildren.push(child)\n }\n }\n\n // Replace the blockquote with a div containing the alert\n parent.children[index] = {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert', `markdown-alert-${alertType}`] },\n children: [\n {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert-icon'] },\n children: [],\n },\n {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert-content'] },\n children: [{ type: 'text', value: ' ' }, ...contentChildren],\n },\n ],\n }\n })\n }\n}\n"],"mappings":";;AAIA,IAAM,cAAc;CAAC;CAAQ;CAAO;CAAa;CAAW;CAAW;AAAS;AAGhF,SAAS,aAAa,MAAoB;CACxC,OAAO,KAAK,SAAS,UAAU,OAAO,KAAK,UAAU,YAAY,QAAQ,KAAK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F,OAAO,IACL,KAAK,MAAM,OAAO,MAAM,CAAC,MAAM,SAE9B,MAAM,MAAM,KAAK,MAAM,UAAU,CAAC,QAAQ,KAAK,KAAK,MAAM,MAAM,MAAM,CAAC,CAAC,IAEzE,KAAK,QAAQ,KAAK,MAAM,MAAM,MAAM,CAAC;QAGrC,KAAK,QAAQ,KAAK,MAAM,QAAQ,mBAAmB,EAAE;GAIvD,MAAM,kBAAkB,CAAC;GACzB,KAAK,IAAI,IAAI,WAAW,IAAI,KAAK,SAAS,QAAQ,KAAK;IACrD,MAAM,QAAQ,KAAK,SAAS;IAC5B,IAAI,OAAO,SAAS,aAAa,MAAM,YAAY,OAAO,MAAM,UAC9D,gBAAgB,KAAK,GAAG,MAAM,QAAQ;SAEtC,gBAAgB,KAAK,KAAK;GAE9B;GAGA,OAAO,SAAS,SAAS;IACvB,MAAM;IACN,SAAS;IACT,YAAY,EAAE,WAAW,CAAC,kBAAkB,kBAAkB,WAAW,EAAE;IAC3E,UAAU,CACR;KACE,MAAM;KACN,SAAS;KACT,YAAY,EAAE,WAAW,CAAC,qBAAqB,EAAE;KACjD,UAAU,CAAC;IACb,GACA;KACE,MAAM;KACN,SAAS;KACT,YAAY,EAAE,WAAW,CAAC,wBAAwB,EAAE;KACpD,UAAU,CAAC;MAAE,MAAM;MAAQ,OAAO;KAAI,GAAG,GAAG,eAAe;IAC7D,CACF;GACF;EACF,CAAC;CACH;AACF"}
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"lowlight",
|
|
17
17
|
"highlightjs"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.4.
|
|
19
|
+
"version": "0.4.17",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=22"
|
|
22
22
|
},
|
|
@@ -71,26 +71,21 @@
|
|
|
71
71
|
],
|
|
72
72
|
"sideEffects": false,
|
|
73
73
|
"dependencies": {
|
|
74
|
-
"
|
|
74
|
+
"@scalar/markdown": "0.1.3",
|
|
75
|
+
"hast-util-to-text": "^4.0.5",
|
|
75
76
|
"highlight.js": "^11.12.0",
|
|
76
77
|
"lowlight": "^3.3.0",
|
|
77
|
-
"rehype-external-links": "^3.0.0",
|
|
78
|
-
"rehype-format": "^5.0.1",
|
|
79
78
|
"rehype-parse": "^9.0.1",
|
|
80
|
-
"rehype-raw": "^7.0.0",
|
|
81
|
-
"rehype-sanitize": "^6.0.0",
|
|
82
79
|
"rehype-stringify": "^10.0.1",
|
|
83
|
-
"remark-gfm": "^4.0.1",
|
|
84
|
-
"remark-parse": "^11.0.0",
|
|
85
|
-
"remark-rehype": "^11.1.2",
|
|
86
|
-
"remark-stringify": "^11.0.0",
|
|
87
80
|
"unified": "^11.0.5",
|
|
88
81
|
"unist-util-visit": "^5.2.0"
|
|
89
82
|
},
|
|
90
83
|
"devDependencies": {
|
|
91
|
-
"@scalar/themes": "0.
|
|
84
|
+
"@scalar/themes": "0.21.1",
|
|
92
85
|
"@types/hast": "^3.0.5",
|
|
93
86
|
"@types/mdast": "^4.0.4",
|
|
87
|
+
"remark-parse": "^11.0.0",
|
|
88
|
+
"remark-rehype": "^11.1.2",
|
|
94
89
|
"vfile": "^6.0.3",
|
|
95
90
|
"vite": "8.3.8"
|
|
96
91
|
},
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { t as visit } from "./lib.js";
|
|
2
|
-
// src/rehype-alert/rehype-alert.ts
|
|
3
|
-
var ALERT_TYPES = [
|
|
4
|
-
"note",
|
|
5
|
-
"tip",
|
|
6
|
-
"important",
|
|
7
|
-
"warning",
|
|
8
|
-
"caution",
|
|
9
|
-
"success"
|
|
10
|
-
];
|
|
11
|
-
function isWhitespace(node) {
|
|
12
|
-
return node.type === "text" && typeof node.value === "string" && /^\s*$/.test(node.value);
|
|
13
|
-
}
|
|
14
|
-
function rehypeAlert() {
|
|
15
|
-
return (tree) => {
|
|
16
|
-
visit(tree, "element", (node, index, parent) => {
|
|
17
|
-
if (node.tagName !== "blockquote" || typeof index !== "number" || !parent || parent.type !== "root") return;
|
|
18
|
-
const headIndex = node.children.findIndex((child) => !isWhitespace(child));
|
|
19
|
-
if (headIndex === -1) return;
|
|
20
|
-
const head = node.children[headIndex];
|
|
21
|
-
if (head?.type !== "element" || head.tagName !== "p") return;
|
|
22
|
-
const text = head.children[0];
|
|
23
|
-
if (text?.type !== "text" || !text.value.startsWith("[!")) return;
|
|
24
|
-
const end = text.value.indexOf("]");
|
|
25
|
-
if (end === -1) return;
|
|
26
|
-
const alertType = text.value.slice(2, end).toLowerCase();
|
|
27
|
-
if (!ALERT_TYPES.includes(alertType)) return;
|
|
28
|
-
if (end + 1 === text.value.length) {
|
|
29
|
-
const next = head.children[1];
|
|
30
|
-
if (next) {
|
|
31
|
-
if (next.type !== "element" || next.tagName !== "br") return;
|
|
32
|
-
if (!head.children[2]) return;
|
|
33
|
-
head.children = head.children.slice(2);
|
|
34
|
-
const node = head.children[0];
|
|
35
|
-
if (node && node.type === "text" && node.value.charAt(0) === "\n") node.value = node.value.slice(1);
|
|
36
|
-
} else {
|
|
37
|
-
const nextIndex = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1]) ? headIndex + 2 : headIndex + 1;
|
|
38
|
-
if (nextIndex >= node.children.length || node.children[nextIndex]?.type !== "element") return;
|
|
39
|
-
node.children = node.children.slice(nextIndex);
|
|
40
|
-
}
|
|
41
|
-
} else if (text.value.charAt(end + 1) === "\n" && (end + 2 === text.value.length || !/^\s*$/.test(text.value.slice(end + 2)))) text.value = text.value.slice(end + 2);
|
|
42
|
-
else text.value = text.value.replace(/^\s*\[!.*?\]\s*/, "");
|
|
43
|
-
const contentChildren = [];
|
|
44
|
-
for (let i = headIndex; i < node.children.length; i++) {
|
|
45
|
-
const child = node.children[i];
|
|
46
|
-
if (child?.type === "element" && child.tagName === "p" && child.children) contentChildren.push(...child.children);
|
|
47
|
-
else contentChildren.push(child);
|
|
48
|
-
}
|
|
49
|
-
parent.children[index] = {
|
|
50
|
-
type: "element",
|
|
51
|
-
tagName: "div",
|
|
52
|
-
properties: { className: ["markdown-alert", `markdown-alert-${alertType}`] },
|
|
53
|
-
children: [{
|
|
54
|
-
type: "element",
|
|
55
|
-
tagName: "div",
|
|
56
|
-
properties: { className: ["markdown-alert-icon"] },
|
|
57
|
-
children: []
|
|
58
|
-
}, {
|
|
59
|
-
type: "element",
|
|
60
|
-
tagName: "div",
|
|
61
|
-
properties: { className: ["markdown-alert-content"] },
|
|
62
|
-
children: [{
|
|
63
|
-
type: "text",
|
|
64
|
-
value: " "
|
|
65
|
-
}, ...contentChildren]
|
|
66
|
-
}]
|
|
67
|
-
};
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
export { rehypeAlert as t };
|
|
72
|
-
|
|
73
|
-
//# sourceMappingURL=rehype-alert.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rehype-alert.js","names":[],"sources":["../../src/rehype-alert/rehype-alert.ts"],"sourcesContent":["import type { Element } from 'hast'\nimport type { Root } from 'mdast'\nimport { visit } from 'unist-util-visit'\n\nconst ALERT_TYPES = ['note', 'tip', 'important', 'warning', 'caution', 'success'] as const\n\n// Simple whitespace check function\nfunction isWhitespace(node: any): boolean {\n return node.type === 'text' && typeof node.value === 'string' && /^\\s*$/.test(node.value)\n}\n\nexport function rehypeAlert() {\n return (tree: Root) => {\n visit(tree, 'element', (node: Element, index, parent: any) => {\n if (node.tagName !== 'blockquote' || typeof index !== 'number' || !parent || parent.type !== 'root') {\n return\n }\n\n // Find the first non-whitespace child\n const headIndex = node.children.findIndex((child) => !isWhitespace(child))\n if (headIndex === -1) {\n return\n }\n\n const head = node.children[headIndex]\n\n if (head?.type !== 'element' || head.tagName !== 'p') {\n return\n }\n\n const text = head.children[0]\n if (text?.type !== 'text' || !text.value.startsWith('[!')) {\n return\n }\n\n const end = text.value.indexOf(']')\n if (end === -1) {\n return\n }\n\n // Extract the alert type\n const alertType = text.value.slice(2, end).toLowerCase() as (typeof ALERT_TYPES)[number]\n if (!ALERT_TYPES.includes(alertType)) {\n return\n }\n\n // Remove the blockquote if it's empty\n if (end + 1 === text.value.length) {\n const next = head.children[1]\n if (next) {\n if (next.type !== 'element' || next.tagName !== 'br') {\n return\n }\n if (!head.children[2]) {\n return\n }\n head.children = head.children.slice(2)\n const node = head.children[0]\n if (node && node.type === 'text' && node.value.charAt(0) === '\\n') {\n node.value = node.value.slice(1)\n }\n } else {\n const skipped = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1])\n const nextIndex = skipped ? headIndex + 2 : headIndex + 1\n if (nextIndex >= node.children.length || node.children[nextIndex]?.type !== 'element') {\n return\n }\n\n node.children = node.children.slice(nextIndex)\n }\n } else if (\n text.value.charAt(end + 1) === '\\n' &&\n // Check if the next character is a newline or a non-whitespace character\n (end + 2 === text.value.length || !/^\\s*$/.test(text.value.slice(end + 2)))\n ) {\n text.value = text.value.slice(end + 2)\n } else {\n // Remove the alert marker if it's not followed by a newline or a non-whitespace character\n text.value = text.value.replace(/^\\s*\\[!.*?\\]\\s*/, '')\n }\n\n // Extract content from paragraphs to avoid wrapping in <p> tags\n const contentChildren = []\n for (let i = headIndex; i < node.children.length; i++) {\n const child = node.children[i]\n if (child?.type === 'element' && child.tagName === 'p' && child.children) {\n contentChildren.push(...child.children)\n } else {\n contentChildren.push(child)\n }\n }\n\n // Replace the blockquote with a div containing the alert\n parent.children[index] = {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert', `markdown-alert-${alertType}`] },\n children: [\n {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert-icon'] },\n children: [],\n },\n {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert-content'] },\n children: [{ type: 'text', value: ' ' }, ...contentChildren],\n },\n ],\n }\n })\n }\n}\n"],"mappings":";;AAIA,IAAM,cAAc;CAAC;CAAQ;CAAO;CAAa;CAAW;CAAW;AAAS;AAGhF,SAAS,aAAa,MAAoB;CACxC,OAAO,KAAK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