|
| 1 | +import { renderToStaticMarkup } from "react-dom/server"; |
| 2 | +import ReactMarkdown, { defaultUrlTransform } from "react-markdown"; |
| 3 | +import rehypeSanitize, { defaultSchema } from "rehype-sanitize"; |
| 4 | +import { describe, expect, it } from "vite-plus/test"; |
| 5 | + |
| 6 | +import { remarkRewriteWindowsFileLinks, rewriteMarkdownFileUriHref } from "./markdown-links"; |
| 7 | + |
| 8 | +const sanitizeSchema = { |
| 9 | + ...defaultSchema, |
| 10 | + protocols: { |
| 11 | + ...defaultSchema.protocols, |
| 12 | + href: [...(defaultSchema.protocols?.href ?? []), "file"], |
| 13 | + }, |
| 14 | +} satisfies Parameters<typeof rehypeSanitize>[0]; |
| 15 | + |
| 16 | +function renderMarkdown(markdown: string): string { |
| 17 | + return renderToStaticMarkup( |
| 18 | + <ReactMarkdown |
| 19 | + remarkPlugins={[remarkRewriteWindowsFileLinks]} |
| 20 | + rehypePlugins={[[rehypeSanitize, sanitizeSchema]]} |
| 21 | + urlTransform={(href) => rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href)} |
| 22 | + > |
| 23 | + {markdown} |
| 24 | + </ReactMarkdown>, |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +describe("Windows markdown file link rendering", () => { |
| 29 | + it("preserves a drive-path href through HTML sanitization", () => { |
| 30 | + const path = "C:/Users/mike/dev-stuff/t3code/apps/web/src/markdown-links.ts"; |
| 31 | + |
| 32 | + expect(renderMarkdown(`[markdown-links.ts](${path}) is open above.`)).toContain( |
| 33 | + `<a href="${path}">markdown-links.ts</a>`, |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it("still removes unsafe schemes", () => { |
| 38 | + expect(renderMarkdown("[unsafe](javascript:alert(1))")).not.toContain("href="); |
| 39 | + }); |
| 40 | +}); |
0 commit comments