From 2f46f45bdbc525ac6c5d23917999c3017e6e387d Mon Sep 17 00:00:00 2001 From: ginnyTheCat Date: Sun, 2 Oct 2022 22:59:27 +0200 Subject: [PATCH] Add table tests --- src/util/markdown.js | 35 +++++++++++++++++++++++++++++++---- src/util/markdown.test.js | 31 +++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/util/markdown.js b/src/util/markdown.js index 25987689..6b86c281 100644 --- a/src/util/markdown.js +++ b/src/util/markdown.js @@ -199,7 +199,7 @@ const markdownRules = { return s; })); - function pad(s, i) { + const pad = (s, i) => { switch (node.align[i]) { case 'right': return s.padStart(colWidth[i]); @@ -210,7 +210,7 @@ const markdownRules = { default: return s.padEnd(colWidth[i]); } - } + }; const line = colWidth.map((len, i) => { switch (node.align[i]) { @@ -232,6 +232,31 @@ const markdownRules = { return table.map((row) => `| ${row.join(' | ')} |\n`).join(''); }, + html: (node, output, state) => { + const header = node.header + .map((content, i) => htmlTag('th', output(content, state), { + scope: 'col', + align: node.align[i] || undefined, + })) + .join(''); + + const rows = node.cells + .map((row) => { + const cols = row + .map((content, i) => htmlTag('td', output(content, state), { + align: node.align[i] || undefined, + })) + .join(''); + + return htmlTag('tr', cols); + }) + .join(''); + + const thead = htmlTag('thead', htmlTag('tr', header)); + const tbody = htmlTag('tbody', rows); + + return htmlTag('table', thead + tbody); + }, }, displayMath: { order: defaultRules.table.order + 0.1, @@ -383,14 +408,16 @@ function mapElement(el) { items: Array.from(el.childNodes).map(mapNode), }]; case 'TABLE': { + const parseAlign = (s) => (['left', 'right', 'center'].includes(s) ? s : null); + const headerEl = Array.from(el.querySelector('thead > tr').childNodes); - const align = headerEl.map((childE) => childE.style['text-align']); + const align = headerEl.map((childE) => parseAlign(childE.align)); return [{ type: 'table', header: headerEl.map(mapChildren), align, cells: Array.from(el.querySelectorAll('tbody > tr')).map((rowEl) => Array.from(rowEl.childNodes).map((childEl, i) => { - if (align[i] === undefined) align[i] = childEl.style['text-align']; + if (align[i] === undefined) align[i] = parseAlign(childEl.align); return mapChildren(childEl); })), }]; diff --git a/src/util/markdown.test.js b/src/util/markdown.test.js index 90bb596c..2012a432 100644 --- a/src/util/markdown.test.js +++ b/src/util/markdown.test.js @@ -89,7 +89,11 @@ describe('link', () => { // mdTest('>quote', '> quote', '
quote
'); // mdTest('> quote', '> quote', '
quote
'); -// mdTest('> multiline\nquote', '> multiline\n> quote', '
multiline
quote
'); +// mdTest( +// '> multiline\nquote', +// '> multiline\n> quote', +// '
multiline
quote
', +// ); // mdTest('> quote\n\ntext after', '
quote
text after'); // }); @@ -101,7 +105,10 @@ describe('list', () => { mdTest('1. item1\n2. item2', '
  1. item1
  2. item2
'); mdTest('2. item2\n3. item3', '
  1. item2
  2. item3
'); - // mdTest('* item1\n * subitem1\n * subitem2\n* item2', ''); + // mdTest( + // '* item1\n * subitem1\n * subitem2\n* item2', + // '', + // ); const elementHtml = ''; test(elementHtml, () => { @@ -109,3 +116,23 @@ describe('list', () => { expect(content.plain).toBe('* item1\n * subitem1\n * subitem2\n* item2'); }); }); + +describe('table', () => { + mdTest( + '|head1|head2|\n|-|-|\n|cell1|cell2|\n|cell3|cell4|', + '| head1 | head2 |\n| ----- | ----- |\n| cell1 | cell2 |\n| cell3 | cell4 |', + '
head1head2
cell1cell2
cell3cell4
', + ); + + mdTest( + '| left | center | right |\n| :--- | :----: | ----: |\n| l | c | r |', + '
leftcenterright
lcr
', + ); + + const unknownAlignHtml = '
head
cell
'; + test(unknownAlignHtml, () => { + const content = html(unknownAlignHtml, { kind: 'edit' }); + expect(content.plain).toBe('| head |\n| ---- |\n| cell |'); + expect(content.html).toBe('
head
cell
'); + }); +});