Skip to content

Commit fd65fcc

Browse files
committed
fix pasting of markdown containing links
1 parent ca6eeb0 commit fd65fcc

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/paste-markdown-html.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ function onPaste(event: ClipboardEvent) {
3737
// Generate DOM tree from HTML string
3838
const parser = new DOMParser()
3939
const doc = parser.parseFromString(textHTMLClean, 'text/html')
40-
const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_ELEMENT, node =>
40+
const whatToShow = navigator.userAgent.indexOf('Firefox') > -1 ? NodeFilter.SHOW_ALL : NodeFilter.SHOW_ELEMENT
41+
const walker = doc.createTreeWalker(doc.body, whatToShow, node =>
4142
node.parentNode && isLink(node.parentNode) ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT
4243
)
4344

@@ -62,14 +63,11 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
6263
// Walk through the DOM tree
6364
while (currentNode && index < NODE_LIMIT) {
6465
index++
66+
const currentNodeText =
67+
navigator.userAgent.indexOf('Firefox') > -1 ? (currentNode as Text) : (currentNode.firstChild as Text)
6568
const text = isLink(currentNode)
6669
? (currentNode.textContent || '').replace(/[\t\n\r ]+/g, ' ')
67-
: (currentNode.firstChild as Text)?.wholeText || ''
68-
69-
// update value of markdownIgnoreBeforeIndex with current index if the current node is not a link
70-
if (!isLink(currentNode)) {
71-
markdownIgnoreBeforeIndex += text.replace(/[\t\n\r ]+/g, ' ').trimStart().length
72-
}
70+
: currentNodeText?.wholeText || ''
7371

7472
// No need to transform whitespace
7573
if (isEmptyString(text)) {
@@ -88,6 +86,8 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
8886
markdown =
8987
markdown.slice(0, markdownFoundIndex) + markdownLink + markdown.slice(markdownFoundIndex + text.length)
9088
markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink.length
89+
} else {
90+
markdownIgnoreBeforeIndex = markdownFoundIndex + text.length
9191
}
9292
}
9393

0 commit comments

Comments
 (0)