Skip to content

Commit 6fe9ef1

Browse files
authored
Merge pull request #20 from github/support-multiple-tables
Parse all tables in clipboard payload
2 parents 934fb5e + 44de6b7 commit 6fe9ef1

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

src/paste-markdown-table.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,22 @@ function tableMarkdown(node: Element): string {
8484
function generateText(transfer: DataTransfer): string | undefined {
8585
if (Array.from(transfer.types).indexOf('text/html') === -1) return
8686

87-
const html = transfer.getData('text/html')
87+
let html = transfer.getData('text/html')
8888
if (!/<table/i.test(html)) return
8989

90+
html = html.replace(/<meta.*?>/, '')
91+
9092
const el = document.createElement('div')
9193
el.innerHTML = html
92-
let table = el.querySelector('table')
93-
table = !table || table.closest('[data-paste-markdown-skip]') ? null : table
94-
if (!table) return
95-
96-
const formattedTable = tableMarkdown(table)
94+
const tables = el.querySelectorAll('table')
95+
96+
for (const table of tables) {
97+
if (table.closest('[data-paste-markdown-skip]')) {
98+
table.replaceWith(new Text(table.textContent || ''))
99+
}
100+
const formattedTable = tableMarkdown(table)
101+
table.replaceWith(new Text(formattedTable))
102+
}
97103

98-
return html.replace(/<meta.*?>/, '').replace(/<table[.\S\s]*<\/table>/, `\n${formattedTable}`)
104+
return el.innerHTML
99105
}

test/test.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,37 @@ describe('paste-markdown', function () {
5757
assert.equal(
5858
textarea.value.trim(),
5959
// eslint-disable-next-line github/unescaped-html-literal
60-
'<p>Here is a cool table</p>\n \n\nname | origin\n-- | --\nhubot | github\nbender | futurama\n\n\n <p>Very cool</p>'
60+
'<p>Here is a cool table</p>\n \nname | origin\n-- | --\nhubot | github\nbender | futurama\n\n\n <p>Very cool</p>'
61+
)
62+
})
63+
64+
it('pastes multiple tables', async function () {
65+
const data = {
66+
'text/html': `
67+
<p>Here is a cool table</p>
68+
<table>
69+
<thead><tr><th>name</th><th>origin</th></tr></thead>
70+
<tbody>
71+
<tr><td>hubot</td><td>github</td></tr>
72+
<tr><td>bender</td><td>futurama</td></tr>
73+
</tbody>
74+
</table>
75+
<p>Another very cool table</p>
76+
<table>
77+
<thead><tr><th>name</th><th>origin</th></tr></thead>
78+
<tbody>
79+
<tr><td>hubot</td><td>github</td></tr>
80+
<tr><td>bender</td><td>futurama</td></tr>
81+
</tbody>
82+
</table>
83+
`
84+
}
85+
86+
paste(textarea, data)
87+
assert.equal(
88+
textarea.value.trim(),
89+
// eslint-disable-next-line github/unescaped-html-literal
90+
'<p>Here is a cool table</p>\n \nname | origin\n-- | --\nhubot | github\nbender | futurama\n\n\n <p>Another very cool table</p>\n \nname | origin\n-- | --\nhubot | github\nbender | futurama'
6191
)
6292
})
6393

@@ -75,9 +105,10 @@ describe('paste-markdown', function () {
75105
}
76106
paste(textarea, data)
77107

78-
// Synthetic paste events don't manipulate the DOM. A empty textarea
79-
// means that the event handler didn't fire and normal paste happened.
80-
assert.equal(textarea.value, '')
108+
assert.equal(
109+
textarea.value,
110+
'\n \n nameorigin\n \n hubotgithub\n benderfuturama\n \n \n '
111+
)
81112
})
82113

83114
it('accepts x-gfm', function () {

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"target": "es2017",
55
"lib": [
66
"es2018",
7-
"dom"
7+
"dom",
8+
"dom.iterable"
89
],
910
"strict": true,
1011
"declaration": true,

0 commit comments

Comments
 (0)