From f6e41ac4d99cb316773418922730c92f57c31473 Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Fri, 6 May 2022 00:09:26 +0800 Subject: [PATCH] fix: add link bugs --- src/events.ts | 36 ++++++++++++++++++---- src/knowledge.ts | 77 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 93 insertions(+), 20 deletions(-) diff --git a/src/events.ts b/src/events.ts index 769a399..d973889 100644 --- a/src/events.ts +++ b/src/events.ts @@ -469,14 +469,14 @@ class AddonEvents extends AddonBase { Zotero.Prefs.get("Knowledge4Zotero.mainKnowledgeID") ) { // Update current line index - let focusNode = - message.content.editorInstance._iframeWindow.document.getSelection() - .focusNode; + let selection = + message.content.editorInstance._iframeWindow.document.getSelection(); + let focusNode = selection.focusNode as XUL.Element; if (!focusNode) { return; } - function getChildIndex(node: Node) { + function getChildIndex(node) { return Array.prototype.indexOf.call(node.parentNode.childNodes, node); } @@ -486,15 +486,39 @@ class AddonEvents extends AddonBase { !focusNode.parentElement.className || focusNode.parentElement.className.indexOf("primary-editor") === -1 ) { - focusNode = focusNode.parentNode; + focusNode = focusNode.parentNode as XUL.Element; } } catch (e) { return; } let currentLineIndex = getChildIndex(focusNode); - this._Addon.knowledge.currentLine = currentLineIndex; + + const tableElements = Array.prototype.filter.call( + Array.prototype.slice.call( + focusNode.parentElement.childNodes, + 0, + currentLineIndex + ), + (e) => { + return e.tagName === "UL" || e.tagName === "OL"; + } + ); + + for (const tableElement of tableElements) { + currentLineIndex += tableElement.childElementCount - 1; + } + + const tagName = focusNode.tagName; + if (tagName === "UL" || tagName === "OL") { + let liElement = selection.focusNode as XUL.Element; + while (liElement.tagName !== "LI") { + liElement = liElement.parentElement; + } + currentLineIndex += getChildIndex(liElement); + } Zotero.debug(`Knowledge4Zotero: line ${currentLineIndex} selected.`); + this._Addon.knowledge.currentLine = currentLineIndex; } } else if (message.type === "export") { /* diff --git a/src/knowledge.ts b/src/knowledge.ts index 85a8ec7..c1c53da 100644 --- a/src/knowledge.ts +++ b/src/knowledge.ts @@ -132,8 +132,50 @@ class Knowledge extends AddonBase { noteText.length - "".length ); } - let noteLines: string[] = noteText.split("\n").filter((s) => s); - return noteLines; + let noteLines: string[] = noteText.split("\n"); + + const cacheStart = [/
/g, //g];
+ const cacheEnd = [
+ /<\/ol>/g,
+ /<\/ul>/g,
+ /<\/li>/g,
+ /<\/blockquote>/g,
+ /<\/pre>/g,
+ ];
+ let parsedLines: string[] = [];
+ let appendLater: boolean = false;
+ let cacheStartLine = false;
+ let cachedLines: string = "";
+ for (let line of noteLines) {
+ cacheStartLine = false;
+ if (
+ cachedLines ||
+ cacheStart.filter((e) => {
+ return line.search(e) !== -1;
+ }).length > 0
+ ) {
+ appendLater = true;
+ cachedLines += `${cachedLines.length > 0 ? "\n" : ""}${line}`;
+ cacheStartLine = true;
+ }
+ if (
+ cacheEnd.filter((e) => {
+ return line.search(e) !== -1;
+ }).length > 0
+ ) {
+ appendLater = false;
+ // If already added to cache
+ if (!cacheStartLine) {
+ cachedLines += `\n${line}`;
+ }
+ line = cachedLines;
+ cachedLines = "";
+ }
+ if (!appendLater) {
+ parsedLines.push(line);
+ }
+ }
+ return parsedLines;
}
setLinesToNote(note: ZoteroItem, noteLines: string[]) {
@@ -401,7 +443,7 @@ class Knowledge extends AddonBase {
if (!note) {
return undefined;
}
- let metadataContainer = this.parseNoteHTML(note);
+ const noteLines = this.getLinesInNote(note);
let tree = new TreeModel();
/*
tree-model/index.js: line 40
@@ -419,21 +461,28 @@ class Knowledge extends AddonBase {
lineIndex: -1,
endIndex: -1,
});
- if (!metadataContainer) {
- return root;
- }
let id = 0;
let currentNode = root;
let lastNode = undefined;
- for (let i = 0; i < metadataContainer.children.length; i++) {
+ let headerStartReg = new RegExp("");
+ let headerStopReg = new RegExp(" ");
+ for (let i in noteLines) {
let currentRank = 7;
- let lineElement = metadataContainer.children[i];
- const isHeading =
- lineElement.tagName[0] === "H" && lineElement.tagName.length === 2;
- const isLink = lineElement.innerHTML.search(/zotero:\/\/note\//g) !== -1;
+ let lineElement = noteLines[i];
+ const isHeading = lineElement.search(headerStartReg) !== -1;
+ const isLink = lineElement.search(/zotero:\/\/note\//g) !== -1;
if (isHeading || isLink) {
+ let name = "";
if (isHeading) {
- currentRank = parseInt(lineElement.tagName[1]);
+ const startIndex = lineElement.search(headerStartReg);
+ const stopIndex = lineElement.search(headerStopReg);
+ currentRank = parseInt(
+ lineElement.slice(startIndex + 2, startIndex + 3)
+ );
+ name = lineElement.slice(startIndex + 4, stopIndex);
+ } else {
+ name = lineElement.slice(lineElement.search(/">/g) + 2);
+ name = name.slice(0, name.search(/<\//g));
}
while (currentNode.model.rank >= currentRank) {
currentNode = currentNode.parent;
@@ -445,9 +494,9 @@ class Knowledge extends AddonBase {
id: id++,
rank: currentRank,
// @ts-ignore
- name: lineElement.innerText,
+ name: name,
lineIndex: i,
- endIndex: metadataContainer.children.length,
+ endIndex: noteLines.length,
});
currentNode.addChild(lastNode);
currentNode = lastNode;