From 15b41560de3018f862ea0069bc9347a5388dad2f Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 26 May 2023 15:51:54 +0800 Subject: [PATCH] fix: improve the speed of generating highlighted markdown content --- packages/api/src/utils/highlightGenerator.ts | 32 +++++++++++++++----- packages/api/src/utils/parser.ts | 8 ++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/api/src/utils/highlightGenerator.ts b/packages/api/src/utils/highlightGenerator.ts index 58bcf325d..cc3ccca0f 100644 --- a/packages/api/src/utils/highlightGenerator.ts +++ b/packages/api/src/utils/highlightGenerator.ts @@ -1,4 +1,5 @@ import { diff_match_patch as DiffMatchPatch } from 'diff-match-patch' +import { parseHTML } from 'linkedom' import { nanoid } from 'nanoid' import { v4 as uuidv4 } from 'uuid' import { interpolationSearch } from './interpolationSearch' @@ -381,18 +382,35 @@ const fillHighlight = ({ } } +export function getArticleTextNodes( + document: Document +): ArticleTextContent | null { + try { + const rootNode = document.getRootNode() + return getTextNodesBetween(rootNode, rootNode, rootNode) + } catch (error) { + console.log(error) + return null + } +} + export function makeHighlightNodeAttributes( id: string, patch: string, - document: Document + articleTextNodes: ArticleTextContent ) { - const rootNode = document.getRootNode() + const document = parseHTML('').document + const textNodes = articleTextNodes.textNodes + const { highlightTextStart, highlightTextEnd } = selectionOffsetsFromPatch( + articleTextNodes.articleText, + patch + ) - const allArticleNodes = getTextNodesBetween(rootNode, rootNode, rootNode) - const { highlightTextStart, highlightTextEnd, textNodes, textNodeIndex } = - getPrefixAndSuffix(allArticleNodes, patch) - - let startingTextNodeIndex = textNodeIndex + // Searching for the starting text node using interpolation search algorithm + let startingTextNodeIndex = interpolationSearch( + textNodes.map(({ startIndex: startIndex }) => startIndex), + highlightTextStart + ) let quote = '' while ( diff --git a/packages/api/src/utils/parser.ts b/packages/api/src/utils/parser.ts index d3f05c1d2..6d255677a 100644 --- a/packages/api/src/utils/parser.ts +++ b/packages/api/src/utils/parser.ts @@ -24,6 +24,7 @@ import { ArticleFormat } from '../resolvers/article' import { EmbeddedHighlightData, findEmbeddedHighlight, + getArticleTextNodes, highlightIdAttribute, makeHighlightNodeAttributes, } from './highlightGenerator' @@ -627,6 +628,11 @@ export const htmlToHighlightedMarkdown = ( return nhm.translate(/* html */ html) } + const articleTextNodes = getArticleTextNodes(document) + if (!articleTextNodes) { + return nhm.translate(/* html */ html) + } + // wrap highlights in special tags highlights .filter((h) => h.type == 'HIGHLIGHT' && h.patch) @@ -635,7 +641,7 @@ export const htmlToHighlightedMarkdown = ( makeHighlightNodeAttributes( highlight.id, highlight.patch as string, - document + articleTextNodes ) } catch (err) { console.log(err)