-
Notifications
You must be signed in to change notification settings - Fork 70
feat(markdown): render GitHub-style alerts in the webview (#258) #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,17 @@ | ||
| import { describe, expect, it } from "vitest" | ||
|
|
||
| import { countMarkdownHeadings, hasComplexMarkdown } from "../markdown" | ||
| import { ALERT_TYPES, countMarkdownHeadings, hasComplexMarkdown, remarkGithubAlerts } from "../markdown" | ||
|
|
||
| // Minimal mdast builders so we can exercise the plugin without a full parser. | ||
| const text = (value: string) => ({ type: "text", value }) | ||
| const paragraph = (...children: any[]) => ({ type: "paragraph", children }) | ||
| const blockquote = (...children: any[]) => ({ type: "blockquote", children }) | ||
| const root = (...children: any[]) => ({ type: "root", children }) | ||
|
|
||
| const transform = (tree: any) => { | ||
| remarkGithubAlerts()(tree) | ||
| return tree | ||
| } | ||
|
|
||
| describe("markdown heading helpers", () => { | ||
| it("returns 0 for empty or undefined", () => { | ||
|
|
@@ -30,3 +41,78 @@ describe("markdown heading helpers", () => { | |
| expect(hasComplexMarkdown("# One\n## Two")).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe("remarkGithubAlerts", () => { | ||
| it.each(ALERT_TYPES)("annotates a [!%s] alert blockquote", (type) => { | ||
| const upper = type.toUpperCase() | ||
| const tree = root(blockquote(paragraph(text(`[!${upper}]\nBody text`)))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does remark actually produce |
||
|
|
||
| transform(tree) | ||
|
|
||
| const bq = tree.children[0] | ||
| expect(bq.data.hProperties["data-alert-type"]).toBe(type) | ||
| expect(bq.data.hProperties.className).toBe(`markdown-alert markdown-alert-${type}`) | ||
|
|
||
| // Marker is stripped from the rendered content. | ||
| expect(bq.children[0].children[0].value).toBe("Body text") | ||
| }) | ||
|
|
||
| it("recognizes markers case-insensitively", () => { | ||
| const tree = root(blockquote(paragraph(text("[!Note]\nhi")))) | ||
|
|
||
| transform(tree) | ||
|
|
||
| expect(tree.children[0].data.hProperties["data-alert-type"]).toBe("note") | ||
| expect(tree.children[0].children[0].children[0].value).toBe("hi") | ||
| }) | ||
|
|
||
| it("removes the marker paragraph when it has no following inline content", () => { | ||
| // `> [!NOTE]` on its own line followed by a separate paragraph. | ||
| const tree = root(blockquote(paragraph(text("[!NOTE]\n")), paragraph(text("Body on next line")))) | ||
|
|
||
| transform(tree) | ||
|
|
||
| const bq = tree.children[0] | ||
| expect(bq.data.hProperties["data-alert-type"]).toBe("note") | ||
| // The emptied marker paragraph is dropped; body paragraph remains. | ||
| expect(bq.children).toHaveLength(1) | ||
| expect(bq.children[0].children[0].value).toBe("Body on next line") | ||
| }) | ||
|
|
||
| it("leaves a normal blockquote untouched", () => { | ||
| const tree = root(blockquote(paragraph(text("Just a quote, not an alert.")))) | ||
|
|
||
| transform(tree) | ||
|
|
||
| const bq = tree.children[0] | ||
| expect(bq.data).toBeUndefined() | ||
| expect(bq.children[0].children[0].value).toBe("Just a quote, not an alert.") | ||
| }) | ||
|
|
||
| it("ignores unsupported markers and renders them as normal blockquotes", () => { | ||
| const tree = root(blockquote(paragraph(text("[!INFO]\nNot a real alert type")))) | ||
|
|
||
| transform(tree) | ||
|
|
||
| const bq = tree.children[0] | ||
| expect(bq.data).toBeUndefined() | ||
| expect(bq.children[0].children[0].value).toBe("[!INFO]\nNot a real alert type") | ||
| }) | ||
|
|
||
| it("does not treat a marker in the middle of text as an alert", () => { | ||
| const tree = root(blockquote(paragraph(text("Some text [!NOTE] still a quote")))) | ||
|
|
||
| transform(tree) | ||
|
|
||
| expect(tree.children[0].data).toBeUndefined() | ||
| }) | ||
|
|
||
| it("annotates nested alert blockquotes", () => { | ||
| const tree = root(blockquote(blockquote(paragraph(text("[!TIP]\nnested"))))) | ||
|
|
||
| transform(tree) | ||
|
|
||
| const inner = tree.children[0].children[0] | ||
| expect(inner.data.hProperties["data-alert-type"]).toBe("tip") | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,3 +21,91 @@ export function countMarkdownHeadings(text: string | undefined): number { | |||||
| export function hasComplexMarkdown(text: string | undefined): boolean { | ||||||
| return countMarkdownHeadings(text) >= 2 | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * GitHub-style Markdown alert types, mapped to their lower-cased identifiers. | ||||||
| * @see https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts | ||||||
| */ | ||||||
| export const ALERT_TYPES = ["note", "tip", "important", "warning", "caution"] as const | ||||||
|
|
||||||
| export type AlertType = (typeof ALERT_TYPES)[number] | ||||||
|
|
||||||
| // Matches a leading alert marker like "[!NOTE]" (case-insensitive) optionally | ||||||
| // followed by trailing whitespace/newline on the first line of a blockquote. | ||||||
| const ALERT_MARKER_REGEX = /^\[!(note|tip|important|warning|caution)\][^\S\r\n]*\r?\n?/i | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The alternation group here (
Suggested change
|
||||||
|
|
||||||
| /** | ||||||
| * remark plugin that detects GitHub-style alerts inside blockquotes | ||||||
| * (e.g. `> [!NOTE]`) and annotates the blockquote node so it can be rendered | ||||||
| * as a distinct alert block. | ||||||
| * | ||||||
| * The marker text is stripped from the rendered content and the recognized | ||||||
| * alert type is exposed via the `data-alert-type` attribute plus matching | ||||||
| * `markdown-alert*` class names on the emitted `<blockquote>` element. | ||||||
| * | ||||||
| * Blockquotes that do not begin with a supported marker are left untouched, so | ||||||
| * normal blockquotes continue to render exactly as before. | ||||||
| */ | ||||||
| export function remarkGithubAlerts() { | ||||||
| return (tree: any) => { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| walkAlertBlockquotes(tree) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function walkAlertBlockquotes(node: any): void { | ||||||
| if (!node || typeof node !== "object") { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| if (node.type === "blockquote") { | ||||||
| annotateAlertBlockquote(node) | ||||||
| } | ||||||
|
|
||||||
| if (Array.isArray(node.children)) { | ||||||
| for (const child of node.children) { | ||||||
| walkAlertBlockquotes(child) | ||||||
| } | ||||||
| } | ||||||
|
Comment on lines
+55
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| } | ||||||
|
|
||||||
| function annotateAlertBlockquote(node: any): void { | ||||||
| const firstChild = node.children?.[0] | ||||||
|
|
||||||
| // The marker must live in the first paragraph's first text node. | ||||||
| if (!firstChild || firstChild.type !== "paragraph") { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| const firstText = firstChild.children?.[0] | ||||||
|
|
||||||
| if (!firstText || firstText.type !== "text" || typeof firstText.value !== "string") { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| const match = firstText.value.match(ALERT_MARKER_REGEX) | ||||||
|
|
||||||
| if (!match) { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| const alertType = match[1].toLowerCase() as AlertType | ||||||
|
|
||||||
| // Strip the marker (and the following newline) from the rendered content. | ||||||
| firstText.value = firstText.value.slice(match[0].length) | ||||||
|
|
||||||
| // Drop the now-empty leading text node so the alert body starts cleanly. | ||||||
| if (firstText.value === "") { | ||||||
| firstChild.children.shift() | ||||||
| } | ||||||
|
|
||||||
| // If the paragraph became empty (marker was on its own line with no inline | ||||||
| // content following it), remove it entirely. | ||||||
| if (firstChild.children.length === 0) { | ||||||
| node.children.shift() | ||||||
| } | ||||||
|
|
||||||
| node.data = node.data || {} | ||||||
| const hProperties = (node.data.hProperties = node.data.hProperties || {}) | ||||||
| hProperties.className = `markdown-alert markdown-alert-${alertType}` | ||||||
| hProperties["data-alert-type"] = alertType | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...propsspreads all hast-derived attributes onto the DOM element. Today onlydata-alert-typeandclassNameare set by the plugin, but any futurehPropertyaddition would silently reach the DOM. Would it be safer to destructure only the known props and discard the rest?