diff --git a/src/html.ts b/src/html.ts index 98c044717..a131dffe1 100644 --- a/src/html.ts +++ b/src/html.ts @@ -1,24 +1,13 @@ /* eslint-disable import/no-named-as-default-member */ +import {extname} from "node:path/posix"; import he from "he"; import hljs from "highlight.js"; import type {DOMWindow} from "jsdom"; import {JSDOM, VirtualConsole} from "jsdom"; import {isAssetPath, parseRelativeUrl, relativePath, resolveLocalPath, resolvePath} from "./path.js"; -const ASSET_ATTRIBUTES: readonly [selector: string, src: string][] = [ - ["a[href][download]", "href"], - ["audio source[src]", "src"], - ["audio[src]", "src"], - ["img[src]", "src"], - ["img[srcset]", "srcset"], - ["link[href]", "href"], - ["picture source[srcset]", "srcset"], - ["video source[src]", "src"], - ["video[src]", "src"] -]; - -const PATH_ATTRIBUTES: readonly [selector: string, src: string][] = [ - ["a[href]", "href"], +const ASSET_ATTRIBUTES: readonly [selector: string, src: string, skip?: (source: string) => boolean][] = [ + ["a[href]", "href", (p: string) => !(p = extname(p)) || p === ".html"], ["audio source[src]", "src"], ["audio[src]", "src"], ["img[src]", "src"], @@ -67,10 +56,11 @@ export function findAssets(html: string, path: string): Assets { } }; - for (const [selector, src] of ASSET_ATTRIBUTES) { + for (const [selector, src, skip] of ASSET_ATTRIBUTES) { for (const element of document.querySelectorAll(selector)) { if (isExternal(element)) continue; const source = decodeURI(element.getAttribute(src)!); + if (skip?.(source)) continue; if (src === "srcset") { for (const s of parseSrcset(source)) { maybeFile(s); @@ -126,7 +116,7 @@ export function rewriteHtmlPaths(html: string, path: string): string { return isAssetPath(specifier) ? relativePath(path, specifier) : specifier; }; - for (const [selector, src] of PATH_ATTRIBUTES) { + for (const [selector, src] of ASSET_ATTRIBUTES) { for (const element of document.querySelectorAll(selector)) { if (isExternal(element)) continue; const source = decodeURI(element.getAttribute(src)!); @@ -154,10 +144,11 @@ export function rewriteHtml( return isAssetPath(specifier) ? resolveFile(specifier) : resolveImport(specifier); }; - for (const [selector, src] of ASSET_ATTRIBUTES) { + for (const [selector, src, skip] of ASSET_ATTRIBUTES) { for (const element of document.querySelectorAll(selector)) { if (isExternal(element)) continue; const source = decodeURI(element.getAttribute(src)!); + if (skip?.(source)) continue; element.setAttribute(src, src === "srcset" ? resolveSrcset(source, resolvePath) : encodeURI(resolvePath(source))); } } diff --git a/test/input/build/assets/horse.jpg b/test/input/build/assets/horse.jpg new file mode 100644 index 000000000..016b4a1cb Binary files /dev/null and b/test/input/build/assets/horse.jpg differ diff --git a/test/input/build/assets/horse.md b/test/input/build/assets/horse.md new file mode 100644 index 000000000..92189c612 --- /dev/null +++ b/test/input/build/assets/horse.md @@ -0,0 +1,9 @@ +# horse + +horse.jpg + +horse (md) + +horse.html + +horse.md diff --git a/test/output/build/assets/_file/horse.2d33a223.jpg b/test/output/build/assets/_file/horse.2d33a223.jpg new file mode 100644 index 000000000..016b4a1cb Binary files /dev/null and b/test/output/build/assets/_file/horse.2d33a223.jpg differ diff --git a/test/output/build/assets/_file/horse.dbb90817.md b/test/output/build/assets/_file/horse.dbb90817.md new file mode 100644 index 000000000..92189c612 --- /dev/null +++ b/test/output/build/assets/_file/horse.dbb90817.md @@ -0,0 +1,9 @@ +# horse + +horse.jpg + +horse (md) + +horse.html + +horse.md diff --git a/test/output/build/assets/_observablehq/client.00000001.js b/test/output/build/assets/_observablehq/client.00000001.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/output/build/assets/_observablehq/runtime.00000002.js b/test/output/build/assets/_observablehq/runtime.00000002.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/output/build/assets/_observablehq/stdlib.00000003.js b/test/output/build/assets/_observablehq/stdlib.00000003.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/output/build/assets/_observablehq/theme-air,near-midnight.00000004.css b/test/output/build/assets/_observablehq/theme-air,near-midnight.00000004.css new file mode 100644 index 000000000..e69de29bb diff --git a/test/output/build/assets/horse.html b/test/output/build/assets/horse.html new file mode 100644 index 000000000..83bb02525 --- /dev/null +++ b/test/output/build/assets/horse.html @@ -0,0 +1,52 @@ + + +
+ + + +