Skip to content

Commit

Permalink
fix: handling of various table formats
Browse files Browse the repository at this point in the history
Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud committed Mar 31, 2022
1 parent 9229d45 commit 0128849
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 16 deletions.
37 changes: 35 additions & 2 deletions src/nodes/Table.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
import { Table } from '@tiptap/extension-table'
import { mergeAttributes } from '@tiptap/core'
import { Node, mergeAttributes } from '@tiptap/core'

/*
* Markdown tables do not include captions.
* We still need to parse them though
* because otherwise tiptap will try to insert their text content
* and put it in the top row of the table.
*/
const tableCaption = Node.create({
name: 'tableCaption',
content: 'inline*',
addAttributes() {
return {}
},

renderHTML() {
return ['caption']
},

toMarkdown(state, node) {
},

parseHTML() {
return [
{ tag: 'table caption', priority: 90 },
]
},
})

export default Table.extend({
content: 'tableHead tableBody',
content: 'tableCaption? tableHead tableBody',

addExtensions() {
return [
tableCaption,
]
},

renderHTML({ HTMLAttributes }) {
return ['table', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
Expand Down
8 changes: 1 addition & 7 deletions src/nodes/TableBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ import { Node, mergeAttributes } from '@tiptap/core'

export default Node.create({
name: 'tableBody',
content: 'tableRow',
content: 'tableRow*',

addOptions() {
return {
HTMLAttributes: {},
}
},

parseHTML() {
return [
{ tag: 'tbody' },
]
},

renderHTML({ HTMLAttributes }) {
return ['tbody', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
Expand Down
8 changes: 8 additions & 0 deletions src/nodes/TableCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ export default TableCell.extend({
state.write(' |')
},

parseHTML() {
return [
{ tag: 'td' },
{ tag: 'th' },
{ tag: 'table thead ~ tbody th', priority: 70 },
{ tag: 'table thead ~ tbody td', priority: 70 },
]
},
})
6 changes: 1 addition & 5 deletions src/nodes/TableHead.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ export default Node.create({
},

addExtensions() {
return [tableHeadRow]
},

parseHTML() {
return [
{ tag: 'thead' },
tableHeadRow,
]
},

Expand Down
12 changes: 12 additions & 0 deletions src/nodes/TableHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ export default TableHeader.extend({
state.write(' |')
},

parseHTML() {
return [
{ tag: 'table thead:empty ~ tbody :first-child th', priority: 80 },
{ tag: 'table thead:empty ~ tbody :first-child td', priority: 80 },
{ tag: 'table thead :first-child th', priority: 60 },
{ tag: 'table thead :first-child td', priority: 60 },
{ tag: 'table tbody :first-child th', priority: 60 },
{ tag: 'table tbody :first-child td', priority: 60 },
{ tag: 'table > :first-child > th', priority: 60 },
{ tag: 'table > :first-child > td', priority: 60 },
]
},
})
5 changes: 5 additions & 0 deletions src/nodes/TableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ export default TableRow.extend({
state.renderInline(node)
},

parseHTML() {
return [
{ tag: 'tr', priority: 80 },
]
},
})
34 changes: 34 additions & 0 deletions src/tests/fixtures/tables/handbook-caption-tbody.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<table id="bkmrk-section-name" style="border-collapse: collapse; width: 100%; height: 150px;" border="1">
<caption>Testing tables
</caption>
<tbody>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Heading 0</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 1</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 2</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 3</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 4</strong></td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Letter</strong></td>
<td style="width: 20%; height: 29px;">a</td>
<td style="width: 20%; height: 29px;">b</td>
<td style="width: 20%; height: 29px;">c</td>
<td style="width: 20%; height: 29px;">d</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Number</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">2</td>
<td style="width: 20%; height: 29px;">3</td>
<td style="width: 20%; height: 29px;">4</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Square</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">4</td>
<td style="width: 20%; height: 29px;">9</td>
<td style="width: 20%; height: 29px;">16</td>
</tr>
</tbody>
</table>
34 changes: 34 additions & 0 deletions src/tests/fixtures/tables/handbook-empty-thead-tbody.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<table id="bkmrk-section-name" style="border-collapse: collapse; width: 100%; height: 150px;" border="1">
<thead>
</thead>
<tbody>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Heading 0</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 1</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 2</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 3</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 4</strong></td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Letter</strong></td>
<td style="width: 20%; height: 29px;">a</td>
<td style="width: 20%; height: 29px;">b</td>
<td style="width: 20%; height: 29px;">c</td>
<td style="width: 20%; height: 29px;">d</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Number</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">2</td>
<td style="width: 20%; height: 29px;">3</td>
<td style="width: 20%; height: 29px;">4</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Square</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">4</td>
<td style="width: 20%; height: 29px;">9</td>
<td style="width: 20%; height: 29px;">16</td>
</tr>
</tbody>
</table>
36 changes: 36 additions & 0 deletions src/tests/fixtures/tables/handbook-multiple-tbody.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<table id="bkmrk-section-name" style="border-collapse: collapse; width: 100%; height: 150px;" border="1">
<tbody>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Heading 0</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 1</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 2</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 3</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 4</strong></td>
</tr>
</tbody>
<tbody>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Letter</strong></td>
<td style="width: 20%; height: 29px;">a</td>
<td style="width: 20%; height: 29px;">b</td>
<td style="width: 20%; height: 29px;">c</td>
<td style="width: 20%; height: 29px;">d</td>
</tr>
</tbody>
<tbody>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Number</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">2</td>
<td style="width: 20%; height: 29px;">3</td>
<td style="width: 20%; height: 29px;">4</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Square</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">4</td>
<td style="width: 20%; height: 29px;">9</td>
<td style="width: 20%; height: 29px;">16</td>
</tr>
</tbody>
</table>
34 changes: 34 additions & 0 deletions src/tests/fixtures/tables/handbook-thead-tbody.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<table id="bkmrk-section-name" style="border-collapse: collapse; width: 100%; height: 150px;" border="1">
<thead>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Heading 0</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 1</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 2</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 3</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 4</strong></td>
</tr>
</thead>
<tbody>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Letter</strong></td>
<td style="width: 20%; height: 29px;">a</td>
<td style="width: 20%; height: 29px;">b</td>
<td style="width: 20%; height: 29px;">c</td>
<td style="width: 20%; height: 29px;">d</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Number</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">2</td>
<td style="width: 20%; height: 29px;">3</td>
<td style="width: 20%; height: 29px;">4</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Square</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">4</td>
<td style="width: 20%; height: 29px;">9</td>
<td style="width: 20%; height: 29px;">16</td>
</tr>
</tbody>
</table>
33 changes: 33 additions & 0 deletions src/tests/fixtures/tables/handbook-with-caption.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<table id="bkmrk-section-name" style="border-collapse: collapse; width: 100%; height: 150px;" border="1">
<caption>test</caption>
<tbody>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Heading 0</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 1</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 2</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 3</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 4</strong></td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Letter</strong></td>
<td style="width: 20%; height: 29px;">a</td>
<td style="width: 20%; height: 29px;">b</td>
<td style="width: 20%; height: 29px;">c</td>
<td style="width: 20%; height: 29px;">d</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Number</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">2</td>
<td style="width: 20%; height: 29px;">3</td>
<td style="width: 20%; height: 29px;">4</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Square</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">4</td>
<td style="width: 20%; height: 29px;">9</td>
<td style="width: 20%; height: 29px;">16</td>
</tr>
</tbody>
</table>
32 changes: 32 additions & 0 deletions src/tests/fixtures/tables/handbook.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<table id="bkmrk-section-name" style="border-collapse: collapse; width: 100%; height: 150px;" border="1">
<tbody>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Heading 0</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 1</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 2</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 3</strong></td>
<td style="width: 20%; height: 29px;"><strong>Heading 4</strong></td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Letter</strong></td>
<td style="width: 20%; height: 29px;">a</td>
<td style="width: 20%; height: 29px;">b</td>
<td style="width: 20%; height: 29px;">c</td>
<td style="width: 20%; height: 29px;">d</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Number</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">2</td>
<td style="width: 20%; height: 29px;">3</td>
<td style="width: 20%; height: 29px;">4</td>
</tr>
<tr style="height: 29px;">
<td style="width: 20%; height: 29px;"><strong>Square</strong></td>
<td style="width: 20%; height: 29px;">1</td>
<td style="width: 20%; height: 29px;">4</td>
<td style="width: 20%; height: 29px;">9</td>
<td style="width: 20%; height: 29px;">16</td>
</tr>
</tbody>
</table>
34 changes: 34 additions & 0 deletions src/tests/fixtures/tables/handbook.out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<table>
<thead>
<tr>
<th><strong>Heading 0</strong></th>
<th><strong>Heading 1</strong></th>
<th><strong>Heading 2</strong></th>
<th><strong>Heading 3</strong></th>
<th><strong>Heading 4</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Letter</strong></td>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
<tr>
<td><strong>Number</strong></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td><strong>Square</strong></td>
<td>1</td>
<td>4</td>
<td>9</td>
<td>16</td>
</tr>
</tbody>
</table>
Loading

0 comments on commit 0128849

Please sign in to comment.