Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

promote links to file assets unless we already know they are pages #1354

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 8 additions & 17 deletions src/html.ts
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)!);
Expand Down Expand Up @@ -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)));
}
}
Expand Down
Binary file added test/input/build/assets/horse.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/input/build/assets/horse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# horse

<a href="horse.jpg">horse.jpg</a>

<a href="horse">horse (md)</a>

<a href="horse.html">horse.html</a>

<a href="horse.md">horse.md</a>
Binary file added test/output/build/assets/_file/horse.2d33a223.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/output/build/assets/_file/horse.dbb90817.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# horse

<a href="horse.jpg">horse.jpg</a>

<a href="horse">horse (md)</a>

<a href="horse.html">horse.html</a>

<a href="horse.md">horse.md</a>
Empty file.
Empty file.
Empty file.
52 changes: 52 additions & 0 deletions test/output/build/assets/horse.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="generator" content="Observable Framework v1.0.0-test">
<title>horse</title>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&amp;display=swap" crossorigin>
<link rel="preload" as="style" href="./_observablehq/theme-air,near-midnight.00000004.css">
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&amp;display=swap" crossorigin>
<link rel="stylesheet" type="text/css" href="./_observablehq/theme-air,near-midnight.00000004.css">
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
<script type="module">

import "./_observablehq/client.00000001.js";

</script>
</head>
<body>
<input id="observablehq-sidebar-toggle" type="checkbox" title="Toggle sidebar">
<label id="observablehq-sidebar-backdrop" for="observablehq-sidebar-toggle"></label>
<nav id="observablehq-sidebar">
<ol>
<label id="observablehq-sidebar-close" for="observablehq-sidebar-toggle"></label>
<li class="observablehq-link"><a href="./">Home</a></li>
</ol>
<ol>
<li class="observablehq-link observablehq-link-active"><a href="./horse">horse</a></li>
</ol>
</nav>
<script>{/* redacted init script */}</script>
<div id="observablehq-center">
<aside id="observablehq-toc" data-selector="h1:not(:first-of-type)[id], h2:first-child[id], :not(h1) + h2[id]">
<nav>
</nav>
</aside>
<main id="observablehq-main" class="observablehq">
<h1 id="horse" tabindex="-1"><a class="observablehq-header-anchor" href="#horse">horse</a></h1>
<p><a href="./_file/horse.2d33a223.jpg">horse.jpg</a></p>
<p><a href="./horse">horse (md)</a></p>
<p><a href="./horse">horse.html</a></p>
<p><a href="./_file/horse.dbb90817.md">horse.md</a></p>
</main>
<footer id="observablehq-footer">
<div>Built with <a href="https://observablehq.com/" target="_blank" rel="noopener noreferrer">Observable</a> on <a title="2024-01-10T16:00:00">Jan 10, 2024</a>.</div>
</footer>
</div>
</body>
</html>