Skip to content

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

fix merged note title selection

tianma-if2b971a5395d0
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 0a52058e..aacf430b 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -10,6 +10,7 @@ import {
LoginSchema,
LoginDeviceSessionUpdateSchema,
markdownToDoc,
+ resolveMergedMemoTitle,
isSuspiciousMemoOverwrite,
isMemoEditBindingValid,
JsonBackupResourceMetadataSchema,
@@ -5719,20 +5720,6 @@ const normalizeMemoTitle = (value: string | null | undefined) => {
return title || DEFAULT_MEMO_TITLE;
};
-const isCustomMemoTitle = (value: string | null | undefined) => {
- const title = value?.trim();
- return Boolean(title && title !== DEFAULT_MEMO_TITLE);
-};
-
-const resolveMergedMemoTitle = (inputTitle: string | undefined, sourceMemos: Array<{ title: string | null }>) => {
- const title = inputTitle?.trim();
- if (title) {
- return title;
- }
-
- return sourceMemos.find((memo) => isCustomMemoTitle(memo.title))?.title?.trim() ?? `合并笔记 ${new Date().toLocaleDateString("zh-CN")}`;
-};
-
const normalizeMemoListSort = (value: string | undefined): MemoListSortMode =>
value === "created-desc" || value === "title-asc" ? value : "updated-desc";
diff --git a/apps/web/src/lib/local-mirror.test.mjs b/apps/web/src/lib/local-mirror.test.mjs
index ea32465e..7101399e 100644
--- a/apps/web/src/lib/local-mirror.test.mjs
+++ b/apps/web/src/lib/local-mirror.test.mjs
@@ -161,6 +161,16 @@ describe("local mirror", () => {
expect((await getLocalMemo(scope, second.id))?.mergedIntoMemoId).toBe(merged?.id);
});
+ test("uses a custom source title when the first merged memo is untitled", async () => {
+ const scope = createLocalDataScope("https://demo.edgeever.org", "user-1");
+ const untitled = await createLocalMemo(scope, { notebookId: "inbox", title: "无标题笔记", contentMarkdown: "one" });
+ const titled = await createLocalMemo(scope, { notebookId: "inbox", title: "手动设置的标题", contentMarkdown: "two" });
+
+ const merged = await mergeLocalMemos(scope, { memoIds: [untitled.id, titled.id] });
+
+ expect(merged?.title).toBe("手动设置的标题");
+ });
+
test("caches memo revisions by account scope", async () => {
const scope = createLocalDataScope("https://demo.edgeever.org", "user-1");
const memo = await createLocalMemo(scope, { notebookId: "inbox" });
diff --git a/apps/web/src/lib/local-mirror.ts b/apps/web/src/lib/local-mirror.ts
index d65d5bda..fd15dcd0 100644
--- a/apps/web/src/lib/local-mirror.ts
+++ b/apps/web/src/lib/local-mirror.ts
@@ -1,4 +1,4 @@
-import { createExcerpt, docToText, markdownToDoc, type MemoDetail, type MemoRevision, type MemoSummary, type MemoTemplate, type Notebook, type ResourceListItem, type TagSummary, type TiptapDoc } from "@edgeever/shared";
+import { createExcerpt, docToText, markdownToDoc, resolveMergedMemoTitle, type MemoDetail, type MemoRevision, type MemoSummary, type MemoTemplate, type Notebook, type ResourceListItem, type TagSummary, type TiptapDoc } from "@edgeever/shared";
import type { MemoFilterMode, MemoSortMode } from "@/lib/app-helpers";
import { api, type SyncChangesResponse } from "@/lib/api";
import { localDb, type LocalMemo, type LocalNotebook, type LocalResource, type LocalRevision } from "@/lib/local-db";
@@ -563,7 +563,7 @@ export const mergeLocalMemos = async (scope: string, input: { memoIds: string[];
const contentMarkdown = sources.map((memo) => memo.contentMarkdown).filter(Boolean).join("\n\n---\n\n");
const memo = await createLocalMemo(scope, {
notebookId: input.notebookId ?? sources[0]!.notebookId,
- title: input.title ?? sources[0]!.title ?? "",
+ title: resolveMergedMemoTitle(input.title, sources),
contentMarkdown,
tags: [...new Set(sources.flatMap((source) => source.tags))],
});
diff --git a/crates/desktop-sidecar/src/main.rs b/crates/desktop-sidecar/src/main.rs
index 24c1f81f..b3403e98 100644
--- a/crates/desktop-sidecar/src/main.rs
+++ b/crates/desktop-sidecar/src/main.rs
@@ -763,8 +763,8 @@ fn merge_memos(database: &Connection, params: &Value) -> Result<Value, String> {
.get("title")
.and_then(Value::as_str)
.filter(|value| !value.trim().is_empty())
- .map(str::to_owned)
- .or_else(|| titles.into_iter().find(|value| !value.starts_with("nb_")))
+ .map(|value| value.trim().to_owned())
+ .or_else(|| resolve_custom_merge_title(titles.iter().map(String::as_str)))
.unwrap_or_default();
tags.sort();
tags.dedup();
@@ -793,6 +793,14 @@ fn merge_memos(database: &Connection, params: &Value) -> Result<Value, String> {
memo_value(database, &id, true).map(|memo| json!({ "memo": memo }))
}
+fn resolve_custom_merge_title<'a>(titles: impl IntoIterator<Item = &'a str>) -> Option<String> {
+ titles
+ .into_iter()
+ .map(str::trim)
+ .find(|title| !title.is_empty() && *title != "无标题笔记")
+ .map(str::to_owned)
+}
+
fn list_memos(database: &Connection, params: &Value) -> Result<Value, String> {
let trash = bool_param(params, "trash", false);
let q = params
@@ -1550,4 +1558,13 @@ mod tests {
assert_eq!(first, content_hash("same", &json));
assert_ne!(first, content_hash("different", &json));
}
+
+ #[test]
+ fn merge_title_skips_untitled_sources() {
+ assert_eq!(
+ resolve_custom_merge_title(["无标题笔记", " 手动标题 ", "另一个标题"]),
+ Some("手动标题".to_owned())
+ );
+ assert_eq!(resolve_custom_merge_title(["无标题笔记", " "]), None);
+ }
}
diff --git a/packages/shared/src/content.test.mjs b/packages/shared/src/content.test.mjs
index d9fd6d57..60950365 100644
--- a/packages/shared/src/content.test.mjs
+++ b/packages/shared/src/content.test.mjs
@@ -1,5 +1,19 @@
import { describe, expect, test } from "bun:test";
-import { countMemoCharacters, docToMarkdown, docToText, markdownToDoc, resolveMemoContentDoc } from "./content.ts";
+import { countMemoCharacters, docToMarkdown, docToText, markdownToDoc, resolveMemoContentDoc, resolveMergedMemoTitle } from "./content.ts";
+
+describe("merged memo title", () => {
+ test("prefers an explicit title, then the first custom source title", () => {
+ const sources = [{ title: "无标题笔记" }, { title: " 手动标题 " }, { title: "另一个标题" }];
+
+ expect(resolveMergedMemoTitle(" 指定标题 ", sources)).toBe("指定标题");
+ expect(resolveMergedMemoTitle(undefined, sources)).toBe("手动标题");
+ });
+
+ test("uses a dated merge title when every source is untitled", () => {
+ expect(resolveMergedMemoTitle(undefined, [{ title: null }, { title: "无标题笔记" }], new Date(2026, 7, 2)))
+ .toBe("合并笔记 2026/8/2");
+ });
+});
describe("memo character count", () => {
test("counts punctuation while excluding whitespace and formatting", () => {
diff --git a/packages/shared/src/content.ts b/packages/shared/src/content.ts
index 70a13c80..74519f84 100644
--- a/packages/shared/src/content.ts
+++ b/packages/shared/src/content.ts
@@ -27,6 +27,22 @@ export type TiptapDoc = {
export const DEFAULT_MEMO_TITLE = "无标题笔记";
+export const resolveMergedMemoTitle = (
+ inputTitle: string | null | undefined,
+ sourceMemos: Array<{ title: string | null | undefined }>,
+ date = new Date(),
+) => {
+ const explicitTitle = inputTitle?.trim();
+ if (explicitTitle) {
+ return explicitTitle;
+ }
+
+ const customTitle = sourceMemos
+ .map((memo) => memo.title?.trim())
+ .find((title): title is string => Boolean(title && title !== DEFAULT_MEMO_TITLE));
+ return customTitle ?? `合并笔记 ${date.toLocaleDateString("zh-CN")}`;
+};
+
export const emptyDoc = (): TiptapDoc => ({
type: "doc",
content: [{ type: "paragraph" }],