asset忘了上传了,重新传一下,增加了模板引擎(见README),修复了printable的目录bug

This commit is contained in:
Nobody committed 2026-11-10 00:43:44 +08:00
commit 294abad18b
550 files changed
+62291

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
import { assert, assertEquals, assertStringIncludes } from "@std/assert";
import { prepareLocalCache } from "./local_cache.ts";
Deno.test("local cache downloads resource references and preserves reader links", async () => {
const root = await Deno.makeTempDir();
const publication = `${root}/publication`;
const templates = `${root}/templates`;
await Deno.mkdir(publication, { recursive: true });
await Deno.mkdir(templates, { recursive: true });
const assetUrl = "https://cdn.example.test/asset.png";
const cssUrl = "https://cdn.example.test/theme.css";
const fontUrl = "https://cdn.example.test/font.woff2";
const htmlUrl = "https://cdn.example.test/widget.html";
const nestedImageUrl = "https://cdn.example.test/widget.png";
await Deno.writeTextFile(
`${publication}/article.md`,
`![asset](${assetUrl})\n\n[reader link](${assetUrl})`,
);
await Deno.writeTextFile(
`${templates}/cover.html`,
`<img src="${assetUrl}"><iframe src="${htmlUrl}"></iframe><link href="${cssUrl}"><a href="https://example.com/page.png">reader</a>`,
);
const originalFetch = globalThis.fetch;
const fetched: string[] = [];
globalThis.fetch = async (input) => {
const url = String(input);
fetched.push(url);
assert(
url === assetUrl || url === cssUrl || url === fontUrl ||
url === htmlUrl || url === nestedImageUrl,
);
if (url === cssUrl) {
return new Response(`@font-face { src: url("${fontUrl}"); }`, {
headers: { "content-type": "text/css" },
});
}
if (url === htmlUrl) {
return new Response(
`<img src="https://cdn.example.test/widget.png"><a href="https://reader.example.test/story.png">story</a>`,
{ headers: { "content-type": "text/html" } },
);
}
if (url === fontUrl) {
return new Response(new Uint8Array([1, 2, 3]), {
headers: { "content-type": "font/woff2" },
});
}
return new Response(new Uint8Array([1, 2, 3]), {
headers: { "content-type": "image/png" },
});
};
try {
assertEquals(await prepareLocalCache(publication, templates), 5);
const rewrittenMd = await Deno.readTextFile(`${publication}/article.md`);
const rewrittenHtml = await Deno.readTextFile(`${templates}/cover.html`);
assertStringIncludes(rewrittenMd, "file://");
assertStringIncludes(rewrittenMd, `[reader link](${assetUrl})`);
assertStringIncludes(rewrittenHtml, "file://");
assertStringIncludes(rewrittenHtml, 'iframe src="file://');
assertStringIncludes(rewrittenHtml, "https://example.com/page.png");
assertEquals(
fetched.sort(),
[assetUrl, cssUrl, fontUrl, htmlUrl, nestedImageUrl].sort(),
);
const cache = await Deno.readTextFile(`${publication}/.cache.toml`);
assertStringIncludes(cache, assetUrl);
assertEquals(await prepareLocalCache(publication, templates), 0);
assertEquals(fetched.length, 5);
} finally {
globalThis.fetch = originalFetch;
await Deno.remove(root, { recursive: true });
}
assert(true);
});