From f5be73be8b394750b42c4aa4885380c695ace2da Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Sun, 20 Nov 2022 00:36:28 +0800 Subject: [PATCH] add: export mindmap with note content --- src/note/noteParse.ts | 72 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/src/note/noteParse.ts b/src/note/noteParse.ts index 5eb2618..05bf095 100644 --- a/src/note/noteParse.ts +++ b/src/note/noteParse.ts @@ -38,7 +38,10 @@ class NoteParse extends AddonBase { return randomstring; } - public parseNoteTree(note: Zotero.Item): TreeModel.Node { + public parseNoteTree( + note: Zotero.Item, + parseLink: boolean = true + ): TreeModel.Node { const noteLines = this._Addon.NoteUtils.getLinesInNote(note); let tree = new TreeModel(); let root = tree.parse({ @@ -55,7 +58,8 @@ class NoteParse extends AddonBase { let currentRank = 7; let lineElement = noteLines[i]; const isHeading = lineElement.search(headerStartReg) !== -1; - const isLink = lineElement.search(/zotero:\/\/note\//g) !== -1; + const isLink = + parseLink && lineElement.search(/zotero:\/\/note\//g) !== -1; if (isHeading || isLink) { let name = ""; let link = ""; @@ -452,17 +456,73 @@ class NoteParse extends AddonBase { return asciidoctor.convert(str); } - parseNoteToFreemind(noteItem: Zotero.Item, options: {} = {}) { - const root = this.parseNoteTree(noteItem); + parseNoteToFreemind( + noteItem: Zotero.Item, + options: { withContent?: boolean } = { withContent: true } + ) { + const root = this.parseNoteTree(noteItem, false); + const textNodeForEach = (e: Node, callbackfn: Function) => { + if (e.nodeType === document.TEXT_NODE) { + callbackfn(e); + return; + } + e.childNodes.forEach((_e) => textNodeForEach(_e, callbackfn)); + }; const html2Escape = (sHtml: string) => { return sHtml.replace(/[<>&"]/g, function (c) { return { "<": "<", ">": ">", "&": "&", '"': """ }[c]; }); }; + let lines = []; + if (options.withContent) { + const instance = this._Addon.WorkspaceWindow.getEditorInstance(noteItem); + const editorCopy = this._Addon.EditorViews.getEditorElement( + instance._iframeWindow.document + ).cloneNode(true) as HTMLElement; + textNodeForEach(editorCopy, (e: Text) => { + e.data = html2Escape(e.data); + }); + lines = this.parseHTMLElements(editorCopy as HTMLElement); + } + const convertClosingTags = (htmlStr: string) => { + const regConfs = [ + { + reg: /]*?>/g, + cbk: (str) => "

", + }, + { + reg: /]*?>/g, + cbk: (str: string) => { + return ``; + }, + }, + ]; + for (const regConf of regConfs) { + htmlStr = htmlStr.replace(regConf.reg, regConf.cbk); + } + return htmlStr; + }; const convertNode = (node: TreeModel.Node) => { mmXML += ``; + )}">`; + if ( + options.withContent && + node.model.lineIndex >= 0 && + node.model.endIndex >= 0 + ) { + mmXML += `${convertClosingTags( + lines + .slice( + node.model.lineIndex, + node.hasChildren() + ? node.children[0].model.lineIndex + : node.model.endIndex + 1 + ) + .map((e) => e.outerHTML) + .join("\n") + )}`; + } if (node.hasChildren()) { node.children.forEach((child: TreeModel.Node) => { convertNode(child); @@ -470,7 +530,7 @@ class NoteParse extends AddonBase { } mmXML += ""; }; - let mmXML = ''; + let mmXML = ''; convertNode(root); mmXML += ""; console.log(mmXML);