Add list tests

This commit is contained in:
ginnyTheCat 2022-10-01 22:34:22 +02:00
parent afbc1ccf9f
commit 6d864bfedb
No known key found for this signature in database
GPG key ID: 6B7EF3FED72A0D97
5 changed files with 1099 additions and 76 deletions

3
jest.config.js Normal file
View file

@ -0,0 +1,3 @@
module.exports = {
testEnvironment: 'jsdom',
};

1122
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -69,6 +69,7 @@
"html-loader": "4.2.0", "html-loader": "4.2.0",
"html-webpack-plugin": "5.5.0", "html-webpack-plugin": "5.5.0",
"jest": "29.0.3", "jest": "29.0.3",
"jest-environment-jsdom": "29.1.2",
"mini-css-extract-plugin": "2.6.1", "mini-css-extract-plugin": "2.6.1",
"path-browserify": "1.0.1", "path-browserify": "1.0.1",
"sass": "1.55.0", "sass": "1.55.0",

View file

@ -153,11 +153,22 @@ const markdownRules = {
state._list = oldList; state._list = oldList;
if (!state._list) { if (state._list) {
items = `\n${items}`;
} else {
items += '\n\n'; items += '\n\n';
} }
return items; return items;
}, },
html: (node, output, state) => {
const items = node.items
.map((item) => htmlTag('li', output(item, state)))
.join('');
return htmlTag(node.ordered ? 'ol' : 'ul', items, {
start: node.start === 1 ? undefined : node.start,
});
},
}, },
def: undefined, def: undefined,
table: { table: {
@ -358,7 +369,7 @@ function mapElement(el) {
return false; return false;
}); });
} }
return [{ type: 'codeBlock', lang, content: el.innerText }]; return [{ type: 'codeBlock', lang, content: el.textContent }];
} }
case 'BLOCKQUOTE': case 'BLOCKQUOTE':
return [{ type: 'blockQuote', content: mapChildren(el) }]; return [{ type: 'blockQuote', content: mapChildren(el) }];
@ -368,7 +379,7 @@ function mapElement(el) {
return [{ return [{
type: 'list', type: 'list',
ordered: true, ordered: true,
start: Number(el.getAttribute('start')), start: Number(el.getAttribute('start')) || 1,
items: Array.from(el.childNodes).map(mapNode), items: Array.from(el.childNodes).map(mapNode),
}]; }];
case 'TABLE': { case 'TABLE': {
@ -433,7 +444,7 @@ function mapElement(el) {
case 'STRIKE': case 'STRIKE':
return [{ type: 'del', content: mapChildren(el) }]; return [{ type: 'del', content: mapChildren(el) }];
case 'CODE': case 'CODE':
return [{ type: 'inlineCode', content: el.innerText }]; return [{ type: 'inlineCode', content: el.textContent }];
case 'DIV': case 'DIV':
if (el.hasAttribute('data-mx-maths')) { if (el.hasAttribute('data-mx-maths')) {

View file

@ -1,17 +1,21 @@
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
/* eslint-disable no-undef */ /* eslint-disable no-undef */
import { markdown } from './markdown'; import { html, markdown } from './markdown';
function mdTest(source, plain, html) { function mdTest(source, plain, htmlStr) {
test(source, () => { test(source, () => {
if (html === undefined) { if (htmlStr === undefined) {
html = plain; htmlStr = plain;
plain = source; plain = source;
} }
const content = markdown(source, { kind: 'edit' }); const content = markdown(source, { kind: 'edit' });
expect(content.plain).toBe(plain); expect(content.plain).toBe(plain);
expect(content.html).toBe(html); expect(content.html).toBe(htmlStr);
const htmlContent = html(htmlStr, { kind: 'edit', onlyPlain: true });
expect(htmlContent.plain).toBe(plain);
}); });
} }
@ -85,3 +89,19 @@ describe('blockquote', () => {
mdTest('> quote\n\ntext after', '<blockquote>quote</blockquote>text after'); mdTest('> quote\n\ntext after', '<blockquote>quote</blockquote>text after');
}); });
describe('list', () => {
mdTest('* item1\n* item2', '<ul><li>item1</li><li>item2</li></ul>');
mdTest('- item1\n- item2', '* item1\n* item2', '<ul><li>item1</li><li>item2</li></ul>');
mdTest('1. item1\n2. item2', '<ol><li>item1</li><li>item2</li></ol>');
mdTest('2. item2\n3. item3', '<ol start="2"><li>item2</li><li>item3</li></ol>');
mdTest('* item1\n * subitem1\n * subitem2\n* item2', '<ul><li>item1<ul><li>subitem1</li><li>subitem2</li></ul></li><li>item2</li></ul>');
const elementHtml = '<ul><li>item1<ul><li>subitem1</li><li>subitem2</li></ul></li><li>item2</li></ul>';
test(elementHtml, () => {
const content = html(elementHtml, { kind: 'edit', onlyPlain: true });
expect(content.plain).toBe('* item1\n * subitem1\n * subitem2\n* item2');
});
});