Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/web/mdrender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export function makeMdRender(): MdRenderApi {

function inlineMd(s: string): string {
let out = escape(s);
out = out.replace(/`([^`]+)`/g, (_m, code: string) => `<code>${code}</code>`);
out = out.replace(/`([^`]+)`/g, (_m, code: string) =>
`<code>${code.replace(/[*[\]]/g, (c) => `&#${c.charCodeAt(0)};`)}</code>`);
// ![alt](url) — must run before the link rule so the image syntax is
// consumed whole. Only published image sources survive: http(s), or the
// panel's own bundled-asset route (/__bili/blog-assets/*, see
Expand Down
44 changes: 44 additions & 0 deletions tests/mdrender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,50 @@ test("makeMdRender is self-contained (IIFE inlining contract)", async () => {
assert.equal(det.highlight('{"a": 1}', "json"), md.highlight('{"a": 1}', "json"));
});

const literalCodeSamples = [
"*literal* **also literal**",
"[guide](https://example.test/docs)",
"![diagram](https://example.test/image.png)",
"[blocked](javascript:alert(1))",
'<img src=x onerror=alert(1)> & "quotes"',
"plain_code",
];

function assertLiteralCode(html: string, expected: string): void {
const inner = /<code>([\s\S]*?)<\/code>/.exec(html)?.[1];
assert.notEqual(inner, undefined);
assert.ok(!inner!.includes("<"), "code contains text rather than nested markup");
const entities: Record<string, string> = { amp: "&", lt: "<", gt: ">", quot: '"' };
const text = inner!.replace(/&#(\d+);|&#x([a-f\d]+);|&(amp|lt|gt|quot);/gi, (_match, decimal, hex, named) =>
decimal ? String.fromCodePoint(Number(decimal)) : hex ? String.fromCodePoint(Number.parseInt(hex, 16)) : entities[named.toLowerCase()]!);
assert.equal(text, expected);
}

for (const sample of literalCodeSamples) {
test(`server Markdown preserves inline code literally: ${sample}`, () => {
assertLiteralCode(markdownToHtml(`\`${sample}\``), sample);
});
}

test("detached browser renderer keeps inline code literal", async () => {
const detached = await makeDetached();
for (const sample of literalCodeSamples) assertLiteralCode(detached.render(`\`${sample}\``), sample);
});

test("inline code keeps its literal content inside a link label", () => {
const html = md.render("[read `**literal**`](https://example.test/docs)");
assert.match(html, /<a href="https:\/\/example.test\/docs"/);
assertLiteralCode(html, "**literal**");
});

test("protecting inline code keeps surrounding emphasis and links active", () => {
const html = md.render("**bold** and `*literal*` and *italic* and [link](https://example.test/docs)");
assertLiteralCode(html, "*literal*");
assert.match(html, /<strong>bold<\/strong>/);
assert.match(html, /<em>italic<\/em>/);
assert.equal((html.match(/<a /g) ?? []).length, 1);
});

// --- escaping / XSS ---

test("escape turns & < > \" into entities (single quote passes through, as in the old renderer)", () => {
Expand Down
Loading