~james/core

core/web/src/lib/markup/inline.test.ts -rw-r--r-- 1.1 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { describe, expect, it } from "vitest";
import { renderInline } from "#lib/markup/inline.ts";

describe("renderInline", () => {
	it("keeps the allowed marks", () => {
		expect(renderInline("**b** *i* ~~s~~ `c`")).toBe(
			"<strong>b</strong> <em>i</em> <s>s</s> <code>c</code>"
		);
	});

	it("links labels and bare urls", () => {
		expect(renderInline("[docs](https://tangled.org/docs) https://tangled.org")).toBe(
			'<a href="https://tangled.org/docs" rel="nofollow ugc noopener noreferrer">docs</a> ' +
				'<a href="https://tangled.org" rel="nofollow ugc noopener noreferrer">https://tangled.org</a>'
		);
	});

	it("keeps the label text when links are off", () => {
		expect(renderInline("[docs](https://tangled.org/docs)", false)).toBe("docs");
	});

	it("drops unsafe link schemes", () => {
		expect(renderInline("[x](javascript:alert(1))")).not.toContain("<a");
	});

	it("escapes raw html and drops other tags", () => {
		expect(renderInline("<b>x</b>")).toBe("&lt;b&gt;x&lt;/b&gt;");
		expect(renderInline("![alt](https://tangled.org/a.png)")).not.toContain("<img");
	});
});