diff --git a/packages/markdown-common/lib/CommonMarkUtils.js b/packages/markdown-common/lib/CommonMarkUtils.js index 9ba772d8..1e508013 100644 --- a/packages/markdown-common/lib/CommonMarkUtils.js +++ b/packages/markdown-common/lib/CommonMarkUtils.js @@ -173,6 +173,15 @@ function mkATXHeading(level) { return Array(level).fill('#').join(''); } +/** + * Create table heading + * @param {number} col - the number of columns + * @return {string} the markup for the table heading + */ +function mkTableHeading(col) { + return Array(col).fill('|---------').join('') + '|'; +} + /** * Adding escapes for text nodes * @param {string} input - unescaped @@ -334,6 +343,7 @@ module.exports.mkNewLine = mkNewLine; module.exports.mkPrefix = mkPrefix; module.exports.mkSetextHeading = mkSetextHeading; module.exports.mkATXHeading = mkATXHeading; +module.exports.mkTableHeading = mkTableHeading; module.exports.escapeText = escapeText; module.exports.escapeCodeBlock = escapeCodeBlock; diff --git a/packages/markdown-common/lib/__snapshots__/CommonMarkTransformer.test.js.snap b/packages/markdown-common/lib/__snapshots__/CommonMarkTransformer.test.js.snap index 8631d0d3..be674f2b 100644 --- a/packages/markdown-common/lib/__snapshots__/CommonMarkTransformer.test.js.snap +++ b/packages/markdown-common/lib/__snapshots__/CommonMarkTransformer.test.js.snap @@ -3192,6 +3192,588 @@ Object { } `; +exports[`markdown converts table.md to concerto JSON 1`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Header1", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Header 2", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Simple", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Table", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + +exports[`markdown converts table_bold.md to concerto JSON 1`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Syntax", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Description", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Strong", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Bold content", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Title", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + +exports[`markdown converts table_code.md to concerto JSON 1`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column1", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 2", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Code", + "text": "code block", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Table", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + +exports[`markdown converts table_image.md to concerto JSON 1`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Syntax", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Description", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Strong", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Bold content", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Image", + "destination": "https://docs.accordproject.org/docs/assets/020/template.png", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "ap_logo", + }, + ], + "title": "AP triangle", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + +exports[`markdown converts table_links.md to concerto JSON 1`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column1", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 2", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Link", + "destination": "http://clause.io", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "link", + }, + ], + "title": "", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Table", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + +exports[`markdown converts table_skewed.md to concerto JSON 1`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Header1", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Header 2", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Simple", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Table", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + exports[`markdown converts text.md to concerto JSON 1`] = ` Object { "$class": "org.accordproject.commonmark.Document", @@ -4165,6 +4747,48 @@ This is second paragraph." exports[`to plain text strong.md 1`] = `"This is some text."`; +exports[`to plain text table.md 1`] = ` +"| Header1 | Header 2 | +|---------|---------| +| Simple | Table | +| Paragraph | Text |" +`; + +exports[`to plain text table_bold.md 1`] = ` +"| Syntax | Description | +|---------|---------| +| **Bold content** | Title | +| Paragraph | Text |" +`; + +exports[`to plain text table_code.md 1`] = ` +"| Column1 | Column 2 | +|---------|---------| +| \`code block\` | Table | +| Paragraph | Text |" +`; + +exports[`to plain text table_image.md 1`] = ` +"| Syntax | Description | +|---------|---------| +| **Bold content** | ![ap_logo](https://docs.accordproject.org/docs/assets/020/template.png \\"AP triangle\\") | +| Paragraph | Text |" +`; + +exports[`to plain text table_links.md 1`] = ` +"| Column1 | Column 2 | +|---------|---------| +| [link](http://clause.io \\"\\") | Table | +| Paragraph | Text |" +`; + +exports[`to plain text table_skewed.md 1`] = ` +"| Header1 | Header 2 | +|---------|---------| +| Simple | Table | +| Paragraph | Text |" +`; + exports[`to plain text text.md 1`] = `"This is some text."`; exports[`to plain text text2.md 1`] = ` diff --git a/packages/markdown-common/lib/fromcommonmarkrules.js b/packages/markdown-common/lib/fromcommonmarkrules.js index 86dad41c..2c3de6de 100644 --- a/packages/markdown-common/lib/fromcommonmarkrules.js +++ b/packages/markdown-common/lib/fromcommonmarkrules.js @@ -149,4 +149,43 @@ rules.Document = (visitor,thing,children,parameters,resultString,resultSeq) => { resultSeq(parameters,result); }; +rules.Table = (visitor,thing,children,parameters,resultString,resultSeq) => { + const result = [children]; + resultSeq(parameters,result); +}; + +rules.TableBody = (visitor,thing,children,parameters,resultString,resultSeq) => { + const result = [children]; + resultSeq(parameters,result); +}; + +rules.TableRow = (visitor,thing,children,parameters,resultString,resultSeq) => { + const next1 = '|'; + const newLine = CommonMarkUtils.mkNewLine(parameters); + const result = [children, resultString(next1), newLine]; + resultSeq(parameters,result); +}; + +rules.TableCell = (visitor,thing,children,parameters,resultString,resultSeq) => { + const next1 = '|'; + const next2 = ' '; + const result = [resultString(next1),resultString(next2),children, resultString(next2)]; + resultSeq(parameters,result); +}; + +rules.TableHead = (visitor,thing,children,parameters,resultString,resultSeq) => { + const col = thing.nodes[0].nodes.length; + const next1 = CommonMarkUtils.mkTableHeading(col); + const newLine = CommonMarkUtils.mkNewLine(parameters); + const result = [children, resultString(next1), resultString(newLine)]; + resultSeq(parameters,result); +}; + +rules.HeaderCell = (visitor,thing,children,parameters,resultString,resultSeq) => { + const next1 = '|'; + const next2 = ' '; + const result = [resultString(next1),resultString(next2),children, resultString(next2)]; + resultSeq(parameters,result); +}; + module.exports = rules; \ No newline at end of file diff --git a/packages/markdown-common/lib/tocommonmarkrules.js b/packages/markdown-common/lib/tocommonmarkrules.js index ccd6ef48..b849491b 100644 --- a/packages/markdown-common/lib/tocommonmarkrules.js +++ b/packages/markdown-common/lib/tocommonmarkrules.js @@ -243,6 +243,96 @@ const listItemCloseRule = { close: true, }; +const tableOpenRule = { + tag: NS_PREFIX_CommonMarkModel + 'Table', + leaf: false, + open: true, + close: false, + enter: (node, token, callback) => {}, +}; + +const tableCloseRule = { + tag: NS_PREFIX_CommonMarkModel + 'Table', + leaf: false, + open: false, + close: true, +}; + +const tableHeadOpenRule = { + tag: NS_PREFIX_CommonMarkModel + 'TableHead', + leaf: false, + open: true, + close: false, + enter: (node, token, callback) => {}, +}; + +const tableHeadCloseRule = { + tag: NS_PREFIX_CommonMarkModel + 'TableHead', + leaf: false, + open: false, + close: true, +}; + +const tableBodyOpenRule = { + tag: NS_PREFIX_CommonMarkModel + 'TableBody', + leaf: false, + open: true, + close: false, + enter: (node, token, callback) => {}, +}; + +const tableBodyCloseRule = { + tag: NS_PREFIX_CommonMarkModel + 'TableBody', + leaf: false, + open: false, + close: true, +}; + +const tableRowOpenRule = { + tag: NS_PREFIX_CommonMarkModel + 'TableRow', + leaf: false, + open: true, + close: false, + enter: (node, token, callback) => {}, +}; + +const tableRowCloseRule = { + tag: NS_PREFIX_CommonMarkModel + 'TableRow', + leaf: false, + open: false, + close: true, +}; + +const headerCellOpenRule = { + tag: NS_PREFIX_CommonMarkModel + 'HeaderCell', + leaf: false, + open: true, + close: false, + enter: (node, token, callback) => {}, +}; + +const headerCellCloseRule = { + tag: NS_PREFIX_CommonMarkModel + 'HeaderCell', + leaf: false, + open: false, + close: true, +}; + +const tableCellOpenRule = { + tag: NS_PREFIX_CommonMarkModel + 'TableCell', + leaf: false, + open: true, + close: false, + enter: (node, token, callback) => {}, +}; + +const tableCellCloseRule = { + tag: NS_PREFIX_CommonMarkModel + 'TableCell', + leaf: false, + open: false, + close: true, +}; + const rules = { inlines: {}, blocks: {}}; rules.inlines.text = textRule; rules.inlines.code_inline = codeInlineRule; @@ -273,5 +363,17 @@ rules.blocks.ordered_list_open = orderedListOpenRule; rules.blocks.ordered_list_close = orderedListCloseRule; rules.blocks.list_item_open = listItemOpenRule; rules.blocks.list_item_close = listItemCloseRule; +rules.blocks.table_open = tableOpenRule; +rules.blocks.table_close = tableCloseRule; +rules.blocks.thead_open = tableHeadOpenRule; +rules.blocks.thead_close = tableHeadCloseRule; +rules.blocks.tbody_open = tableBodyOpenRule; +rules.blocks.tbody_close = tableBodyCloseRule; +rules.blocks.tr_open = tableRowOpenRule; +rules.blocks.tr_close = tableRowCloseRule; +rules.blocks.th_open = headerCellOpenRule; +rules.blocks.th_close = headerCellCloseRule; +rules.blocks.td_open = tableCellOpenRule; +rules.blocks.td_close = tableCellCloseRule; module.exports = rules; diff --git a/packages/markdown-common/test/data/table.md b/packages/markdown-common/test/data/table.md new file mode 100644 index 00000000..bfb96236 --- /dev/null +++ b/packages/markdown-common/test/data/table.md @@ -0,0 +1,4 @@ +| Header1 | Header 2 | +| ----------- | ----------- | +| Simple | Table | +| Paragraph | Text | \ No newline at end of file diff --git a/packages/markdown-common/test/data/table_bold.md b/packages/markdown-common/test/data/table_bold.md new file mode 100644 index 00000000..cf4d1b7c --- /dev/null +++ b/packages/markdown-common/test/data/table_bold.md @@ -0,0 +1,4 @@ +| Syntax | Description | +| ---------------- | ----------- | +| **Bold content** | Title | +| Paragraph | Text | \ No newline at end of file diff --git a/packages/markdown-common/test/data/table_code.md b/packages/markdown-common/test/data/table_code.md new file mode 100644 index 00000000..ac2e66bf --- /dev/null +++ b/packages/markdown-common/test/data/table_code.md @@ -0,0 +1,4 @@ +| Column1 | Column 2 | +| ----------- | ----------- | +| `code block` | Table | +| Paragraph | Text | \ No newline at end of file diff --git a/packages/markdown-common/test/data/table_image.md b/packages/markdown-common/test/data/table_image.md new file mode 100644 index 00000000..a0e8286b --- /dev/null +++ b/packages/markdown-common/test/data/table_image.md @@ -0,0 +1,4 @@ +| Syntax | Description | +| ---------------- | ----------- | +| **Bold content** | ![ap_logo](https://docs.accordproject.org/docs/assets/020/template.png "AP triangle") | +| Paragraph | Text | \ No newline at end of file diff --git a/packages/markdown-common/test/data/table_links.md b/packages/markdown-common/test/data/table_links.md new file mode 100644 index 00000000..f7aa7cd5 --- /dev/null +++ b/packages/markdown-common/test/data/table_links.md @@ -0,0 +1,4 @@ +| Column1 | Column 2 | +| ----------- | ----------- | +| [link](http://clause.io) | Table | +| Paragraph | Text | \ No newline at end of file diff --git a/packages/markdown-common/test/data/table_skewed.md b/packages/markdown-common/test/data/table_skewed.md new file mode 100644 index 00000000..a7262575 --- /dev/null +++ b/packages/markdown-common/test/data/table_skewed.md @@ -0,0 +1,4 @@ +| Header1 | Header 2 | +| ----------- | ----------- | +| Simple | Table | +| Paragraph | Text | \ No newline at end of file diff --git a/packages/markdown-html/src/ToHtmlStringVisitor.js b/packages/markdown-html/src/ToHtmlStringVisitor.js index 2bf4b2c4..b05dc143 100644 --- a/packages/markdown-html/src/ToHtmlStringVisitor.js +++ b/packages/markdown-html/src/ToHtmlStringVisitor.js @@ -205,6 +205,32 @@ class ToHtmlStringVisitor { case 'Item': parameters.result += `
  • ${ToHtmlStringVisitor.visitChildren(this, thing)}
  • \n`; break; + + case 'Table': + parameters.result += '\n'; + parameters.result += `${ToHtmlStringVisitor.visitChildren(this, thing)}\n
    \n`; + break; + + case 'TableHead': + parameters.result += `\n${ToHtmlStringVisitor.visitChildren(this,thing)}\n`; + break; + + case 'TableBody': + parameters.result += `\n${ToHtmlStringVisitor.visitChildren(this,thing)}\n`; + break; + + case 'TableRow': + parameters.result += `\n\n${ToHtmlStringVisitor.visitChildren(this,thing)}`; + break; + + case 'TableCell': + parameters.result += `${ToHtmlStringVisitor.visitChildren(this,thing)}\n`; + break; + + case 'HeaderCell': + parameters.result += `${ToHtmlStringVisitor.visitChildren(this,thing)}\n`; + break; + case 'Document': parameters.result += `\n\n\n
    \n${ToHtmlStringVisitor.visitChildren(this, thing)}
    \n\n`; break; diff --git a/packages/markdown-html/src/__snapshots__/HtmlTransformer.test.js.snap b/packages/markdown-html/src/__snapshots__/HtmlTransformer.test.js.snap index c2914a2f..9c0cd18f 100644 --- a/packages/markdown-html/src/__snapshots__/HtmlTransformer.test.js.snap +++ b/packages/markdown-html/src/__snapshots__/HtmlTransformer.test.js.snap @@ -1712,6 +1712,296 @@ exports[`markdown <-> html converts strong.md to html 2`] = ` " `; +exports[`markdown <-> html converts table.md to html 1`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Header1", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Header 2", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Simple", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Table", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + +exports[`markdown <-> html converts table.md to html 2`] = ` +" + + +
    + + + + + + + + + + + + + + + + + + +
    Header1Header 2
    SimpleTable
    ParagraphText
    +
    + +" +`; + +exports[`markdown <-> html converts table_complex.md to html 1`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column1", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 2", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Code", + "text": "code block", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Image", + "destination": "https://docs.accordproject.org/docs/assets/020/template.png", + "nodes": Array [], + "title": "AP triangle", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Strong", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Bold content", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Link", + "destination": "http://clause.io", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "link", + }, + ], + "title": "", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Emph", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Italics", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + +exports[`markdown <-> html converts table_complex.md to html 2`] = ` +" + + +
    + + + + + + + + + + + + + + + + + + + + + + +
    Column1Column 2
    code block
    ParagraphBold content
    linkItalics
    +
    + +" +`; + exports[`markdown <-> html converts text.md to html 1`] = ` Object { "$class": "org.accordproject.commonmark.Document", diff --git a/packages/markdown-html/src/rules.js b/packages/markdown-html/src/rules.js index 60caef1f..589cfe83 100644 --- a/packages/markdown-html/src/rules.js +++ b/packages/markdown-html/src/rules.js @@ -500,6 +500,47 @@ const HTML_BLOCK_RULE = { } }; +const TABLE_RULE = { + deserialize(el, next, ignoreSpace) { + if (el.tagName && el.tagName.toLowerCase() === 'table') { + return { + $class: `${NS_PREFIX_CommonMarkModel}Table`, + nodes: next(el.childNodes), + }; + } + if (el.tagName && el.tagName.toLowerCase() === 'thead') { + return { + $class: `${NS_PREFIX_CommonMarkModel}TableHead`, + nodes: next(el.childNodes), + }; + } + if (el.tagName && el.tagName.toLowerCase() === 'tbody') { + return { + $class: `${NS_PREFIX_CommonMarkModel}TableBody`, + nodes: next(el.childNodes), + }; + } + if (el.tagName && el.tagName.toLowerCase() === 'tr') { + return { + $class: `${NS_PREFIX_CommonMarkModel}TableRow`, + nodes: next(el.childNodes), + }; + } + if (el.tagName && el.tagName.toLowerCase() === 'th') { + return { + $class: `${NS_PREFIX_CommonMarkModel}HeaderCell`, + nodes: next(el.childNodes), + }; + } + if (el.tagName && el.tagName.toLowerCase() === 'td') { + return { + $class: `${NS_PREFIX_CommonMarkModel}TableCell`, + nodes: next(el.childNodes), + }; + } + }, +}; + const rules = [ LIST_RULE, PARAGRAPH_RULE, @@ -520,7 +561,8 @@ const rules = [ TEXT_RULE, HTML_INLINE_RULE, HTML_BLOCK_RULE, - IMAGE_RULE + IMAGE_RULE, + TABLE_RULE ]; diff --git a/packages/markdown-html/test/data/markdown/table.md b/packages/markdown-html/test/data/markdown/table.md new file mode 100644 index 00000000..bfb96236 --- /dev/null +++ b/packages/markdown-html/test/data/markdown/table.md @@ -0,0 +1,4 @@ +| Header1 | Header 2 | +| ----------- | ----------- | +| Simple | Table | +| Paragraph | Text | \ No newline at end of file diff --git a/packages/markdown-html/test/data/markdown/table_complex.md b/packages/markdown-html/test/data/markdown/table_complex.md new file mode 100644 index 00000000..67896fb5 --- /dev/null +++ b/packages/markdown-html/test/data/markdown/table_complex.md @@ -0,0 +1,5 @@ +| Column1 | Column 2 | +| ----------- | ----------- | +| `code block` | ![](https://docs.accordproject.org/docs/assets/020/template.png "AP triangle") | +| Paragraph | **Bold content** | +| [link](http://clause.io) | *Italics* | \ No newline at end of file diff --git a/packages/markdown-pdf/src/commonmarkrules.js b/packages/markdown-pdf/src/commonmarkrules.js index 246bc387..019aeb93 100644 --- a/packages/markdown-pdf/src/commonmarkrules.js +++ b/packages/markdown-pdf/src/commonmarkrules.js @@ -102,6 +102,24 @@ rules.ListBlock = (visitor, thing, children, parameters) => { rules.List = (visitor, thing, children, parameters) => { parameters.result[thing.type === 'ordered' ? 'ol' : 'ul'] = children; }; +rules.Table = (visitor, thing, children, parameters) => { + parameters.result.table = {'body': children}; +}; +rules.TableHead = (visitor, thing, children, parameters) => { + parameters.result = children; +}; +rules.TableBody = (visitor, thing, children, parameters) => { + parameters.result = children; +}; +rules.TableRow = (visitor, thing, children, parameters) => { + parameters.result = [children]; +}; +rules.HeaderCell = (visitor, thing, children, parameters) => { + parameters.result.stack = children; +}; +rules.TableCell = (visitor, thing, children, parameters) => { + parameters.result.stack = children; +}; rules.Document = (visitor, thing, children, parameters) => { parameters.result.content = children; }; diff --git a/packages/markdown-pdf/src/pdfmakeutil.js b/packages/markdown-pdf/src/pdfmakeutil.js index 4dd359d9..e5b22563 100644 --- a/packages/markdown-pdf/src/pdfmakeutil.js +++ b/packages/markdown-pdf/src/pdfmakeutil.js @@ -135,6 +135,11 @@ const defaultStyles = { }, background: { color: 'white' + }, + HeaderCell: { + fontSize: 13, + bold: true, + color: 'black' } }; diff --git a/packages/markdown-pdf/test/data/tables.md b/packages/markdown-pdf/test/data/tables.md new file mode 100644 index 00000000..5e56c198 --- /dev/null +++ b/packages/markdown-pdf/test/data/tables.md @@ -0,0 +1,14 @@ +This is a simple table: + +| Header1 | Header 2 | +| ----------- | ----------- | +| Simple | Table | +| Paragraph | Text | + +This is a complex table: + +| Column1 | Column 2 | +| ----------- | ----------- | +| `code block` | ![](https://docs.accordproject.org/docs/assets/020/template.png "AP triangle") | +| Paragraph | **Bold content** | +| [link](http://clause.io) | *Italics* | \ No newline at end of file diff --git a/packages/markdown-slate/lib/__snapshots__/SlateTransformer.test.js.snap b/packages/markdown-slate/lib/__snapshots__/SlateTransformer.test.js.snap index 27ac19d9..f6d0dd0e 100644 --- a/packages/markdown-slate/lib/__snapshots__/SlateTransformer.test.js.snap +++ b/packages/markdown-slate/lib/__snapshots__/SlateTransformer.test.js.snap @@ -10127,6 +10127,785 @@ Object { } `; +exports[`markdown <-> slate converts table.json to and from Markdown 1`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 1", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 2", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Simple", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Table", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + +exports[`markdown <-> slate converts table.json to and from Markdown 2`] = ` +"| Column 1 | Column 2 | +| ----------- | ----------- | +| Simple | Table | +| Paragraph | Text |" +`; + +exports[`markdown <-> slate converts table.json to and from Markdown 3`] = ` +Object { + "document": Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "object": "text", + "text": "Column 1", + }, + ], + "data": Object {}, + "object": "block", + "type": "header_cell", + }, + Object { + "children": Array [ + Object { + "object": "text", + "text": "Column 2", + }, + ], + "data": Object {}, + "object": "block", + "type": "header_cell", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_row", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_head", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "object": "text", + "text": "Simple", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_cell", + }, + Object { + "children": Array [ + Object { + "object": "text", + "text": "Table", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_cell", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_row", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "object": "text", + "text": "Paragraph", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_cell", + }, + Object { + "children": Array [ + Object { + "object": "text", + "text": "Text", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_cell", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_row", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_body", + }, + ], + "data": Object {}, + "object": "block", + "type": "table", + }, + ], + "data": Object {}, + "object": "document", + }, +} +`; + +exports[`markdown <-> slate converts table.json to and from Markdown 4`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 1", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 2", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Simple", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Table", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Text", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + +exports[`markdown <-> slate converts table_complex.json to and from Markdown 1`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 1", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 2", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Code", + "text": "code block", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Image", + "destination": "https://docs.accordproject.org/docs/assets/020/template.png", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "", + }, + ], + "title": "AP triangle", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Strong", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Bold content", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Link", + "destination": "http://clause.io", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "link", + }, + ], + "title": "", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Emph", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Italics", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + +exports[`markdown <-> slate converts table_complex.json to and from Markdown 2`] = ` +"| Column 1 | Column 2 | +| ----------- | ----------- | +| \`code block\` | ![](https://docs.accordproject.org/docs/assets/020/template.png \\"AP triangle\\") | +| Paragraph | **Bold content** | +| [link](http://clause.io) | *Italics* |" +`; + +exports[`markdown <-> slate converts table_complex.json to and from Markdown 3`] = ` +Object { + "document": Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "object": "text", + "text": "Column 1", + }, + ], + "data": Object {}, + "object": "block", + "type": "header_cell", + }, + Object { + "children": Array [ + Object { + "object": "text", + "text": "Column 2", + }, + ], + "data": Object {}, + "object": "block", + "type": "header_cell", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_row", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_head", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "code": true, + "object": "text", + "text": "code block", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_cell", + }, + Object { + "children": Array [ + Object { + "object": "text", + "text": "", + }, + Object { + "children": Array [ + Object { + "object": "text", + "text": "", + }, + ], + "data": Object { + "href": "https://docs.accordproject.org/docs/assets/020/template.png", + "title": "AP triangle", + }, + "object": "inline", + "type": "image", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_cell", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_row", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "object": "text", + "text": "Paragraph", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_cell", + }, + Object { + "children": Array [ + Object { + "bold": true, + "object": "text", + "text": "Bold content", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_cell", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_row", + }, + Object { + "children": Array [ + Object { + "children": Array [ + Object { + "object": "text", + "text": "", + }, + Object { + "children": Array [ + Object { + "object": "text", + "text": "link", + }, + ], + "data": Object { + "href": "http://clause.io", + "title": "", + }, + "object": "inline", + "type": "link", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_cell", + }, + Object { + "children": Array [ + Object { + "italic": true, + "object": "text", + "text": "Italics", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_cell", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_row", + }, + ], + "data": Object {}, + "object": "block", + "type": "table_body", + }, + ], + "data": Object {}, + "object": "block", + "type": "table", + }, + ], + "data": Object {}, + "object": "document", + }, +} +`; + +exports[`markdown <-> slate converts table_complex.json to and from Markdown 4`] = ` +Object { + "$class": "org.accordproject.commonmark.Document", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Table", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableHead", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 1", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.HeaderCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Column 2", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableBody", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Code", + "text": "code block", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Image", + "destination": "https://docs.accordproject.org/docs/assets/020/template.png", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "", + }, + ], + "title": "AP triangle", + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Paragraph", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Strong", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Bold content", + }, + ], + }, + ], + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableRow", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Link", + "destination": "http://clause.io", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "link", + }, + ], + "title": "", + }, + ], + }, + Object { + "$class": "org.accordproject.commonmark.TableCell", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Emph", + "nodes": Array [ + Object { + "$class": "org.accordproject.commonmark.Text", + "text": "Italics", + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + "xmlns": "http://commonmark.org/xml/1.0", +} +`; + exports[`markdown <-> slate converts text.json to and from Markdown 1`] = ` Object { "$class": "org.accordproject.commonmark.Document", diff --git a/packages/markdown-slate/lib/commonmarkfromslaterules.js b/packages/markdown-slate/lib/commonmarkfromslaterules.js index 91cf371e..649592e7 100644 --- a/packages/markdown-slate/lib/commonmarkfromslaterules.js +++ b/packages/markdown-slate/lib/commonmarkfromslaterules.js @@ -78,5 +78,23 @@ rules.ol_list = (node,processNodes) => { rules.ul_list = (node,processNodes) => { return {$class : `${NS_PREFIX_CommonMarkModel}List`, type: 'bullet', delimiter: node.data.delimiter, start: node.data.start, tight: node.data.tight, nodes: []}; }; +rules.table = (node, processNodes) => { + return { $class: `${NS_PREFIX_CommonMarkModel}Table`, nodes: [] }; +}; +rules.table_head = (node, processNodes) => { + return { $class: `${NS_PREFIX_CommonMarkModel}TableHead`, nodes: [] }; +}; +rules.table_body = (node, processNodes) => { + return { $class: `${NS_PREFIX_CommonMarkModel}TableBody`, nodes: [] }; +}; +rules.table_row = (node, processNodes) => { + return { $class: `${NS_PREFIX_CommonMarkModel}TableRow`, nodes: [] }; +}; +rules.table_cell = (node, processNodes) => { + return { $class: `${NS_PREFIX_CommonMarkModel}TableCell`, nodes: [] }; +}; +rules.header_cell = (node, processNodes) => { + return { $class: `${NS_PREFIX_CommonMarkModel}HeaderCell`, nodes: [] }; +}; module.exports = rules; \ No newline at end of file diff --git a/packages/markdown-slate/lib/commonmarktoslaterules.js b/packages/markdown-slate/lib/commonmarktoslaterules.js index f46da7b4..64bf6070 100644 --- a/packages/markdown-slate/lib/commonmarktoslaterules.js +++ b/packages/markdown-slate/lib/commonmarktoslaterules.js @@ -174,5 +174,58 @@ rules.Document = (thing,processChildren,parameters) => { data : {} }; }; +rules.Table = (thing, processChildren, parameters) => { + return { + object: 'block', + type: 'table', + data: {}, + children: processChildren(thing, 'nodes', parameters), + }; +}; + +rules.TableHead = (thing, processChildren, parameters) => { + return { + object: 'block', + type: 'table_head', + data: {}, + children: processChildren(thing, 'nodes', parameters), + }; +}; + +rules.TableBody = (thing, processChildren, parameters) => { + return { + object: 'block', + type: 'table_body', + data: {}, + children: processChildren(thing, 'nodes', parameters), + }; +}; + +rules.TableRow = (thing, processChildren, parameters) => { + return { + object: 'block', + type: 'table_row', + data: {}, + children: processChildren(thing, 'nodes', parameters), + }; +}; + +rules.TableCell = (thing, processChildren, parameters) => { + return { + object: 'block', + type: 'table_cell', + data: {}, + children: processChildren(thing, 'nodes', parameters), + }; +}; + +rules.HeaderCell = (thing, processChildren, parameters) => { + return { + object: 'block', + type: 'header_cell', + data: {}, + children: processChildren(thing, 'nodes', parameters), + }; +}; module.exports = rules; \ No newline at end of file diff --git a/packages/markdown-slate/test/data/markdown/table.json b/packages/markdown-slate/test/data/markdown/table.json new file mode 100644 index 00000000..519e4916 --- /dev/null +++ b/packages/markdown-slate/test/data/markdown/table.json @@ -0,0 +1,116 @@ +{ + "document": { + "object": "document", + "data": {}, + "children": [ + { + "object": "block", + "type": "table", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_head", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_row", + "data": {}, + "children": [ + { + "object": "block", + "type": "header_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Column 1" + } + ] + }, + { + "object": "block", + "type": "header_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Column 2" + } + ] + } + ] + } + ] + }, + { + "object": "block", + "type": "table_body", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_row", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Simple" + } + ] + }, + { + "object": "block", + "type": "table_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Table" + } + ] + } + ] + }, + { + "object": "block", + "type": "table_row", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Paragraph" + } + ] + }, + { + "object": "block", + "type": "table_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Text" + } + ] + } + ] + } + ] + } + ] + } + ] + } +} diff --git a/packages/markdown-slate/test/data/markdown/table.md b/packages/markdown-slate/test/data/markdown/table.md new file mode 100644 index 00000000..5512ab5a --- /dev/null +++ b/packages/markdown-slate/test/data/markdown/table.md @@ -0,0 +1,4 @@ +| Column 1 | Column 2 | +| ----------- | ----------- | +| Simple | Table | +| Paragraph | Text | \ No newline at end of file diff --git a/packages/markdown-slate/test/data/markdown/table_complex.json b/packages/markdown-slate/test/data/markdown/table_complex.json new file mode 100644 index 00000000..3d0c0801 --- /dev/null +++ b/packages/markdown-slate/test/data/markdown/table_complex.json @@ -0,0 +1,176 @@ +{ + "document": { + "object": "document", + "data": {}, + "children": [ + { + "object": "block", + "type": "table", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_head", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_row", + "data": {}, + "children": [ + { + "object": "block", + "type": "header_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Column 1" + } + ] + }, + { + "object": "block", + "type": "header_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Column 2" + } + ] + } + ] + } + ] + }, + { + "object": "block", + "type": "table_body", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_row", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "code block", + "code": true + } + ] + }, + { + "object": "block", + "type": "table_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "" + }, + { + "object": "inline", + "type": "image", + "data": { + "href": "https://docs.accordproject.org/docs/assets/020/template.png", + "title": "AP triangle" + }, + "children": [ + { + "object": "text", + "text": "" + } + ] + } + ] + } + ] + }, + { + "object": "block", + "type": "table_row", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Paragraph" + } + ] + }, + { + "object": "block", + "type": "table_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Bold content", + "bold": true + } + ] + } + ] + }, + { + "object": "block", + "type": "table_row", + "data": {}, + "children": [ + { + "object": "block", + "type": "table_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "" + }, + { + "object": "inline", + "type": "link", + "data": { + "href": "http://clause.io", + "title": "" + }, + "children": [ + { + "object": "text", + "text": "link" + } + ] + } + ] + }, + { + "object": "block", + "type": "table_cell", + "data": {}, + "children": [ + { + "object": "text", + "text": "Italics", + "italic": true + } + ] + } + ] + } + ] + } + ] + } + ] + } +} diff --git a/packages/markdown-slate/test/data/markdown/table_complex.md b/packages/markdown-slate/test/data/markdown/table_complex.md new file mode 100644 index 00000000..b2518947 --- /dev/null +++ b/packages/markdown-slate/test/data/markdown/table_complex.md @@ -0,0 +1,5 @@ +| Column 1 | Column 2 | +| ----------- | ----------- | +| `code block` | ![](https://docs.accordproject.org/docs/assets/020/template.png "AP triangle") | +| Paragraph | **Bold content** | +| [link](http://clause.io) | *Italics* | \ No newline at end of file diff --git a/scripts/external/models.json b/scripts/external/models.json index 8a21a78c..664009e6 100644 --- a/scripts/external/models.json +++ b/scripts/external/models.json @@ -3,7 +3,7 @@ "models": [ { "name": "CommonMarkModel", "namespace" : "org.accordproject.commonmark", - "from": "https://models.accordproject.org/markdown/commonmark@0.2.0.cto", + "from": "https://models.accordproject.org/markdown/commonmark@0.4.0.cto", "js": "packages/markdown-common/lib/externalModels" }, { "name": "ConcertoMetaModel", "namespace" : "concerto.metamodel", @@ -11,11 +11,11 @@ "js": "packages/markdown-cicero/lib/externalModels" }, { "name": "CiceroMarkModel", "namespace" : "org.accordproject.ciceromark", - "from": "https://models.accordproject.org/markdown/ciceromark@0.3.2.cto", + "from": "https://models.accordproject.org/markdown/ciceromark@0.3.3.cto", "js": "packages/markdown-cicero/lib/externalModels" }, { "name": "TemplateMarkModel", "namespace" : "org.accordproject.templatemark", - "from": "https://models.accordproject.org/markdown/templatemark@0.1.2.cto", + "from": "https://models.accordproject.org/markdown/templatemark@0.1.3.cto", "js": "packages/markdown-template/lib/externalModels" } ] } \ No newline at end of file