Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: fix extraction of blocks with links

Abdullah Atta831b8497b21f
diff --git a/packages/core/src/collections/notes.ts b/packages/core/src/collections/notes.ts
index 67d187483..7bfab4200 100644
--- a/packages/core/src/collections/notes.ts
+++ b/packages/core/src/collections/notes.ts
@@ -405,7 +405,7 @@ export class Notes implements ICollection {
if (!content || content.locked) return [];
return getContentFromData(content.type, content.data).extract(
- "linked-blocks"
+ "blocksWithLink"
).blocks;
}
diff --git a/packages/core/src/content-types/tiptap.ts b/packages/core/src/content-types/tiptap.ts
index cd7d0cd27..95e1717ec 100644
--- a/packages/core/src/content-types/tiptap.ts
+++ b/packages/core/src/content-types/tiptap.ts
@@ -35,14 +35,18 @@ import {
} from "../utils/html-parser";
import { HTMLRewriter } from "../utils/html-rewriter";
import { ContentBlock } from "../types";
-import { InternalLink, parseInternalLink } from "../utils/internal-link";
+import {
+ InternalLink,
+ isInternalLink,
+ parseInternalLink
+} from "../utils/internal-link";
import { Element } from "domhandler";
export type ResolveHashes = (
hashes: string[]
) => Promise<Record<string, string>>;
-const ExtractableTypes = ["blocks", "internalLinks", "linked-blocks"] as const;
+const ExtractableTypes = ["blocks", "internalLinks", "blocksWithLink"] as const;
type ExtractableType = (typeof ExtractableTypes)[number];
type ExtractionResult = {
blocks: ContentBlock[];
@@ -150,20 +154,24 @@ export class Tiptap {
withStartIndices: true
});
- if (types.includes("linked-blocks")) {
+ if (types.includes("blocksWithLink")) {
result.blocks.push(
- ...findAll((element): element is Element => {
- return isTag(element) && element.tagName === "a";
- }, document.childNodes).map((node) => {
- let parent = node.parent as Element;
- while (!(parent as Element).attribs[ATTRIBUTES.blockId]) {
- parent = parent?.parent as Element;
- }
+ ...findAll((element) => {
+ return (
+ !!element.attribs[ATTRIBUTES.blockId] &&
+ element.childNodes.some(
+ (node) =>
+ isTag(node) &&
+ node.tagName === "a" &&
+ isInternalLink(node.attribs.href)
+ )
+ );
+ }, document.childNodes).map((element) => {
return {
- id: parent.attribs[ATTRIBUTES.blockId],
- type: parent.tagName.toLowerCase(),
+ id: element.attribs[ATTRIBUTES.blockId],
+ type: element.tagName.toLowerCase(),
content: convertHtmlToTxt(
- this.data.slice(parent.startIndex || 0, parent.endIndex || 0),
+ this.data.slice(element.startIndex || 0, element.endIndex || 0),
false
)
};
diff --git a/packages/core/src/utils/internal-link.ts b/packages/core/src/utils/internal-link.ts
index b247c9340..b26e1c8ee 100644
--- a/packages/core/src/utils/internal-link.ts
+++ b/packages/core/src/utils/internal-link.ts
@@ -65,6 +65,10 @@ export function parseInternalLink(link: string): InternalLink | undefined {
};
}
+export function isInternalLink(link: string) {
+ return link.startsWith("nn://");
+}
+
function isValidInternalType(type: string): type is InternalLinkType {
return InternalLinkTypes.includes(type as any);
}