| 1 | import type { Nodes, PhrasingContent, Root, Text } from "mdast"; |
| 2 | |
| 3 | export const ALERTS = |
| 4 | note: "Note", |
| 5 | tip: "Tip", |
| 6 | important: "Important", |
| 7 | warning: "Warning", |
| 8 | caution: "Caution", |
| 9 | } as const; |
| 10 | |
| 11 | export type AlertType |
| 12 | |
| 13 | const MARKER = / |
| 14 | |
| 15 | /** GitHub's alerts: a top-level blockquote whose first line is only `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]` or `[!CAUTION]`. One with nothing after the marker stays a blockquote, as on GitHub. */ |
| 16 | export function remarkAlerts() |
| 17 | return (tree: |
| 18 | for (const node |
| 19 | if (node.type |
| 20 | const [first] |
| 21 | if (first?.type |
| 22 | const [text] |
| 23 | if (text?.type |
| 24 | const match = |
| 25 | if (!match) |
| 26 | |
| 27 | const rest = |
| 28 | // the marker must end its line: `[!NOTE] **x**` is a quote that happens to start with brackets |
| 29 | if (!match[2 |
| 30 | const emptied = |
| 31 | if (emptied && |
| 32 | |
| 33 | if (rest) text |
| 34 | else first.children. |
| 35 | if (emptied) node |
| 36 | |
| 37 | const type = |
| 38 | node.data = |
| 39 | hName: "div", |
| 40 | hProperties: { |
| 41 | className: ["markdown-alert", |
| 42 | }, |
| 43 | }; |
| 44 | node.children.unshift |
| 45 | type: "paragraph", |
| 46 | data: { hProperties: { |
| 47 | children: [{ type: |
| 48 | }); |
| 49 | } |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | /** GitHub's comment rendering: every newline in issue, pull request and comment text is a line break, which a Markdown file does not do. */ |
| 54 | export function remarkBreaks() |
| 55 | const walk = |
| 56 | if (!("children" |
| 57 | node.children = |
| 58 | if (child.type |
| 59 | walk(child); |
| 60 | return [child]; |
| 61 | }) as typeof node |
| 62 | }; |
| 63 | return (tree: |
| 64 | } |
| 65 | |
| 66 | function split({ value |
| 67 | return value |
| 68 | .split(/\r?\n |
| 69 | .flatMap((line, |
| 70 | ...(index > |
| 71 | ...(line ? |
| 72 | ]); |
| 73 | } |