From e58750fe89ec44113b28e74f29679db81f85602b Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Thu, 18 Jan 2024 12:12:41 +0100 Subject: [PATCH] chore: add more test coverage (#88) --- deno.json | 2 +- deps.ts | 2 +- mod.ts | 9 +++++---- test/test.ts | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/deno.json b/deno.json index 4c8adfa..fce9cd2 100644 --- a/deno.json +++ b/deno.json @@ -2,7 +2,7 @@ "tasks": { "build": "deno run --allow-read --allow-write --allow-net --allow-run --allow-env ./style/patch.ts && deno fmt", "check:types": "deno check **/*.ts", - "coverage": "deno test --allow-read --coverage=cov_profile", + "coverage": "rm -rf cov_profile && deno test --allow-read --coverage=cov_profile", "dev": "deno run -A --unstable --watch --no-check ./example/main.ts", "ok": "deno fmt --check && deno lint && deno task check:types && deno task test", "report": "deno coverage cov_profile --html", diff --git a/deps.ts b/deps.ts index 439c813..7199448 100644 --- a/deps.ts +++ b/deps.ts @@ -21,4 +21,4 @@ export { default as sanitizeHtml } from "https://esm.sh/sanitize-html@2.11.0?tar export { escape as htmlEscape } from "https://esm.sh/he@1.2.0?pin=v135"; -export { default as katex } from "https://esm.sh/katex@0.16.9/dist/katex.mjs?pin=v135"; +export { default as katex } from "https://esm.sh/katex@0.16.9?pin=v135"; diff --git a/mod.ts b/mod.ts index 56924fd..4027e82 100644 --- a/mod.ts +++ b/mod.ts @@ -36,8 +36,8 @@ export class Renderer extends Marked.Renderer { return `${text}`; } - image(src: string, title: string | null, alt: string | null) { - return `${alt ?? `; + image(src: string, title: string | null, alt: string) { + return `${alt}`; } code(code: string, language?: string) { @@ -246,7 +246,7 @@ export function render(markdown: string, opts: RenderOptions = {}): string { allowedTags, allowedAttributes: { ...sanitizeHtml.defaults.allowedAttributes, - img: ["src", "alt", "height", "width", "align"], + img: ["src", "alt", "height", "width", "align", "title"], video: [ "src", "alt", @@ -258,8 +258,9 @@ export function render(markdown: string, opts: RenderOptions = {}): string { "playsinline", "poster", "controls", + "title", ], - a: ["id", "aria-hidden", "href", "tabindex", "rel", "target"], + a: ["id", "aria-hidden", "href", "tabindex", "rel", "target", "title"], svg: ["viewbox", "width", "height", "aria-hidden", "background"], path: ["fill-rule", "d"], circle: ["cx", "cy", "r", "stroke", "stroke-width", "fill", "alpha"], diff --git a/test/test.ts b/test/test.ts index 36913bb..20f61a8 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,4 +1,7 @@ -import { assertEquals } from "https://deno.land/std@0.211.0/assert/assert_equals.ts"; +import { + assertEquals, + assertStringIncludes, +} from "https://deno.land/std@0.211.0/assert/mod.ts"; import { DOMParser } from "https://deno.land/x/deno_dom@v0.1.43/deno-dom-wasm.ts"; import { render, Renderer } from "../mod.ts"; @@ -232,3 +235,55 @@ Deno.test( assertEquals(html, expected.trim()); }, ); + +Deno.test("image title and no alt", () => { + const markdown = `![](image.jpg "best title")`; + const expected = `

\n`; + + const html = render(markdown); + assertEquals(html, expected); +}); + +Deno.test("js language", () => { + const markdown = "```js\nconst foo = 'bar';\n```"; + const expected = + `
const foo = 'bar';
`; + + const html = render(markdown); + assertEquals(html, expected); +}); + +Deno.test("link with title", () => { + const markdown = `[link](https://example.com "asdf")`; + const expected = + `

link

\n`; + const html = render(markdown); + assertEquals(html, expected); +}); + +Deno.test("expect console warning from invalid math", () => { + const originalWarn = console.warn; + const warnCalls: string[] = []; + console.warn = (...args) => { + warnCalls.push(args[0].message); + }; + + const html = render("$$ +& $$", { allowMath: true }); + const expected = + `

$$ +&

\n`; + assertEquals(html, expected); + assertStringIncludes( + warnCalls[0], + "KaTeX parse error: Expected 'EOF', got '&' at position 2: +&̲", + ); + + const html2 = render(" $&$", { allowMath: true }); + const expected2 = `

$&$

\n`; + assertEquals(html2, expected2); + assertStringIncludes( + warnCalls[1], + "KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲", + ); + + console.warn = originalWarn; +});