An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Add a dialog box for Markdown export parameters https://github.com/siyuan-note/siyuan/issues/17031
Danielad0b008802c6
diff --git a/app/src/assets/scss/business/_export.scss b/app/src/assets/scss/business/_export.scssindex 905d287647..b5ae77094a 100644--- a/app/src/assets/scss/business/_export.scss+++ b/app/src/assets/scss/business/_export.scss@@ -19,3 +19,10 @@}}}++// 导出 Markdown 参数对话框 https://github.com/siyuan-note/siyuan/issues/17031+.export-md__content {+ .b3-label__text {+ font-size: 13px !important;+ }+}diff --git a/app/src/constants.ts b/app/src/constants.tsindex c037d682fa..8a7cdcb2fb 100644--- a/app/src/constants.ts+++ b/app/src/constants.ts@@ -202,6 +202,7 @@ export abstract class Constants {public static readonly DIALOG_EXPORTIMAGE = "dialog-exportimage"; // 导出为图片public static readonly DIALOG_EXPORTTEMPLATE = "dialog-exporttemplate"; // 导出为模板public static readonly DIALOG_EXPORTWORD = "dialog-exportword"; // 导出为 word+ public static readonly DIALOG_EXPORTMARKDOWN = "dialog-exportmarkdown"; // 导出为 Markdownpublic static readonly DIALOG_HISTORY = "dialog-history"; // 数据历史(Alt + H)public static readonly DIALOG_HISTORYDOC = "dialog-historydoc"; // 文档历史public static readonly DIALOG_MOVEPATHTO = "dialog-movepathto"; // 移动文档diff --git a/app/src/menus/commonMenuItem.ts b/app/src/menus/commonMenuItem.tsindex c4175b7f43..a64ac41fe3 100644--- a/app/src/menus/commonMenuItem.ts+++ b/app/src/menus/commonMenuItem.ts@@ -6,6 +6,7 @@ import {getSearch, isMobile, isValidCustomAttrName} from "../util/functions";import {isLocalPath, movePathTo, moveToPath, pathPosix} from "../util/pathName";import {MenuItem} from "./Menu";import {onExport, saveExport} from "../protyle/export";+import {exportMarkdownZip} from "../protyle/export/exportMd";import {isInAndroid,isInHarmony,@@ -629,12 +630,7 @@ export const exportMd = (id: string) => {label: "Markdown .zip",icon: "iconMarkdown",click: () => {- const msgId = showMessage(window.siyuan.languages.exporting, -1);- fetchPost("/api/export/exportMd", {- id,- }, response => {- saveExportFile(response.data.zip, msgId);- });+ exportMarkdownZip({id});}}, {id: "exportImage",diff --git a/app/src/menus/navigation.ts b/app/src/menus/navigation.tsindex 05a1a0344f..b10a06f1ec 100644--- a/app/src/menus/navigation.ts+++ b/app/src/menus/navigation.ts@@ -29,6 +29,7 @@ import {makeCard} from "../card/makeCard";import {transaction} from "../protyle/wysiwyg/transaction";import {emitOpenMenu} from "../plugin/EventBus";import {saveExportFile} from "../protyle/util/compatibility";+import {exportMarkdownZip} from "../protyle/export/exportMd";import {addFilesToDatabase} from "../protyle/render/av/addToDatabase";const initMultiMenu = (selectItemElements: NodeListOf<Element>, app: App) => {@@ -166,12 +167,7 @@ const initMultiMenu = (selectItemElements: NodeListOf<Element>, app: App) => {label: "Markdown .zip",icon: "iconMarkdown",click: () => {- const msgId = showMessage(window.siyuan.languages.exporting, -1);- fetchPost("/api/export/exportMds", {- ids: blockIDs,- }, response => {- saveExportFile(response.data.zip, msgId);- });+ exportMarkdownZip({ids: blockIDs});}}]}).element);@@ -398,12 +394,7 @@ export const initNavigationMenu = (app: App, liElement: HTMLElement) => {label: "Markdown .zip",icon: "iconMarkdown",click: () => {- const msgId = showMessage(window.siyuan.languages.exporting, -1);- fetchPost("/api/export/exportNotebookMd", {- notebook: notebookId- }, response => {- saveExportFile(response.data.zip, msgId);- });+ exportMarkdownZip({notebook: notebookId});}}]}).element);diff --git a/app/src/protyle/export/exportMd.ts b/app/src/protyle/export/exportMd.tsnew file mode 100644index 0000000000..9af3187199--- /dev/null+++ b/app/src/protyle/export/exportMd.ts@@ -0,0 +1,149 @@+import {Constants} from "../../constants";+import {Dialog} from "../../dialog";+import {showMessage} from "../../dialog/message";+import {fetchPost} from "../../util/fetch";+import {isMobile} from "../../util/functions";+import {saveExportFile} from "../util/compatibility";++// 导出参数对话框 https://github.com/siyuan-note/siyuan/issues/17031+// 通用部分(8 项)可被各导出格式复用,Markdown 专属部分仅 Markdown 导出使用。+// 默认值一律取自全局 window.siyuan.config.export,确认后本次导出生效,不修改全局设置、不记忆上次选择。++interface IExportMdOptions {+ id?: string;+ ids?: string[];+ notebook?: string;+}++// openExportOptionsDialog 渲染「通用 + Markdown 专属」两组开关,确认时回调 onConfirm 传出全部 13 项。+export const openExportOptionsDialog = (onConfirm: (options: IExportMdOptionsPayload) => void) => {+ const conf = window.siyuan.config.export;+ const bool = (id: string) => `<input id="${id}" class="b3-switch fn__flex-center" type="checkbox" ${conf[id] ? "checked" : ""}>`;+ // 渲染 select:复用设置面板的标准 class(fn__flex-center fn__size200),value 为当前全局值时标记 selected+ const select = (id: string, options: {value: number; label: string}[]) => {+ const opts = options.map(o =>+ `<option value="${o.value}" ${conf[id] === o.value ? "selected" : ""}>${o.label}</option>`).join("");+ return `<select id="${id}" class="b3-select fn__flex-center fn__size200">${opts}</select>`;+ };+ // 一行:左侧标题+说明,右侧控件。复用设置面板标准布局 class(config-item config-wrap)+ const row = (title: string, desc: string, control: string) =>+ `<label class="fn__flex b3-label config-item config-wrap">+ <div class="fn__flex-1">+ <div class="config-name">${title}</div>+ <div class="b3-label__text">${desc}</div>+ </div>+ <span class="fn__space"></span>+ ${control}+ </label>`;+ // 左右两个输入框,复用设置面板标准宽度 class(fn__size96)+ const textPair = (leftId: string, rightId: string) =>+ `<input id="${leftId}" class="b3-text-field fn__flex-center fn__size96" value="${conf[leftId] ?? ""}">+ <span class="fn__space"></span>+ <input id="${rightId}" class="b3-text-field fn__flex-center fn__size96" value="${conf[rightId] ?? ""}">`;++ const dialog = new Dialog({+ title: window.siyuan.languages.export + " Markdown",+ content: `<div class="b3-dialog__content export-md__content">+ <!-- 常用 -->+ ${row(window.siyuan.languages.export17, window.siyuan.languages.export18, bool("addTitle"))}+ ${row(window.siyuan.languages.includeSubDocs, window.siyuan.languages.includeSubDocsTip, bool("includeSubDocs"))}+ ${row(window.siyuan.languages.includeRelatedDocs, window.siyuan.languages.includeRelatedDocsTip, bool("includeRelatedDocs"))}+ ${row(window.siyuan.languages.export23, window.siyuan.languages.export24, bool("markdownYFM"))}+ ${row(window.siyuan.languages.removeAssetsID, window.siyuan.languages.removeAssetsIDTip, bool("removeAssetsID"))}+ <!-- 其他 -->+ ${row(window.siyuan.languages.export31, window.siyuan.languages.export32, bool("inlineMemo"))}+ ${row(window.siyuan.languages.ref, window.siyuan.languages.export11,+ select("blockRefMode", [+ {value: 2, label: window.siyuan.languages.export2},+ {value: 3, label: window.siyuan.languages.export3},+ {value: 4, label: window.siyuan.languages.export4},+ ]))}+ ${row(window.siyuan.languages.blockEmbed, window.siyuan.languages.export12,+ select("blockEmbedMode", [+ {value: 0, label: window.siyuan.languages.export0},+ {value: 1, label: window.siyuan.languages.export1},+ ]))}+ ${row(window.siyuan.languages.export5, window.siyuan.languages.export6,+ select("fileAnnotationRefMode", [+ {value: 0, label: window.siyuan.languages.export7},+ {value: 1, label: window.siyuan.languages.export8},+ ]))}+ ${row(window.siyuan.languages.export13, window.siyuan.languages.export14,+ textPair("blockRefTextLeft", "blockRefTextRight"))}+ ${row(window.siyuan.languages.export15, window.siyuan.languages.export16,+ textPair("tagOpenMarker", "tagCloseMarker"))}+</div>+<div class="b3-dialog__action">+ <button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>+ <button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>+</div>`,+ width: "520px",+ height: isMobile() ? "70vh" : "60vh",+ });+ dialog.element.setAttribute("data-key", Constants.DIALOG_EXPORTMARKDOWN);++ const el = dialog.element;+ const collect = (): IExportMdOptionsPayload => ({+ addTitle: (el.querySelector("#addTitle") as HTMLInputElement).checked,+ inlineMemo: (el.querySelector("#inlineMemo") as HTMLInputElement).checked,+ blockRefMode: parseInt((el.querySelector("#blockRefMode") as HTMLSelectElement).value, 10),+ blockEmbedMode: parseInt((el.querySelector("#blockEmbedMode") as HTMLSelectElement).value, 10),+ fileAnnotationRefMode: parseInt((el.querySelector("#fileAnnotationRefMode") as HTMLSelectElement).value, 10),+ blockRefTextLeft: (el.querySelector("#blockRefTextLeft") as HTMLInputElement).value,+ blockRefTextRight: (el.querySelector("#blockRefTextRight") as HTMLInputElement).value,+ tagOpenMarker: (el.querySelector("#tagOpenMarker") as HTMLInputElement).value,+ tagCloseMarker: (el.querySelector("#tagCloseMarker") as HTMLInputElement).value,+ includeSubDocs: (el.querySelector("#includeSubDocs") as HTMLInputElement).checked,+ includeRelatedDocs: (el.querySelector("#includeRelatedDocs") as HTMLInputElement).checked,+ markdownYFM: (el.querySelector("#markdownYFM") as HTMLInputElement).checked,+ removeAssetsID: (el.querySelector("#removeAssetsID") as HTMLInputElement).checked,+ });++ const btnsElement = el.querySelectorAll(".b3-button");+ btnsElement[0].addEventListener("click", () => {+ dialog.destroy();+ });+ btnsElement[1].addEventListener("click", () => {+ const payload = collect();+ dialog.destroy();+ onConfirm(payload);+ });+};++interface IExportMdOptionsPayload {+ addTitle: boolean;+ inlineMemo: boolean;+ blockRefMode: number;+ blockEmbedMode: number;+ fileAnnotationRefMode: number;+ blockRefTextLeft: string;+ blockRefTextRight: string;+ tagOpenMarker: string;+ tagCloseMarker: string;+ includeSubDocs: boolean;+ includeRelatedDocs: boolean;+ markdownYFM: boolean;+ removeAssetsID: boolean;+}++// exportMarkdownZip 为 Markdown .zip 导出入口:弹参数对话框,确认后按 id/ids/notebook 调对应 API。+export const exportMarkdownZip = (options: IExportMdOptions) => {+ openExportOptionsDialog(params => {+ const msgId = showMessage(window.siyuan.languages.exporting, -1);+ let url: string;+ let payload: IObject;+ if (options.id) {+ url = "/api/export/exportMd";+ payload = {id: options.id, ...params};+ } else if (options.ids) {+ url = "/api/export/exportMds";+ payload = {ids: options.ids, ...params};+ } else {+ url = "/api/export/exportNotebookMd";+ payload = {notebook: options.notebook, ...params};+ }+ fetchPost(url, payload, response => {+ saveExportFile(response.data.zip, msgId);+ });+ });+};diff --git a/kernel/api/export.go b/kernel/api/export.goindex d86c892542..c7ebbf2ef3 100644--- a/kernel/api/export.go+++ b/kernel/api/export.go@@ -389,7 +389,7 @@ func exportNotebookMd(c *gin.Context) {if !util.ParseJsonArgs(arg, ret, util.BindJsonArg("notebook", ¬ebook, true, true)) {return}- zipPath := model.ExportNotebookMarkdown(notebook)+ zipPath := model.ExportNotebookMarkdownWithOptions(notebook, model.ParseExportOptions(arg))ret.Data = map[string]any{"name": path.Base(zipPath),"zip": zipPath,@@ -411,7 +411,7 @@ func exportMds(c *gin.Context) {ids = append(ids, id.(string))}- name, zipPath := model.ExportPandocConvertZip(ids, "", ".md")+ name, zipPath := model.ExportPandocConvertZipWithOptions(ids, "", ".md", model.ParseExportOptions(arg))ret.Data = map[string]any{"name": name,"zip": zipPath,@@ -431,7 +431,7 @@ func exportMd(c *gin.Context) {if !util.ParseJsonArgs(arg, ret, util.BindJsonArg("id", &id, true, true)) {return}- name, zipPath := model.ExportPandocConvertZip([]string{id}, "", ".md")+ name, zipPath := model.ExportPandocConvertZipWithOptions([]string{id}, "", ".md", model.ParseExportOptions(arg))ret.Data = map[string]any{"name": name,"zip": zipPath,diff --git a/kernel/model/export.go b/kernel/model/export.goindex 8e1de68512..c8e3c75064 100644--- a/kernel/model/export.go+++ b/kernel/model/export.go@@ -1721,6 +1721,149 @@ func ExportStdMarkdown(id string, assetsDestSpace2Underscore, fillCSSVar, adjustConf.Export.AddTitle, Conf.Export.InlineMemo, defBlockIDs, true, fillCSSVar)}+// ExportOptions 为单次导出提供的临时选项,缺省字段(nil)使用全局 Conf.Export 的值。+// 通用部分可被各导出格式复用,Markdown 专属部分仅 Markdown 导出使用。+// 用于「导出 Markdown 参数对话框」https://github.com/siyuan-note/siyuan/issues/17031+type ExportOptions struct {+ // 通用部分(PDF/Word/HTML/Markdown 均读取)+ AddTitle *bool `json:"addTitle"` // 是否添加文档标题+ InlineMemo *bool `json:"inlineMemo"` // 是否导出行级备注+ BlockRefMode *int `json:"blockRefMode"` // 内容块引用导出模式+ BlockEmbedMode *int `json:"blockEmbedMode"` // 内容块嵌入导出模式+ FileAnnotationRefMode *int `json:"fileAnnotationRefMode"` // 文件标注引用导出模式+ BlockRefTextLeft *string `json:"blockRefTextLeft"` // 块引锚文本左侧符号+ BlockRefTextRight *string `json:"blockRefTextRight"` // 块引锚文本右侧符号+ TagOpenMarker *string `json:"tagOpenMarker"` // 标签开始标记符+ TagCloseMarker *string `json:"tagCloseMarker"` // 标签结束标记符+ // Markdown 专属部分+ IncludeSubDocs *bool `json:"includeSubDocs"` // 是否包含子文档+ IncludeRelatedDocs *bool `json:"includeRelatedDocs"` // 是否包含关联文档+ MarkdownYFM *bool `json:"markdownYFM"` // 是否添加 YAML Front Matter+ RemoveAssetsID *bool `json:"removeAssetsID"` // 是否移除资源文件名中的 ID+}++// applyExportOptions 临时用 opts 覆盖 Conf.Export,返回还原函数(务必 defer 调用)。+// 用于「导出 Markdown 参数对话框」#17031,导出过程串行执行,覆盖期间无并发风险。+func applyExportOptions(opts *ExportOptions) func() {+ snapshot := *Conf.Export // 值拷贝保存原状+ if nil != opts {+ if nil != opts.AddTitle {+ Conf.Export.AddTitle = *opts.AddTitle+ }+ if nil != opts.InlineMemo {+ Conf.Export.InlineMemo = *opts.InlineMemo+ }+ if nil != opts.BlockRefMode {+ Conf.Export.BlockRefMode = *opts.BlockRefMode+ }+ if nil != opts.BlockEmbedMode {+ Conf.Export.BlockEmbedMode = *opts.BlockEmbedMode+ }+ if nil != opts.FileAnnotationRefMode {+ Conf.Export.FileAnnotationRefMode = *opts.FileAnnotationRefMode+ }+ if nil != opts.BlockRefTextLeft {+ Conf.Export.BlockRefTextLeft = *opts.BlockRefTextLeft+ }+ if nil != opts.BlockRefTextRight {+ Conf.Export.BlockRefTextRight = *opts.BlockRefTextRight+ }+ if nil != opts.TagOpenMarker {+ Conf.Export.TagOpenMarker = *opts.TagOpenMarker+ }+ if nil != opts.TagCloseMarker {+ Conf.Export.TagCloseMarker = *opts.TagCloseMarker+ }+ if nil != opts.IncludeSubDocs {+ Conf.Export.IncludeSubDocs = *opts.IncludeSubDocs+ }+ if nil != opts.IncludeRelatedDocs {+ Conf.Export.IncludeRelatedDocs = *opts.IncludeRelatedDocs+ }+ if nil != opts.MarkdownYFM {+ Conf.Export.MarkdownYFM = *opts.MarkdownYFM+ }+ if nil != opts.RemoveAssetsID {+ Conf.Export.RemoveAssetsID = *opts.RemoveAssetsID+ }+ }+ return func() { *Conf.Export = snapshot }+}++// ExportPandocConvertZipWithOptions 在 ExportPandocConvertZip 基础上接受单次导出选项 #17031。+func ExportPandocConvertZipWithOptions(ids []string, pandocTo, ext string, opts *ExportOptions) (name, zipPath string) {+ restore := applyExportOptions(opts)+ defer restore()+ return ExportPandocConvertZip(ids, pandocTo, ext)+}++// ExportNotebookMarkdownWithOptions 在 ExportNotebookMarkdown 基础上接受单次导出选项 #17031。+func ExportNotebookMarkdownWithOptions(boxID string, opts *ExportOptions) (zipPath string) {+ restore := applyExportOptions(opts)+ defer restore()+ return ExportNotebookMarkdown(boxID)+}++// ParseExportOptions 从 JSON 请求参数中解析导出选项,未传入的字段保持 nil(沿用全局配置)#17031。+func ParseExportOptions(arg map[string]any) (opts *ExportOptions) {+ opts = &ExportOptions{}+ // 通用部分+ if nil != arg["addTitle"] {+ v := arg["addTitle"].(bool)+ opts.AddTitle = &v+ }+ if nil != arg["inlineMemo"] {+ v := arg["inlineMemo"].(bool)+ opts.InlineMemo = &v+ }+ if nil != arg["blockRefMode"] {+ v := int(arg["blockRefMode"].(float64))+ opts.BlockRefMode = &v+ }+ if nil != arg["blockEmbedMode"] {+ v := int(arg["blockEmbedMode"].(float64))+ opts.BlockEmbedMode = &v+ }+ if nil != arg["fileAnnotationRefMode"] {+ v := int(arg["fileAnnotationRefMode"].(float64))+ opts.FileAnnotationRefMode = &v+ }+ if nil != arg["blockRefTextLeft"] {+ v := arg["blockRefTextLeft"].(string)+ opts.BlockRefTextLeft = &v+ }+ if nil != arg["blockRefTextRight"] {+ v := arg["blockRefTextRight"].(string)+ opts.BlockRefTextRight = &v+ }+ if nil != arg["tagOpenMarker"] {+ v := arg["tagOpenMarker"].(string)+ opts.TagOpenMarker = &v+ }+ if nil != arg["tagCloseMarker"] {+ v := arg["tagCloseMarker"].(string)+ opts.TagCloseMarker = &v+ }+ // Markdown 专属部分+ if nil != arg["includeSubDocs"] {+ v := arg["includeSubDocs"].(bool)+ opts.IncludeSubDocs = &v+ }+ if nil != arg["includeRelatedDocs"] {+ v := arg["includeRelatedDocs"].(bool)+ opts.IncludeRelatedDocs = &v+ }+ if nil != arg["markdownYFM"] {+ v := arg["markdownYFM"].(bool)+ opts.MarkdownYFM = &v+ }+ if nil != arg["removeAssetsID"] {+ v := arg["removeAssetsID"].(bool)+ opts.RemoveAssetsID = &v+ }+ return+}+func ExportPandocConvertZip(ids []string, pandocTo, ext string) (name, zipPath string) {block := treenode.GetBlockTree(ids[0])box := Conf.Box(block.BoxID)