21 lines
1.2 KiB
JavaScript
21 lines
1.2 KiB
JavaScript
/* Local, pinned Markdown renderer compatible with the marked.parse API used by the chat UI. */
|
|
(function () {
|
|
const escape = value => String(value).replace(/[&<>"']/g, character => ({"&":"&","<":"<",">":">","\"":""","'":"'"}[character]));
|
|
const inline = value => escape(value)
|
|
.replace(/`([^`]+)`/g, "<code>$1</code>")
|
|
.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>")
|
|
.replace(/__([^_]+)__/g, "<strong>$1</strong>")
|
|
.replace(/\*([^*]+)\*/g, "<em>$1</em>")
|
|
.replace(/_([^_]+)_/g, "<em>$1</em>")
|
|
.replace(/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g, '<a href="$2" rel="noreferrer noopener">$1</a>');
|
|
window.marked = {parse(markdown) {
|
|
return String(markdown || "").split(/\n{2,}/).map(block => {
|
|
const lines = block.split("\n");
|
|
if (/^### /.test(lines[0])) return `<h3>${inline(lines[0].slice(4))}</h3>`;
|
|
if (/^## /.test(lines[0])) return `<h2>${inline(lines[0].slice(3))}</h2>`;
|
|
if (/^# /.test(lines[0])) return `<h1>${inline(lines[0].slice(2))}</h1>`;
|
|
return `<p>${lines.map(inline).join("<br>")}</p>`;
|
|
}).join("");
|
|
}};
|
|
}());
|