Skip to content

Commit

Permalink
Don't preserve whitespace from HTML in annotation text
Browse files Browse the repository at this point in the history
This is an imperfect approach because it might miss empty block elements that
nevertheless start new paragraphs, but it works well for most webpages and
typical EPUBs. We can fine-tune more later.

https://forums.zotero.org/discussion/107685/z7-beta-zotero-introduces-extra-line-breaks-in-snapshot-annotations
  • Loading branch information
AbeJellinek committed Sep 11, 2023
1 parent abf4f62 commit 5378d93
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/dom/epub/epub-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import Epub, {
} from "epubjs";
import {
moveRangeEndsIntoTextNodes,
PersistentRange
PersistentRange,
splitRangeToTextNodes
} from "../common/lib/range";
import {
FragmentSelector,
Expand Down Expand Up @@ -350,10 +351,32 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
if (range.collapsed) {
return null;
}
let text = type == 'highlight' || type == 'underline' ? range.toString().trim() : undefined;
// If this annotation type wants text, but we didn't get any, abort
if (text === '') {
return null;
let text;
if (type == 'highlight' || type == 'underline') {
text = '';
let lastSplitRange;
for (let splitRange of splitRangeToTextNodes(range)) {
if (lastSplitRange) {
let lastSplitRangeContainer = closestElement(lastSplitRange.commonAncestorContainer);
let lastSplitRangeBlock = lastSplitRangeContainer && getContainingBlock(lastSplitRangeContainer);
let splitRangeContainer = closestElement(splitRange.commonAncestorContainer);
let splitRangeBlock = splitRangeContainer && getContainingBlock(splitRangeContainer);
if (lastSplitRangeBlock !== splitRangeBlock) {
text += '\n\n';
}
}
text += splitRange.toString().replace(/\s+/g, ' ');
lastSplitRange = splitRange;
}
text = text.trim();

// If this annotation type wants text, but we didn't get any, abort
if (!text) {
return null;
}
}
else {
text = undefined;
}

let selector = this.toSelector(range);
Expand Down

0 comments on commit 5378d93

Please sign in to comment.