diff --git a/src/lib/markdown-images.ts b/src/lib/markdown-images.ts index 82ada0c..57c58a2 100644 --- a/src/lib/markdown-images.ts +++ b/src/lib/markdown-images.ts @@ -24,7 +24,7 @@ export const MAX_IMAGE_COUNT = 20; const MAX_NAMED_FAILURES = 10; export interface ImageFailure { - /** The reference as written, minus `<…>` brackets and surrounding space. */ + /** The reference, minus `<…>` brackets, surrounding space and markdown escapes. */ src: string; /** Short reason, e.g. "could not be fetched (HTTP 404)". */ reason: string; @@ -40,9 +40,22 @@ export type PreparedExport = | { ok: true; markdown: string } | { ok: false; message: string }; -// Markdown image: ![alt](src "optional title"). Group 2 is the src (possibly -// wrapped in <…>); group 3 is the optional title with its leading whitespace. -const MD_IMAGE = /!\[([^\]]*)\]\(\s*(<[^>]+>|[^)\s]+)((?:\s+(?:"[^"]*"|'[^']*'))?)\s*\)/g; +// A markdown image destination's path characters, with backslash escapes. A +// backslash before whitespace is not an escape, so `my\ photo.png` is no path. +const MD_PATH_RUN = String.raw`[^\s()\\]*(?:\\\S[^\s()\\]*)*`; +// A bare destination with parentheses balanced to two levels: `a(b(c)).png`. +// Each loop is unrolled because nesting the quantifiers backtracks +// exponentially on an unterminated `![a](xxxx`. +const MD_BARE_DEST = String.raw`(?=[^\s)])${MD_PATH_RUN}(?:\(${MD_PATH_RUN}(?:\(${MD_PATH_RUN}\)${MD_PATH_RUN})*\)${MD_PATH_RUN})*`; +// Markdown image: ![alt](src "optional title"). Group 3 is the optional title +// with its leading whitespace. Group 2 is the src, one of: +// - angle brackets, where `\>` is an escape: `` +// - bare, with balanced or backslash-escaped parentheses: `a(b).png`, `a\(b.png` +// - bare, up to the first `)`, for an unbalanced `(`: `a(b.png` +const MD_IMAGE = new RegExp( + String.raw`!\[([^\]]*)\]\(\s*(<[^>\n\\]*(?:\\.[^>\n\\]*)*>|${MD_BARE_DEST}|[^)\s]+)((?:\s+(?:"[^"]*"|'[^']*'))?)\s*\)`, + "g", +); // HTML . Group 1 is everything up to and including `src=`, // group 3/4 is the quoted value (double/single). `[\s/]+` before `src`, not a // bare `\b`, so `data-src=` is not mistaken for the real attribute. @@ -67,6 +80,18 @@ function cleanSrc(raw: string): string { return trimmed.startsWith("<") && trimmed.endsWith(">") ? trimmed.slice(1, -1).trim() : trimmed; } +/** + * The src of a markdown image: `cleanSrc`, then undo the escapes markdown allows + * in a destination. Parentheses need no escape inside angle brackets, so a + * backslash before one there is a path separator: ``. HTML + * attributes have no such escapes, so `` srcs stay on `cleanSrc`. + */ +function cleanMarkdownSrc(raw: string): string { + const trimmed = raw.trim(); + const angle = trimmed.startsWith("<") && trimmed.endsWith(">"); + return cleanSrc(raw).replace(angle ? /\\([<>])/g : /\\([()<>])/g, "$1"); +} + /** * Replace fenced code blocks and inline code spans with sentinel tokens so the * image scan never touches them, returning a `restore` to swap them back in. @@ -98,7 +123,7 @@ function collectRefs(source: string): { masked: string; restore: (value: string) const { masked, restore } = maskCode(source); const found: { index: number; src: string }[] = []; for (const match of masked.matchAll(MD_IMAGE)) { - found.push({ index: match.index, src: cleanSrc(match[2] ?? "") }); + found.push({ index: match.index, src: cleanMarkdownSrc(match[2] ?? "") }); } for (const match of masked.matchAll(HTML_IMAGE_SRC)) { found.push({ index: match.index, src: cleanSrc(match[3] ?? match[4] ?? "") }); @@ -159,7 +184,7 @@ export async function inlineRelativeImages( if (uris.size === 0) return { markdown: source, failures }; let out = masked.replace(MD_IMAGE, (whole, alt: string, url: string, title: string | undefined) => { - const uri = uris.get(cleanSrc(url)); + const uri = uris.get(cleanMarkdownSrc(url)); return uri ? `![${alt}](${uri}${title ?? ""})` : whole; }); out = out.replace(HTML_IMAGE_SRC, (whole, prefix: string, _quoted: string, doubleQuoted: string | undefined, singleQuoted: string | undefined) => { diff --git a/tests/markdown-images.test.ts b/tests/markdown-images.test.ts index 96933c6..6a3db6a 100644 --- a/tests/markdown-images.test.ts +++ b/tests/markdown-images.test.ts @@ -57,6 +57,59 @@ describe("inlineRelativeImages", () => { ); }); + it("finds paths with parentheses in every destination form", () => { + expect( + findRelativeRefs( + '![a](a(b).png) ![b](c\\(d\\).png) ![c]() ![d](g%20(h).png) ![e](u(v.png) ![f](y.png>)', + ), + ).toEqual(["a(b).png", "c(d).png", "e (f).png", "g%20(h).png", "u(v.png", "x>y.png"]); + }); + + it("finds nested parentheses and an unescaped < in full", () => { + expect(findRelativeRefs("![a](a((b)).png) ![b](a(b(c)d).png) ![c]()")).toEqual([ + "a((b)).png", + "a(b(c)d).png", + "a { + expect(findRelativeRefs("![a](my\\ photo.png)")).toEqual([]); + }); + + it("keeps a backslash before a parenthesis inside angle brackets", () => { + expect(findRelativeRefs("![a]()")).toEqual(["pics\\(1).png"]); + }); + + it("embeds a path with parentheses and keeps what follows the image", async () => { + const { markdown, failures } = await inlineRelativeImages( + '![a](a(b).png "the (title)") (see above) ![b](plain.png) (note)', + okResolve, + ); + expect(markdown).toBe( + '![a](data:image/png;base64,RESOLVED(a(b).png) "the (title)") (see above) ![b](data:image/png;base64,RESOLVED(plain.png)) (note)', + ); + expect(failures).toEqual([]); + }); + + it("does not treat an img src as a markdown destination", () => { + expect(findRelativeRefs('')).toEqual(["a\\(b\\).png"]); + }); + + it("does not stall on an unterminated destination", () => { + for (const tail of ["(a)".repeat(50_000), "((a)".repeat(50_000), "\\(".repeat(50_000), " ".repeat(160_000)]) { + const started = Date.now(); + findRelativeRefs(`![x](${tail}`); + expect(Date.now() - started).toBeLessThan(2000); + } + }); + + it("leaves a multi-megabyte data URI alone", () => { + const data = `data:image/png;base64,${"A".repeat(16_000_000)}`; + expect(findRelativeRefs(`![x](${data})`)).toEqual([]); + expect(findRelativeRefs(`![x](<${data}>)`)).toEqual([]); + }); + it("preserves a Markdown title after the URL", async () => { const { markdown: out } = await inlineRelativeImages('![shot](shot.png "the title")', okResolve); expect(out).toBe('![shot](data:image/png;base64,RESOLVED(shot.png) "the title")');