diff --git a/src/util/markdown.js b/src/util/markdown.js index 6b86c281..433b307a 100644 --- a/src/util/markdown.js +++ b/src/util/markdown.js @@ -89,10 +89,6 @@ const plainRules = { plain: (node, output, state) => `${output(node.content, state)}\n\n`, html: (node, output, state) => htmlTag('p', output(node.content, state)), }, - escape: { - ...defaultRules.escape, - plain: (node, output, state) => `\\${output(node.content, state)}`, - }, br: { ...defaultRules.br, match: anyScopeRegex(/^ *\n/), @@ -188,7 +184,7 @@ const markdownRules = { } }); header.forEach((s, i) => { - if (s.length > colWidth[i])colWidth[i] = s.length; + if (s.length > colWidth[i]) colWidth[i] = s.length; }); const cells = node.cells.map((row) => row.map((content, i) => { diff --git a/src/util/markdown.test.js b/src/util/markdown.test.js index 2012a432..a31cd5ba 100644 --- a/src/util/markdown.test.js +++ b/src/util/markdown.test.js @@ -3,22 +3,36 @@ import { html, markdown } from './markdown'; -function mdTest(source, plain, htmlStr) { +function mdTest(source, plain, htmlStr, state) { test(source, () => { + if (typeof htmlStr === 'object') { + state = htmlStr; + htmlStr = undefined; + } if (htmlStr === undefined) { htmlStr = plain; plain = source; } - const content = markdown(source, { kind: 'edit' }); + const content = markdown(source, { kind: 'edit', ...state }); expect(content.plain).toBe(plain); expect(content.html).toBe(htmlStr); - const htmlContent = html(htmlStr, { kind: 'edit', onlyPlain: true }); + const htmlContent = html(htmlStr, { kind: 'edit', onlyPlain: true, ...state }); expect(htmlContent.plain).toBe(plain); }); } +function htmlTest(source, plain, htmlStr) { + test(source, () => { + const content = html(source, { kind: 'edit', onlyPlain: htmlStr === undefined }); + expect(content.plain).toBe(plain); + if (htmlStr !== undefined) { + expect(content.html).toBe(htmlStr); + } + }); +} + describe('text', () => { mdTest('some text', 'some text'); @@ -28,6 +42,8 @@ describe('text', () => { // mdTest('¯\\_(ツ)_/¯', '¯\\_(ツ)_/¯'); mdTest('¯\\_(ツ)_/¯', '¯\\\\_(ツ)\\_/¯', '¯\\_(ツ)_/¯'); + + mdTest('\\*escape*', '\\*escape\\*', '*escape*'); }); describe('inline', () => { @@ -35,8 +51,29 @@ describe('inline', () => { mdTest('**bold**', 'bold'); mdTest('__underline__', 'underline'); mdTest('~~strikethrough~~', 'strikethrough'); - mdTest('||spoiler||', 'spoiler'); - mdTest('||spoiler||(reason)', 'spoiler'); +}); + +describe('spoiler', () => { + mdTest('||content||', 'content'); + mdTest('||content||(reason)', 'content'); + + let content = markdown('||content||', { kind: 'notification', onlyPlain: true }); + expect(content.plain).toBe(''); + + content = markdown('||content||(reason)', { kind: 'notification', onlyPlain: true }); + expect(content.plain).toBe(''); + + content = markdown('||content||', { onlyPlain: true }); + expect(content.plain).toBe('[spoiler](content)'); + + content = markdown('||content||(reason)', { onlyPlain: true }); + expect(content.plain).toBe('[spoiler: reason](content)'); +}); + +describe('hr', () => { + mdTest('---', '
'); + mdTest('***', '---', '
'); + mdTest('___', '---', '
'); }); describe('code', () => { @@ -68,6 +105,9 @@ describe('heading', () => { mdTest('sub-heading\n---', '## sub-heading', '

sub-heading

'); mdTest('###### small heading', '
small heading
'); + + mdTest('# heading', 'heading\n=======', '

heading

', { kind: '' }); + mdTest('heading\n=======', '

heading

', { kind: '' }); }); describe('link', () => { @@ -84,6 +124,30 @@ describe('link', () => { mdTest('[empty]()', 'empty'); }); +describe('emoji', () => { + const emojis = new Map(); + + emojis.set('unicode', { unicode: 'u' }); + mdTest(':unicode:', 'u', 'u', { emojis }); + + emojis.set('emoticon', { shortcode: 'shortcode', mxc: 'mxc://' }); + mdTest(':emoticon:', ':shortcode:', ':shortcode:', { emojis }); + + mdTest(':unknown:', ':unknown:', { emojis }); + mdTest(':unicode:unknown:', 'uunknown:', 'uunknown:', { emojis }); + mdTest(':unknown:unicode:', ':unknownu', ':unknownu', { emojis }); +}); + +describe('mention', () => { + mdTest('#room:example.com', '#room:example.com'); +}); + +describe('image', () => { + mdTest('![alt](https://example.com)', 'alt'); + + mdTest('![empty]()', 'empty'); +}); + // describe('blockquote', () => { // mdTest('> quote', '
quote
'); // mdTest('>quote', '> quote', '
quote
'); @@ -110,11 +174,10 @@ describe('list', () => { // '', // ); - const elementHtml = ''; - test(elementHtml, () => { - const content = html(elementHtml, { kind: 'edit', onlyPlain: true }); - expect(content.plain).toBe('* item1\n * subitem1\n * subitem2\n* item2'); - }); + htmlTest( + '', + '* item1\n * subitem1\n * subitem2\n* item2', + ); }); describe('table', () => { @@ -129,10 +192,18 @@ describe('table', () => { '
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
'); - }); + htmlTest( + '
head
cell
', + '| head |\n| ---- |\n| cell |', + '
head
cell
', + ); +}); + +describe('html', () => { + htmlTest('
text
', 'text'); + htmlTest('text', 'text'); + + htmlTest('', ''); + + htmlTest('reply', '', ''); });