Skip to content

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

:bug: Preserve inline formatting when renaming tabs https://github.com/siyuan-note/siyuan/issues/17642

Danielf62e75573ae8
diff --git a/app/src/protyle/wysiwyg/tabs.ts b/app/src/protyle/wysiwyg/tabs.ts
index a82200eb47..9341e30d9c 100644
--- a/app/src/protyle/wysiwyg/tabs.ts
+++ b/app/src/protyle/wysiwyg/tabs.ts
@@ -13,6 +13,7 @@ import {queueTransaction} from "../util/transactionQueue";
import {remapTabsDOMIDs} from "../util/tabsCopy";
import {Dialog} from "../../dialog";
import {isMobile} from "../../util/functions";
+import {getTabsTitleMarkdown, renderTabsTitleMarkdown} from "./tabsTitle";
const canEdit = (protyle: IProtyle, element: Element) => !protyle.disabled &&
!protyle.options.action.includes(Constants.CB_GET_HISTORY) && !element.closest(".protyle-wysiwyg__embed");
@@ -44,7 +45,7 @@ export const renameTab = (protyle: IProtyle, item: HTMLElement) => {
}
revealTabAncestors(protyle.wysiwyg.element, item);
const title = getTabTitle(item);
- const initialTitle = title.textContent || "";
+ const initialTitle = getTabsTitleMarkdown(protyle.lute, title);
const dialog = new Dialog({
title: window.siyuan.languages.rename,
content: `<div class="b3-dialog__content"><input class="b3-text-field fn__block"></div>
@@ -60,7 +61,7 @@ export const renameTab = (protyle: IProtyle, item: HTMLElement) => {
const value = input.value.trim();
if (value !== initialTitle) {
changeTabs(protyle, [item.parentElement], () => {
- title.textContent = value;
+ title.innerHTML = renderTabsTitleMarkdown(protyle.lute, value);
});
}
dialog.destroy();
diff --git a/app/src/protyle/wysiwyg/tabsTitle.test.ts b/app/src/protyle/wysiwyg/tabsTitle.test.ts
new file mode 100644
index 0000000000..88add5f5c1
--- /dev/null
+++ b/app/src/protyle/wysiwyg/tabsTitle.test.ts
@@ -0,0 +1,70 @@
+import * as assert from "node:assert/strict";
+import {test} from "node:test";
+import {getTabsTitleMarkdown, renderTabsTitleMarkdown} from "./tabsTitle";
+
+test("serializes the existing tab title as Markdown", () => {
+ assert.equal(getTabsTitleMarkdown({
+ BlockDOM2Md: () => "",
+ BlockDOM2StdMd: html => html === '<span data-type="strong">foo</span>' ? "**foo**\n" : "",
+ InlineMd2BlockDOM: () => "",
+ Md2BlockDOM: () => "",
+ }, {
+ innerHTML: '<span data-type="strong">foo</span>',
+ querySelector: (): Element | null => null,
+ } as unknown as HTMLElement), "**foo**");
+});
+
+test("serializes combined font styles as reversible Kramdown", () => {
+ const markdown = '<span data-type="strong text">foo</span>{: style="color: red;"}';
+ assert.equal(getTabsTitleMarkdown({
+ BlockDOM2Md: html => html.includes('data-type="strong text"') ? markdown + "\n" : "",
+ BlockDOM2StdMd: () => {
+ throw new Error("unexpected standard Markdown serializer");
+ },
+ InlineMd2BlockDOM: () => "",
+ Md2BlockDOM: () => "",
+ }, {
+ innerHTML: '<span data-type="strong text" style="color: red;">foo</span>',
+ querySelector: (selector: string): Element | null =>
+ selector === "span[style]" ? {} as Element : null,
+ } as unknown as HTMLElement), markdown);
+});
+
+test("parses regular inline Markdown without block interpretation", () => {
+ let inline = "";
+ const html = renderTabsTitleMarkdown({
+ BlockDOM2Md: () => "",
+ BlockDOM2StdMd: () => "",
+ InlineMd2BlockDOM: markdown => {
+ inline = markdown;
+ return '<div data-type="NodeParagraph"><div contenteditable="true"><span data-type="strong">foo</span></div></div>';
+ },
+ Md2BlockDOM: () => {
+ throw new Error("unexpected block parser");
+ },
+ }, "**foo**", {
+ content: {querySelector: () => ({innerHTML: '<span data-type="strong">foo</span>'})},
+ } as unknown as HTMLTemplateElement);
+ assert.equal(inline, "**foo**");
+ assert.equal(html, '<span data-type="strong">foo</span>');
+});
+
+test("restores serialized combined font styles with the block Markdown parser", () => {
+ const markdown = '<span data-type="strong text">foo</span>{: style="color: red;"}';
+ let block = "";
+ const html = renderTabsTitleMarkdown({
+ BlockDOM2Md: () => "",
+ BlockDOM2StdMd: () => "",
+ InlineMd2BlockDOM: () => {
+ throw new Error("unexpected inline parser");
+ },
+ Md2BlockDOM: value => {
+ block = value;
+ return '<div data-type="NodeParagraph"><div contenteditable="true"><span data-type="strong text" style="color: red;">foo</span></div></div>';
+ },
+ }, markdown, {
+ content: {querySelector: () => ({innerHTML: '<span data-type="strong text" style="color: red;">foo</span>'})},
+ } as unknown as HTMLTemplateElement);
+ assert.equal(block, markdown + "\n");
+ assert.equal(html, '<span data-type="strong text" style="color: red;">foo</span>');
+});
diff --git a/app/src/protyle/wysiwyg/tabsTitle.ts b/app/src/protyle/wysiwyg/tabsTitle.ts
new file mode 100644
index 0000000000..7ab4437baa
--- /dev/null
+++ b/app/src/protyle/wysiwyg/tabsTitle.ts
@@ -0,0 +1,23 @@
+interface ITabsTitleLute {
+ BlockDOM2Md(html: string): string;
+ BlockDOM2StdMd(html: string): string;
+ InlineMd2BlockDOM(markdown: string): string;
+ Md2BlockDOM(markdown: string): string;
+}
+
+export const getTabsTitleMarkdown = (lute: ITabsTitleLute, title: HTMLElement) => {
+ // 标准 Markdown 会丢弃 strong text 等组合文本标记上的字体样式,含样式时使用可逆的 Kramdown。
+ const serialize = title.querySelector("span[style]") ?
+ lute.BlockDOM2Md.bind(lute) : lute.BlockDOM2StdMd.bind(lute);
+ return serialize(title.innerHTML).trim();
+};
+
+export const renderTabsTitleMarkdown = (lute: ITabsTitleLute, markdown: string,
+ template = document.createElement("template")) => {
+ // 字体颜色等文本标记使用带 span 属性的 Kramdown,块解析器可将其还原为原文本标记。
+ const hasKramdownStyle = /<span\b[^>]*\bdata-type=["'][^"']*\btext\b/i.test(markdown) ||
+ /\{:\s*style\s*=/.test(markdown);
+ template.innerHTML = hasKramdownStyle ?
+ lute.Md2BlockDOM(markdown + "\n") : lute.InlineMd2BlockDOM(markdown);
+ return template.content.querySelector<HTMLElement>('[contenteditable="true"]')?.innerHTML || "";
+};
diff --git a/app/src/types/protyle.d.ts b/app/src/types/protyle.d.ts
index 2ff8ea3750..344fbf8c98 100644
--- a/app/src/types/protyle.d.ts
+++ b/app/src/types/protyle.d.ts
@@ -295,6 +295,8 @@ declare class Lute {
public Md2BlockDOMWithAutoLink(html: string): string;
+ public InlineMd2BlockDOM(markdown: string): string;
+
public SetProtyleWYSIWYG(wysiwyg: boolean): void;
public MarkdownStr(name: string, md: string): string;
diff --git a/docs/TABS.md b/docs/TABS.md
index 385f94ebb3..f68202970f 100644
--- a/docs/TABS.md
+++ b/docs/TABS.md
@@ -78,6 +78,7 @@ Linux 下的说明。
- 直属页签项使用 `.tab-item`,标题保存在项内唯一的 `.tab-item-title.callout-title`,正文位于 `.tab-item-content`。正文不复用 `.callout-content`,避免提示块退出行为破坏页签父子关系。
- 导航栏只是标题的展示,反向解析以页签项中的原始标题为唯一来源。嵌套查找必须限制到直属结构。
- 单击切页,双击或菜单进入标题编辑。标题编辑复用提示块行级格式、选区与输入法规则,移动端提供菜单入口。
+- 重命名对话框使用行级 Markdown:普通格式使用标准 Markdown,含字体颜色等样式时使用可逆的 Kramdown,确认时重新解析,因此 `**foo**` 等格式可以直接输入,组合文本标记和字体样式不会因重命名而丢失。由列表转换的标题段落仍保留原 ID 和块属性。
- 新建页签块默认包含两个页签,各包含一个空段落。标题、正文、排序和布局变更参与正常撤销。
- 可写编辑器中的主动切换和搜索、引用定位保存选中项。切换不占用正文撤销栈,不刷新正文修改时间,但会落盘并参与同步。
- 刷新或重启后恢复文档位置时,页签选择优先于过期光标。旧光标位于未选中的页签内时,将焦点移到当前选中页的正文,不复用旧文本偏移;保存文档位置时也不记录隐藏页中的过期光标。主动搜索和块引用定位仍可切换到目标页。