Skip to content

Commit 983a716

Browse files
committed
fix: skip child nodes of links
1 parent 287767d commit 983a716

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/paste-markdown-html.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ function onPaste(event: ClipboardEvent) {
3030
// Generate DOM tree from HTML string
3131
const parser = new DOMParser()
3232
const doc = parser.parseFromString(textHTMLClean, 'text/html')
33-
const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_ELEMENT)
33+
const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_ELEMENT, node =>
34+
node.parentNode && isLink(node.parentNode) ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT
35+
)
3436

3537
const markdown = convertToMarkdown(plaintext, walker)
3638

test/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ describe('paste-markdown', function () {
148148
assert.equal(textarea.value, markdownSentence)
149149
})
150150

151+
it('deals with links with nested html', function () {
152+
// eslint-disable-next-line github/unescaped-html-literal
153+
const sentence = `<a href="https://example.com/"><span>foo</span></a>
154+
<a href="https://example.com/">bar</a>
155+
foo bar`
156+
const plaintextSentence = 'foo bar foo bar'
157+
const markdownSentence = '[foo](https://example.com/) [bar](https://example.com/) foo bar'
158+
159+
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
160+
assert.equal(textarea.value, markdownSentence)
161+
})
162+
151163
it("doesn't render any markdown for html link without corresponding plaintext", function () {
152164
// eslint-disable-next-line github/unescaped-html-literal
153165
const link = `<meta charset='utf-8'><a href="https://github.com/monalisa/playground/issues/1">

0 commit comments

Comments
 (0)