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
37 changes: 31 additions & 6 deletions src/lib/markdown-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: `<my (1).png>`
// - 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 <img …src="…">. 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.
Expand All @@ -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: `<pics\(1).png>`. HTML
* attributes have no such escapes, so `<img>` 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.
Expand Down Expand Up @@ -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] ?? "") });
Expand Down Expand Up @@ -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) => {
Expand Down
53 changes: 53 additions & 0 deletions tests/markdown-images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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](<e (f).png>) ![d](g%20(h).png) ![e](u(v.png) ![f](<x\\>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](<a<b c.png>)")).toEqual([
"a((b)).png",
"a(b(c)d).png",
"a<b c.png",
]);
});

it("does not make a path from a backslash before a space", () => {
expect(findRelativeRefs("![a](my\\ photo.png)")).toEqual([]);
});

it("keeps a backslash before a parenthesis inside angle brackets", () => {
expect(findRelativeRefs("![a](<pics\\(1).png>)")).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('<img src="a\\(b\\).png">')).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")');
Expand Down
Loading