mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
fix: improve the speed of generating highlighted markdown content
This commit is contained in:
parent
15b977f7c6
commit
15b41560de
2 changed files with 32 additions and 8 deletions
|
|
@ -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 (
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue