Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve Unicode document title length limits https://github.com/siyuan-note/siyuan/issues/19938

Daniel87f6a74e6e28
diff --git a/app/src/editor/rename.ts b/app/src/editor/rename.ts
index 118a4745e5..6afcd07e57 100644
--- a/app/src/editor/rename.ts
+++ b/app/src/editor/rename.ts
@@ -42,12 +42,17 @@ export const replaceFileName = (name: string) => {
return name.replace(/\r\n|\r|\n|\u2028|\u2029|\t|/g, "").substring(0, Constants.SIZE_TITLE);
};
+export const truncateDocumentTitle = (name: string) => {
+ return window.siyuan.documentAddressingVersion === 2 ?
+ Array.from(name).slice(0, Constants.SIZE_TITLE).join("") : name.substring(0, Constants.SIZE_TITLE);
+};
+
// 文档标题与文件名分开处理;旧内核保留既有输入规则。
export const normalizeDocumentTitle = (name: string) => {
if (window.siyuan.documentAddressingVersion !== 2) {
return replaceFileName(name);
}
- return Array.from(name.replace(/\r\n|\r|\n|\u2028|\u2029|\t/g, "")).slice(0, Constants.SIZE_TITLE).join("");
+ return truncateDocumentTitle(name.replace(/\r\n|\r|\n|\u2028|\u2029|\t/g, ""));
};
export const replaceLocalPath = (name: string) => {
diff --git a/app/src/mobile/util/initFramework.test.ts b/app/src/mobile/util/initFramework.test.ts
new file mode 100644
index 0000000000..3ba0f4ab8b
--- /dev/null
+++ b/app/src/mobile/util/initFramework.test.ts
@@ -0,0 +1,73 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {resolve} from "node:path";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const compile = (file: string, suffix = "") => transpileModule(
+ readFileSync(resolve(__dirname, file), "utf8") + suffix,
+ {compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021}},
+).outputText;
+
+test("mobile toolbar saves Unicode titles and preserves full characters at the length limit", () => {
+ const listeners = new Map<string, () => void>();
+ const attributes = new Map<string, string>();
+ const input = {
+ value: "",
+ getAttribute: (name: string) => attributes.get(name),
+ setAttribute: (name: string, value: string) => attributes.set(name, value),
+ addEventListener: (name: string, handler: () => void) => listeners.set(name, handler),
+ };
+ const runtime = {siyuan: {documentAddressingVersion: 2, config: {editor: {autoDirection: false}},
+ languages: {fileNameRule: "Invalid filename", _kernel: {16: "Untitled", 106: "Title too long"}},
+ mobile: {editor: {protyle: {notebookId: "box", path: "/root.sy", block: {rootID: "root"}}}}}};
+ const constants = {SIZE_TITLE: 512};
+ const rename = {};
+ runInNewContext(compile("../../editor/rename.ts"), {
+ exports: rename,
+ window: runtime,
+ require: (name: string) => name === "../constants" ? {Constants: constants} : {showMessage: () => {}},
+ });
+ const requests: Array<{title: string}> = [];
+ const moduleExports: {initEditorName?: () => void} = {};
+ runInNewContext(compile("initFramework.ts", "\nexport {initEditorName};"), {
+ exports: moduleExports,
+ window: runtime,
+ document: {getElementById: () => input},
+ require: (name: string) => {
+ if (name === "../../editor/rename") return rename;
+ if (name === "../../constants") return {Constants: constants};
+ if (name === "../../protyle/render/autoDirection") return {setTitleAutoDirection: () => {}};
+ if (name === "../../util/processTitle") return {setTitle: () => {}};
+ if (name === "../../util/fetch") return {fetchPost: (_url: string, data: {title: string}) => {
+ requests.push(data);
+ return Promise.resolve();
+ }};
+ return {};
+ },
+ });
+ moduleExports.initEditorName();
+ const blur = () => listeners.get("blur")();
+ for (const title of ["😀".repeat(512), "文档/Title", ""]) {
+ input.value = title;
+ blur();
+ assert.equal(requests.at(-1)?.title, title);
+ assert.equal(input.value, title);
+ }
+ for (const [title, expected] of [
+ ["😀".repeat(513), "😀".repeat(512)],
+ ["A" + "😀".repeat(512), "A" + "😀".repeat(511)],
+ ["Title\nwith/slash", "Title\nwith/slash"],
+ ]) {
+ input.value = title;
+ blur();
+ assert.equal(requests.length, 3);
+ assert.equal(input.value, expected);
+ }
+ runtime.siyuan.documentAddressingVersion = 1;
+ input.value = "😀".repeat(512);
+ blur();
+ assert.equal(requests.length, 3);
+ assert.equal(input.value, "😀".repeat(256));
+});
diff --git a/app/src/mobile/util/initFramework.ts b/app/src/mobile/util/initFramework.ts
index cf6e80c4f1..339c3f471e 100644
--- a/app/src/mobile/util/initFramework.ts
+++ b/app/src/mobile/util/initFramework.ts
@@ -4,7 +4,7 @@ import {openStandaloneDatabaseItemByURI} from "../../protyle/render/av/openStand
import {closeModel, closePanel} from "./closePanel";
import {getCurrentEditor, openMobileFileById} from "../editor";
import {openMobileOnboarding} from "../../onboarding";
-import {validateName} from "../../editor/rename";
+import {truncateDocumentTitle, validateName} from "../../editor/rename";
import {getEventName, isDisabledFeature, isInMobileApp} from "../../protyle/util/compatibility";
import {fetchPost} from "../../util/fetch";
import {setInlineStyle} from "../../util/assets";
@@ -457,8 +457,8 @@ const initEditorName = () => {
if (inputElement.getAttribute("readonly") === "readonly") {
return titleSavePromise;
}
- if (!validateName(inputElement.value)) {
- inputElement.value = inputElement.value.substring(0, Constants.SIZE_TITLE);
+ if (!validateName(inputElement.value, undefined, true)) {
+ inputElement.value = truncateDocumentTitle(inputElement.value);
return titleSavePromise;
}
diff --git a/app/src/protyle/header/Title.test.ts b/app/src/protyle/header/Title.test.ts
new file mode 100644
index 0000000000..a5474d357f
--- /dev/null
+++ b/app/src/protyle/header/Title.test.ts
@@ -0,0 +1,52 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {resolve} from "node:path";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const compile = (file: string) => transpileModule(readFileSync(resolve(__dirname, file), "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+}).outputText;
+
+test("title input truncates at the same character boundary as title validation", () => {
+ const runtime = {siyuan: {documentAddressingVersion: 2,
+ languages: {fileNameRule: "Invalid filename", _kernel: {106: "Title too long"}}}};
+ const constants = {SIZE_TITLE: 512};
+ const rename = {};
+ runInNewContext(compile("../../editor/rename.ts"), {
+ exports: rename,
+ window: runtime,
+ require: (name: string) => name === "../constants" ? {Constants: constants} :
+ {showMessage: () => {}, showTooltip: () => {}},
+ });
+ const moduleExports: {Title?: {prototype: any}} = {};
+ runInNewContext(compile("Title.ts"), {
+ exports: moduleExports,
+ clearTimeout: () => {},
+ require: (name: string) => {
+ if (name === "../../editor/rename") return rename;
+ if (name === "../../constants") return {Constants: constants};
+ if (name === "../util/selectionOffsets") return {getSelectionOffset: () => ({start: 1026, end: 1026})};
+ if (name === "../util/selection") return {focusByOffset: () => {}};
+ return {};
+ },
+ });
+ const title = Object.create(moduleExports.Title.prototype);
+ title.editElement = {textContent: ""};
+ title.setTitle = (value: string) => { title.editElement.textContent = value; };
+ for (const [input, expected] of [
+ ["😀".repeat(513), "😀".repeat(512)],
+ ["A" + "😀".repeat(512), "A" + "😀".repeat(511)],
+ ["文".repeat(513), "文".repeat(512)],
+ ["Title\nwith/slash", "Title\nwith/slash"],
+ ]) {
+ title.editElement.textContent = input;
+ assert.equal(title.rename({}), false);
+ assert.equal(title.editElement.textContent, expected);
+ }
+ runtime.siyuan.documentAddressingVersion = 1;
+ title.editElement.textContent = "😀".repeat(513);
+ assert.equal(title.rename({}), false);
+ assert.equal(title.editElement.textContent, "😀".repeat(256));
+});
diff --git a/app/src/protyle/header/Title.ts b/app/src/protyle/header/Title.ts
index f79a49a559..46f179896f 100644
--- a/app/src/protyle/header/Title.ts
+++ b/app/src/protyle/header/Title.ts
@@ -6,7 +6,7 @@ import {
} from "../util/selection";
import {focusByRange, getSelectionOffset} from "../util/selectionOffsets";
import {fetchPost} from "../../util/fetch";
-import {normalizeDocumentTitle, validateName} from "../../editor/rename";
+import {normalizeDocumentTitle, truncateDocumentTitle, validateName} from "../../editor/rename";
import {MenuItem} from "../../menus/Menu";
import {openFileAttr,} from "../../menus/commonMenuItem";
import {Constants} from "../../constants";
@@ -350,7 +350,7 @@ export class Title {
if (!validateName(this.editElement.textContent, this.editElement, true)) {
// 字数过长会导致滚动
const offset = getSelectionOffset(this.editElement);
- this.setTitle(this.editElement.textContent.substring(0, Constants.SIZE_TITLE));
+ this.setTitle(truncateDocumentTitle(this.editElement.textContent));
focusByOffset(this.editElement, offset.start, offset.end);
return false;
}