Skip to content

Commit 8a0ee28

Browse files
authored
Merge pull request #16 from github/layout-table
Prevent parsing table[data-paste-markdown-skip]
2 parents 2e14bea + 00f317e commit 8a0ee28

4 files changed

Lines changed: 20 additions & 14 deletions

File tree

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Paste Markdown objects
22

3-
Paste spreadsheet cells as a Markdown table. Convert pasted image URLs to
4-
Markdown image link syntax.
3+
- Paste spreadsheet cells and HTML tables as a Markdown tables.
4+
- Paste image URLs as Markdown image links.
5+
- *Paste markdown as markdown. See [`@github/quote-selection`/Preserving markdown syntax](https://github.com/github/quote-selection/tree/9ae5f88f5bc3021f51d2dc9981eca83ce7cfe04f#preserving-markdown-syntax) for details.
56

67
## Installation
78

@@ -31,6 +32,16 @@ import subscribe from '@github/paste-markdown'
3132
observe('textarea[data-paste-markdown]', {subscribe})
3233
```
3334

35+
### Excluding `<table>`s
36+
37+
Some `<table>`s are not meant to be pasted as markdown; for example, a file content table with line numbers in a column. Use `data-paste-markdown-skip` to prevent it.
38+
39+
```html
40+
<table data-paste-markdown-skip>
41+
...
42+
</table>
43+
```
44+
3445
## Development
3546

3647
```

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</tbody>
3030
</table>
3131

32-
<table border="1" cellpadding="5" class="js-comment">
32+
<table border="1" cellpadding="5" data-paste-markdown-skip>
3333
<thead>
3434
<tr>
3535
<th>name</th>

src/paste-markdown-table.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,12 @@ function parseTable(html: string): HTMLElement | null {
9595
return el.querySelector('table')
9696
}
9797

98-
function hasTable(transfer: DataTransfer): HTMLElement | null | void {
99-
if (Array.from(transfer.types).indexOf('text/html') === -1) return
98+
function hasTable(transfer: DataTransfer): HTMLElement | null {
99+
if (Array.from(transfer.types).indexOf('text/html') === -1) return null
100100

101101
const html = transfer.getData('text/html')
102-
if (!/<table/i.test(html)) return
102+
if (!/<table/i.test(html)) return null
103103

104104
const table = parseTable(html)
105-
if (!table) return
106-
107-
// Edge copies the surrounding table from our issue comment text.
108-
if (table.closest('.js-comment')) return
109-
110-
return /\b(js|blob|diff)-./.test(table.className) ? null : table
105+
return !table || table.closest('[data-paste-markdown-skip]') ? null : table
111106
}

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ describe('paste-markdown', function() {
3838
assert.include(textarea.value, 'name | origin\n-- | --\nhubot | github\nbender | futurama')
3939
})
4040

41-
it('rejects HTML from github.com markup', function() {
41+
it('rejects layout tables', function() {
4242
const data = {
4343
'text/html': `
44-
<table class="js-comment">
44+
<table data-paste-markdown-skip>
4545
<thead><tr><th>name</th><th>origin</th></tr></thead>
4646
<tbody>
4747
<tr><td>hubot</td><td>github</td></tr>

0 commit comments

Comments
 (0)