Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax <max@nextcloud.com>2022-03-10 11:31:57 +0300
committerMax <max@nextcloud.com>2022-03-31 15:29:20 +0300
commita62606ce6af09fb88fc18866864dc73035f108f3 (patch)
tree49a3a8cba4dea72f8f808b674f01afc6a016896e /src
parent4629457932accaa24abefc9a8f444dc9398a94d5 (diff)
refactor: render Markdown in table nodes
Each node is responsible for rendering its markdown content. This splits the responsibility and simplifies the `toMarkdown` functions a lot. At the same time it makes it harder to beautify the entire table because every cell only knows about itself - not about the rest of the column. To work around this we can introduce padding attributes for TableCell and TableHeader. These need to be updated when the content of the table changes. They will also allow us to preserve the original padding from a markdown file. Signed-off-by: Max <max@nextcloud.com>
Diffstat (limited to 'src')
-rw-r--r--src/nodes/Table.js28
-rw-r--r--src/nodes/TableBody.js4
-rw-r--r--src/nodes/TableCell.js8
-rw-r--r--src/nodes/TableHead.js17
-rw-r--r--src/nodes/TableHeader.js8
-rw-r--r--src/nodes/TableRow.js6
-rw-r--r--src/tests/fixtures/table.md6
7 files changed, 47 insertions, 30 deletions
diff --git a/src/nodes/Table.js b/src/nodes/Table.js
index 12bdf6486..02441aacf 100644
--- a/src/nodes/Table.js
+++ b/src/nodes/Table.js
@@ -9,33 +9,7 @@ export default Table.extend({
},
toMarkdown(state, node) {
- const widths = []
- const head = node.child(0)
- const body = node.child(1)
- head.forEach(row => {
- state.write('|')
- row.forEach(cell => {
- widths.push(cell.textContent.length)
- state.renderInline(cell)
- state.write('|')
- })
- })
- state.ensureNewLine()
- state.write('|')
- widths.forEach(width => {
- state.write(state.repeat('-', width))
- state.write('|')
- })
- body.forEach(row => {
- state.ensureNewLine()
- state.write('|')
- row.forEach((cell, _, i) => {
- state.renderInline(cell)
- state.write(state.repeat(' ', widths[i] - cell.textContent.length))
- state.write('|')
- })
- })
- state.closeBlock(node)
+ state.renderContent(node)
},
})
diff --git a/src/nodes/TableBody.js b/src/nodes/TableBody.js
index fa3cc83ba..bf0e9b616 100644
--- a/src/nodes/TableBody.js
+++ b/src/nodes/TableBody.js
@@ -20,4 +20,8 @@ export default Node.create({
return ['tbody', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
+ toMarkdown(state, node) {
+ state.renderContent(node)
+ },
+
})
diff --git a/src/nodes/TableCell.js b/src/nodes/TableCell.js
index 606f2b428..b3829640f 100644
--- a/src/nodes/TableCell.js
+++ b/src/nodes/TableCell.js
@@ -2,7 +2,15 @@ import { TableCell } from '@tiptap/extension-table-cell'
export default TableCell.extend({
content: 'inline*',
+
addAttributes() {
return {}
},
+
+ toMarkdown(state, node) {
+ state.write(' ')
+ state.renderInline(node)
+ state.write(' |')
+ },
+
})
diff --git a/src/nodes/TableHead.js b/src/nodes/TableHead.js
index 6e45a3095..9fe6c94da 100644
--- a/src/nodes/TableHead.js
+++ b/src/nodes/TableHead.js
@@ -20,6 +20,12 @@ const tableHeadRow = Node.create({
return ['tr', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
+ toMarkdown(state, node) {
+ state.write('|')
+ state.renderInline(node)
+ state.ensureNewLine()
+ },
+
})
export default Node.create({
@@ -46,4 +52,15 @@ export default Node.create({
return ['thead', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
+ toMarkdown(state, node) {
+ state.renderContent(node)
+ const row = node.child(0)
+ state.write('|')
+ row.forEach(cell => {
+ state.write(state.repeat('-', cell.textContent.length + 2))
+ state.write('|')
+ })
+ state.ensureNewLine()
+ },
+
})
diff --git a/src/nodes/TableHeader.js b/src/nodes/TableHeader.js
index b50603bca..936ab26f2 100644
--- a/src/nodes/TableHeader.js
+++ b/src/nodes/TableHeader.js
@@ -2,7 +2,15 @@ import { TableHeader } from '@tiptap/extension-table-header'
export default TableHeader.extend({
content: 'inline*',
+
addAttributes() {
return {}
},
+
+ toMarkdown(state, node) {
+ state.write(' ')
+ state.renderInline(node)
+ state.write(' |')
+ },
+
})
diff --git a/src/nodes/TableRow.js b/src/nodes/TableRow.js
index c42aeee34..915cae27b 100644
--- a/src/nodes/TableRow.js
+++ b/src/nodes/TableRow.js
@@ -5,4 +5,10 @@ export default TableRow.extend({
addAttributes() {
return {}
},
+
+ toMarkdown(state, node) {
+ state.write('|')
+ state.renderInline(node)
+ },
+
})
diff --git a/src/tests/fixtures/table.md b/src/tests/fixtures/table.md
index 1e7479f58..76b83ce8f 100644
--- a/src/tests/fixtures/table.md
+++ b/src/tests/fixtures/table.md
@@ -1,3 +1,3 @@
-|heading|other heading|
-|-------|-------------|
-|cell |other cell |
+| heading | other heading |
+|---------|---------------|
+| cell | other cell |