Skip to content

Commit 72974d0

Browse files
Fix GitHub-style Markdown alert rendering
1 parent f11ddbb commit 72974d0

1 file changed

Lines changed: 80 additions & 4 deletions

File tree

app/docs.html

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
@@ -65,11 +65,38 @@
6565
margin-bottom: 0;
6666
}
6767

68-
/* Style for [!Important] callouts */
69-
.markdown-body blockquote:has(strong:first-child) {
68+
.markdown-alert {
69+
padding: 0.5rem 1rem;
70+
margin: 0 0 1rem 0;
71+
border-left: 0.25rem solid #30363d;
72+
background-color: #161b22;
73+
}
74+
75+
.markdown-alert-title {
76+
font-weight: 600;
77+
margin-bottom: 0.25rem;
78+
}
79+
80+
.markdown-alert-note {
81+
border-left-color: #58a6ff;
82+
}
83+
84+
.markdown-alert-tip {
85+
border-left-color: #3fb950;
86+
}
87+
88+
.markdown-alert-important {
7089
border-left-color: #a371f7;
7190
}
7291

92+
.markdown-alert-warning {
93+
border-left-color: #d29922;
94+
}
95+
96+
.markdown-alert-caution {
97+
border-left-color: #f85149;
98+
}
99+
73100
/* Card fills the width */
74101
.doc-card {
75102
min-height: calc(100vh - 120px);
@@ -380,6 +407,52 @@
380407
.replace(/src="\.\/images\//g, `src="${GITHUB_IMAGES_BASE}`);
381408
}
382409

410+
// Convert GitHub-style alerts to the same HTML structure GitHub renders
411+
function transformMarkdownAlerts(html) {
412+
const parser = new DOMParser();
413+
const doc = parser.parseFromString(html, "text/html");
414+
const alertTypes = {
415+
NOTE: "note",
416+
TIP: "tip",
417+
IMPORTANT: "important",
418+
WARNING: "warning",
419+
CAUTION: "caution",
420+
};
421+
422+
doc.querySelectorAll("blockquote").forEach((blockquote) => {
423+
const firstParagraph = blockquote.querySelector("p");
424+
if (!firstParagraph) return;
425+
426+
const text = firstParagraph.textContent || "";
427+
const match = text.match(
428+
/^\s*\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]\s*/i,
429+
);
430+
if (!match) return;
431+
432+
const type = match[1].toUpperCase();
433+
const slug = alertTypes[type];
434+
if (!slug) return;
435+
436+
firstParagraph.textContent = text.replace(match[0], "");
437+
438+
const alert = doc.createElement("div");
439+
alert.className = `markdown-alert markdown-alert-${slug}`;
440+
441+
const title = doc.createElement("p");
442+
title.className = "markdown-alert-title";
443+
title.textContent = type.charAt(0) + type.slice(1).toLowerCase();
444+
alert.appendChild(title);
445+
446+
while (blockquote.firstChild) {
447+
alert.appendChild(blockquote.firstChild);
448+
}
449+
450+
blockquote.replaceWith(alert);
451+
});
452+
453+
return doc.body.innerHTML;
454+
}
455+
383456
// Load and render markdown
384457
async function loadMarkdown(docName) {
385458
const spinner = document.getElementById("loadingSpinner");
@@ -408,6 +481,9 @@
408481
// Fix image URLs
409482
html = fixImageUrls(html);
410483

484+
// Convert GitHub-style alerts
485+
html = transformMarkdownAlerts(html);
486+
411487
// Sanitize HTML
412488
html = DOMPurify.sanitize(html, {
413489
ADD_TAGS: ["img"],
@@ -439,7 +515,7 @@
439515
if (href && href.startsWith("docs.html?doc=")) {
440516
e.preventDefault();
441517
const docName = new URLSearchParams(href.split("?")[1]).get(
442-
"doc"
518+
"doc",
443519
);
444520
if (docName) {
445521
window.history.pushState({}, "", href);

0 commit comments

Comments
 (0)