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
2 changes: 1 addition & 1 deletion src/web/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4320,7 +4320,7 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " +
if (name === "logs" && hq) applyLogFilter(hq);
} else {
const ses = hash.match(new RegExp("^#/sessions(?:/(.+))?$")) || hash.match(new RegExp("^#/session/(.+)$"));
const blg = hash.match(new RegExp("^#/blog/([A-Za-z0-9_-]+)$"));
const blg = hash.match(new RegExp("^#/blog/(.+)$"));
if (ses) {
name = "sessions";
if (ses[1]) detailId = decodeURIComponent(ses[1]);
Expand Down
39 changes: 37 additions & 2 deletions tests/web-blog-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@ const mkHarnessEl = (): HarnessEl => ({

const FEED = { items: [{ id: "demo", kind: "post", date: "2026-10-08", title: "Demo post", titleZh: "演示", excerpt: "ex", html: "<p>hi</p>", htmlZh: "<p>你好</p>" }] };

async function runClient(hash: string): Promise<{ els: Map<string, HarnessEl>; blogHits: number; unhandled: unknown[] }> {
// #4957: blog slugs keep whatever the filename carried after the date prefix
// (blogfeed.ts strips only `YYYY-MM-DD_`), so ids can contain dots, spaces and
// CJK text — and the list href is percent-encoded, so it also contains `%`.
const NONASCII_FEED = {
items: [
{ id: "v0.1.208·压缩上线", kind: "post", date: "2026-10-20", title: "Dot and middot id", titleZh: "", excerpt: "ex", html: "<p>dot</p>", htmlZh: "" },
{ id: "深入浅出上下文压缩", kind: "post", date: "2026-10-21", title: "CJK id", titleZh: "", excerpt: "ex", html: "<p>cjk</p>", htmlZh: "" },
],
};

async function runClient(hash: string, feed: unknown = FEED): Promise<{ els: Map<string, HarnessEl>; blogHits: number; unhandled: unknown[] }> {
const unhandled: unknown[] = [];
const onRej = (r: unknown) => unhandled.push(r);
process.on("unhandledRejection", onRej);
Expand All @@ -62,7 +72,7 @@ async function runClient(hash: string): Promise<{ els: Map<string, HarnessEl>; b
fetch: async (url: string | URL | Request) => {
if (String(url).endsWith("/blog")) {
blogHits += 1;
return { ok: true, status: 200, json: async () => FEED };
return { ok: true, status: 200, json: async () => feed };
}
return { ok: true, status: 200, json: async () => ({}) };
},
Expand Down Expand Up @@ -113,6 +123,31 @@ test("unknown slug degrades to a back link without a crash", async () => {
assert.ok(!list.innerHTML.includes("post-body"), "no article body for an unknown slug");
});

test("blog ids containing non-ASCII or dots route to the article detail, not overview (#4957)", async () => {
// The hash is exactly what the index row href produces (encodeURIComponent of
// the id): dots survive literal, CJK becomes %XX sequences. The old route
// regex only admitted [A-Za-z0-9_-] so both fell back to overview.
for (const item of NONASCII_FEED.items) {
const hash = "#/blog/" + encodeURIComponent(item.id);
const r = await runClient(hash, NONASCII_FEED);
assert.equal(r.unhandled.length, 0, `${hash} raised unhandled rejection: ${r.unhandled.join("; ")}`);
assert.equal(r.blogHits, 1, `${hash} fetches the feed once`);
assert.ok(r.els.get("page-blog") && !r.els.get("page-blog")!.hidden, `${hash} shows the blog section`);
assert.ok(r.els.get("page-overview")!.hidden, `${hash} must NOT fall back to the overview page`);
const list = r.els.get("bl-list")!;
assert.ok(list.innerHTML.includes("post-body"), `${hash} renders the article body`);
}
});

test("unknown non-ASCII blog slug degrades to the back-link card without a crash", async () => {
const hash = "#/blog/" + encodeURIComponent("不存在的文章.md");
const r = await runClient(hash, NONASCII_FEED);
assert.equal(r.unhandled.length, 0, `client raised unhandled rejection: ${r.unhandled.join("; ")}`);
const list = r.els.get("bl-list")!;
assert.ok(list.innerHTML.includes('<a href="#/blog" class="bl-back">'), "back link present for an unknown non-ASCII slug");
assert.ok(!list.innerHTML.includes("post-body"), "no article body for an unknown slug");
});

test("default and other hashes do NOT activate the blog view or hit the blog endpoint", async () => {
for (const hash of ["#/overview", "#/logs"]) {
const r = await runClient(hash);
Expand Down
Loading