Skip to content

Commit

Permalink
fix: keep tables separate in markdown
Browse files Browse the repository at this point in the history
Also make sure that table rows are rendered on one line each.

Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud committed Mar 15, 2022
1 parent 4beea0b commit 3b1b2f5
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cypress/fixtures/Table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## Preserve Tables

This is a table

| Header | other Header |
|--------|--------------|
| Cell | other cell |
| Cell | other cell |

---

This is a table

| Header | other Header |
|--------|--------------|
| Cell | other cell |
| Cell | other cell |

## Create a table

insertTable

---

| | | |
|--|--|--|
| | | |
| | | |

did insertTable

## Create second tables

| | | |
|--|--|--|
| | | |
| | | |

insertTable

---

| | | |
|--|--|--|
| | | |
| | | |

| | | |
|--|--|--|
| | | |
| | | |

did insertTable

76 changes: 76 additions & 0 deletions cypress/integration/Table.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import Table from './../../src/nodes/Table'
import TableBody from './../../src/nodes/TableBody'
import TableCell from './../../src/nodes/TableCell'
import TableHead from './../../src/nodes/TableHead'
import TableHeader from './../../src/nodes/TableHeader'
import TableRow from './../../src/nodes/TableRow'
import Markdown from './../../src/extensions/Markdown'
import markdownit from './../../src/markdownit'
import { createMarkdownSerializer } from './../../src/extensions/Markdown';
import { findChildren, findChildrenByType } from 'prosemirror-utils'
import createEditor from './../../src/tests/createEditor'
import testData from '../fixtures/Table.md'

describe('ListItem extension integrated in the editor', () => {

const editor = createEditor({
content: '',
extensions: [
Markdown,
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
],
})

for (const spec of testData.split(/#+\s+/)){
const [description, ...rest] = spec.split(/\n/)
const [input, output] = rest.join('\n').split(/\n\n---\n\n/)
if (!description) {
continue
}
it(description, () => {
expect(spec).to.include('\n')
expect(input).to.be.ok
expect(output).to.be.ok
loadMarkdown(input)
runCommands()
expectMarkdown(output.replace(/\n*$/, ''))
})
}

function loadMarkdown(markdown) {
editor.commands.setContent(markdownit.render(markdown))
}

function runCommands() {
let found
while (found = findCommand()) {
const name = found.node.text
editor.commands.setTextSelection(found.pos)
editor.commands[name]()
const updated = findCommand()
editor.commands.setTextSelection(updated.pos)
editor.commands.insertContent('did ')
}
}

function findCommand() {
const doc = editor.state.doc
return findChildren(doc, child => {
return child.isText && editor.commands.hasOwnProperty(child.text)
})[0]
}

function expectMarkdown(markdown) {
expect(getMarkdown().replace(/\n$/, '')).to.equal(markdown)
}

function getMarkdown() {
const serializer = createMarkdownSerializer(editor.schema)
return serializer.serialize(editor.state.doc)
}
})
1 change: 1 addition & 0 deletions src/nodes/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export default Table.extend({

toMarkdown(state, node) {
state.renderContent(node)
state.closeBlock(node)
},

})
1 change: 1 addition & 0 deletions src/nodes/TableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default TableRow.extend({
toMarkdown(state, node) {
state.write('|')
state.renderInline(node)
state.ensureNewLine()
},

parseHTML() {
Expand Down

0 comments on commit 3b1b2f5

Please sign in to comment.