Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(share): keep the zh-CN default title out of server-rendered share pages (#715)

Dariusz Korytod8185362f436
diff --git a/apps/api/src/notebook-share-routes.test.mjs b/apps/api/src/notebook-share-routes.test.mjs
index 66b880e1..5ade80ed 100644
--- a/apps/api/src/notebook-share-routes.test.mjs
+++ b/apps/api/src/notebook-share-routes.test.mjs
@@ -334,6 +334,11 @@ describe("public notebook site controls", () => {
expect(tokenHtml).toContain(`<meta name="robots" content="${PUBLIC_PAGE_ROBOTS}">`);
expect(tokenHtml).toContain(`rel="canonical" href="https://example.org/s/docs${suffix}"`);
}
+ // The stored zh-CN default title means "untitled"; the page falls back to the notebook name.
+ f.db.run("UPDATE memos SET title = ? WHERE id = 'memo_child'", ["无标题笔记"]);
+ const untitledHtml = await (await render("/s/docs/memo_child")).text();
+ expect(untitledHtml).not.toContain("无标题笔记");
+ expect(untitledHtml).toMatch(/<title>[^<]+ · EdgeEver<\/title>/);
await manage(f, "/memos/memo_child", { excluded: true });
const hidden = await render("/s/docs/memo_child");
expect(hidden.status).toBe(404);
diff --git a/apps/api/src/notebook-site-page.ts b/apps/api/src/notebook-site-page.ts
index 8458a1ba..557b3af8 100644
--- a/apps/api/src/notebook-site-page.ts
+++ b/apps/api/src/notebook-site-page.ts
@@ -1,3 +1,4 @@
+import { getMemoDisplayTitle } from "@edgeever/shared/memo-title";
import { PUBLIC_PAGE_HEADERS, PUBLIC_PAGE_ROBOTS } from "@edgeever/shared/public-page-headers";
import type { DatabaseAdapter } from "./storage-contract";
import { getNotebookSite, publicTreeSql, resolveNotebookSiteToken } from "./notebook-site-service";
@@ -31,7 +32,7 @@ export const renderNotebookSitePage = async (
.bind(site.token, selected).first<{ title: string | null; excerpt: string }>() : null;
if (memoId && !note) status = 404;
if (status === 200) {
- title = `${note?.title?.trim() || site.name} · EdgeEver`;
+ title = `${getMemoDisplayTitle(note?.title, site.name)} · EdgeEver`;
description = note?.excerpt || site.name;
indexable = alias && site.settings.allowIndexing;
const base = site.site_slug ? `/s/${encodeURIComponent(site.site_slug)}` : `/share/notebook/${encodeURIComponent(site.token)}`;
diff --git a/apps/api/src/share-page.test.mjs b/apps/api/src/share-page.test.mjs
index f0bc7fc5..bd6dfef0 100644
--- a/apps/api/src/share-page.test.mjs
+++ b/apps/api/src/share-page.test.mjs
@@ -174,6 +174,19 @@ describe("public memo share pages", () => {
expect(await lockedMarkdown.text()).not.toContain("secret()");
});
+ test("treats the stored default title as untitled in metadata and markdown", async () => {
+ const sqlite = createDatabase();
+ sqlite.query("UPDATE memos SET title = ? WHERE id = 'memo_source'").run("无标题笔记");
+ sqlite.query("UPDATE memo_contents SET content_markdown = ? WHERE memo_id = 'memo_source'").run("Body text\n");
+ const html = await (await render(sqlite, `/share/${token}`)).text();
+ expect(html).not.toContain("无标题笔记");
+ expect(html).toContain("<title>Shared note · EdgeEver</title>");
+ expect(html).toContain('<meta property="og:title" content="Shared note">');
+ expect(html).toContain('<meta name="twitter:title" content="Shared note">');
+ expect(html).toContain("Body text");
+ expect(await (await render(sqlite, `/share/${token}.md`)).text()).toBe("Body text\n");
+ });
+
test("HEAD keeps the headers and omits the body", async () => {
const response = await renderPublicMemoSharePage(
new Request(`https://note.example/share/${token}.md`, { method: "HEAD" }),
diff --git a/apps/api/src/share-page.ts b/apps/api/src/share-page.ts
index 4aa50969..68ddfa40 100644
--- a/apps/api/src/share-page.ts
+++ b/apps/api/src/share-page.ts
@@ -1,3 +1,4 @@
+import { getMemoDisplayTitle } from "@edgeever/shared/memo-title";
import { isValidMemoShareSlug } from "@edgeever/shared/share-slug";
import { PUBLIC_PAGE_HEADERS, PUBLIC_PAGE_ROBOTS } from "@edgeever/shared/public-page-headers";
import type { DatabaseAdapter } from "./storage-contract";
@@ -80,7 +81,8 @@ export const PUBLIC_SHARE_MARKDOWN_PASSWORD = "This shared note requires a passw
/** Markdown body for a public note. A title already used as the first heading is kept once. */
export const publicShareMarkdown = (title: string | null, body: string) => {
- const heading = (title ?? "").replace(/[\r\n]+/g, " ").trim();
+ // The stored zh-CN default title means "untitled"; it is not a heading.
+ const heading = getMemoDisplayTitle(title, "").replace(/[\r\n]+/g, " ").trim();
if (!heading) return body;
const firstLine = body.split(/\r?\n/, 1)[0] ?? "";
if (firstLine.replace(/^#\s+/, "").trim() === heading) return body;
@@ -153,7 +155,7 @@ export const renderPublicMemoSharePage = async (
return new Response(request.method === "HEAD" ? null : publicShareMarkdown(row.title, body), { status: 200, headers });
}
- const title = readable ? (row?.title?.replace(/[\r\n]+/g, " ").trim() || "Shared note") : "Shared note";
+ const title = readable ? (getMemoDisplayTitle(row?.title, "").replace(/[\r\n]+/g, " ").trim() || "Shared note") : "Shared note";
const description = readable
? describeNote(body)
: locked
diff --git a/packages/shared/package.json b/packages/shared/package.json
index 156a84dd..6d63831a 100644
--- a/packages/shared/package.json
+++ b/packages/shared/package.json
@@ -7,6 +7,7 @@
".": "./src/index.ts",
"./diagram-canvas": "./src/diagram-canvas.ts",
"./public-demo": "./src/public-demo.ts",
+ "./memo-title": "./src/memo-title.ts",
"./share-slug": "./src/share-slug.ts",
"./ai-generation": "./src/ai-generation.ts",
"./diagram-layout": "./src/diagram-layout.ts",
diff --git a/packages/shared/src/content.ts b/packages/shared/src/content.ts
index 174ba3be..51e1e04b 100644
--- a/packages/shared/src/content.ts
+++ b/packages/shared/src/content.ts
@@ -4,6 +4,7 @@ import {
type CreateEdgeEverDocumentExtensionsOptions,
} from "./document-extensions";
import { MERGE_DIVIDER_NODE_TYPE } from "./merge-divider";
+import { DEFAULT_MEMO_TITLE } from "./memo-title";
import { PDF_ATTACHMENT_NODE_TYPE, upgradeStandalonePdfLinks } from "./pdf-attachment";
import { FILE_ATTACHMENT_NODE_TYPE, upgradeStandaloneFileLinks } from "./file-attachment";
import {
@@ -77,16 +78,7 @@ export type TiptapDoc = {
content: TiptapNode[];
};
-export const DEFAULT_MEMO_TITLE = "无标题笔记";
-
-/**
- * Title to show for a note. Untitled notes are stored with the zh-CN default
- * title, so it is replaced by the reader's localized label like an empty one.
- */
-export const getMemoDisplayTitle = (title: string | null | undefined, untitledLabel: string) => {
- const trimmed = title?.trim() ?? "";
- return trimmed && trimmed !== DEFAULT_MEMO_TITLE ? trimmed : untitledLabel;
-};
+export { DEFAULT_MEMO_TITLE, getMemoDisplayTitle } from "./memo-title";
export const resolveMergedMemoTitle = (
inputTitle: string | null | undefined,
diff --git a/packages/shared/src/memo-title.ts b/packages/shared/src/memo-title.ts
new file mode 100644
index 00000000..0f92e901
--- /dev/null
+++ b/packages/shared/src/memo-title.ts
@@ -0,0 +1,12 @@
+/** Kept free of imports so server entry points loaded by Node can use it. */
+
+export const DEFAULT_MEMO_TITLE = "无标题笔记";
+
+/**
+ * Title to show for a note. Untitled notes are stored with the zh-CN default
+ * title, so it is replaced by the reader's localized label like an empty one.
+ */
+export const getMemoDisplayTitle = (title: string | null | undefined, untitledLabel: string) => {
+ const trimmed = title?.trim() ?? "";
+ return trimmed && trimmed !== DEFAULT_MEMO_TITLE ? trimmed : untitledLabel;
+};