Skip to content

Commit

Permalink
fix(list): support checklist && add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jiawei686 authored and humyfred committed Dec 28, 2021
1 parent 43ba79a commit f4d6a2a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/core/hooks/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,18 @@ export default class List extends ParagraphBase {
const attr = {};
const content = children.reduce((html, item) => {
const child = this.tree[item];
const itemAttr = {};
const str = `<p>${child.strs.join('<br>')}</p>`;
child.lines += child.strs.length;
const children = child.children.length ? this.renderTree(item) : '';
node.lines += child.lines;
lines += child.lines;
return `${html}<li>${str}${children}</li>`;
// checklist 样式适配
const checklistRegex = /<span class="ch-icon ch-icon-(square|check)"><\/span>/;
if (checklistRegex.test(str)) {
itemAttr.class = 'check-list-item';
}
return `${html}<li${attrsToAttributeString(itemAttr)}>${str}${children}</li>`;
}, '');
if (node.parent === undefined) {
// 根节点增加属性
Expand Down
11 changes: 10 additions & 1 deletion test/core/hooks/List.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,22 @@ cases[3] = `
- test
`;

cases[4] = `
- [ ] checklist 1
- test
- [x] checklist 2
- [ ] checklist 3
- test
- [ ] checklist 4
`;

const listHook = new List({ config: {}});
Object.defineProperty(listHook, '$engine', {
value: { md5 },
});

describe('core/hooks/list', () => {
it('checklist replace', () => {
it('list hook', () => {
cases.forEach((item) => {
expect(listHook.toHtml(item, (text) => ({ html: text }))).toMatchSnapshot();
});
Expand Down
10 changes: 6 additions & 4 deletions test/core/hooks/__snapshots__/List.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`core/hooks/list checklist replace 1`] = `"<ul data-lines=\\"8\\" data-sign=\\"0ee90839df66d748d6faefc70677e3ee\\" class=\\"cherry-list__default\\"><li><p>1</p></li><li><p>2</p><ul class=\\"cherry-list__default\\"><li><p>2.1</p></li><li><p>2.2</p></li></ul></li><li><p>3</p><ul class=\\"cherry-list__circle\\"><li><p>3.1</p></li></ul></li><li><p>4</p><ul class=\\"cherry-list__square\\"><li><p>4.2</p></li></ul></li></ul>"`;
exports[`core/hooks/list list hook 1`] = `"<ul data-lines=\\"8\\" data-sign=\\"0ee90839df66d748d6faefc70677e3ee\\" class=\\"cherry-list__default\\"><li><p>1</p></li><li><p>2</p><ul class=\\"cherry-list__default\\"><li><p>2.1</p></li><li><p>2.2</p></li></ul></li><li><p>3</p><ul class=\\"cherry-list__circle\\"><li><p>3.1</p></li></ul></li><li><p>4</p><ul class=\\"cherry-list__square\\"><li><p>4.2</p></li></ul></li></ul>"`;
exports[`core/hooks/list checklist replace 2`] = `"<ul data-lines=\\"8\\" data-sign=\\"517b0da7dd7b1b616ab99665b9d280eb\\" class=\\"cherry-list__default\\"><li><p>1</p><ul class=\\"cherry-list__default\\"><li><p>2</p><ul class=\\"cherry-list__default\\"><li><p>2.1</p></li><li><p>2.2</p></li></ul></li></ul></li><li><p>3<br>3.1</p></li><li><p>4</p></li><li><p>4.2</p></li></ul>"`;
exports[`core/hooks/list list hook 2`] = `"<ul data-lines=\\"8\\" data-sign=\\"517b0da7dd7b1b616ab99665b9d280eb\\" class=\\"cherry-list__default\\"><li><p>1</p><ul class=\\"cherry-list__default\\"><li><p>2</p><ul class=\\"cherry-list__default\\"><li><p>2.1</p></li><li><p>2.2</p></li></ul></li></ul></li><li><p>3<br>3.1</p></li><li><p>4</p></li><li><p>4.2</p></li></ul>"`;
exports[`core/hooks/list checklist replace 3`] = `"<ul data-lines=\\"1\\" data-sign=\\"567632d2786fbb27c9e5df62189c44a7\\" class=\\"cherry-list__default\\"><li><p>1</p></li></ul><ol data-lines=\\"2\\" data-sign=\\"567632d2786fbb27c9e5df62189c44a7\\" class=\\"cherry-list__default\\"><li><p>test<br></p></li></ol><ul data-lines=\\"8\\" data-sign=\\"567632d2786fbb27c9e5df62189c44a7\\" class=\\"cherry-list__default\\"><li><p>1.1</p><ul class=\\"cherry-list__default\\"><li><p>1.1.2</p><ul class=\\"cherry-list__default\\"><li><p>blank</p></li></ul></li><li><p>1.2</p></li></ul></li><li><p>2<br>blank</p><ul class=\\"cherry-list__default\\"><li><p>2.1</p></li><li><p>2.2</p></li></ul></li></ul><ol data-lines=\\"2\\" data-sign=\\"567632d2786fbb27c9e5df62189c44a7\\" class=\\"cherry-list__default\\"><li><p>test</p><ol class=\\"cherry-list__default\\"><li><p>2</p></li></ol></li></ol>"`;
exports[`core/hooks/list list hook 3`] = `"<ul data-lines=\\"1\\" data-sign=\\"567632d2786fbb27c9e5df62189c44a7\\" class=\\"cherry-list__default\\"><li><p>1</p></li></ul><ol data-lines=\\"2\\" data-sign=\\"567632d2786fbb27c9e5df62189c44a7\\" class=\\"cherry-list__default\\"><li><p>test<br></p></li></ol><ul data-lines=\\"8\\" data-sign=\\"567632d2786fbb27c9e5df62189c44a7\\" class=\\"cherry-list__default\\"><li><p>1.1</p><ul class=\\"cherry-list__default\\"><li><p>1.1.2</p><ul class=\\"cherry-list__default\\"><li><p>blank</p></li></ul></li><li><p>1.2</p></li></ul></li><li><p>2<br>blank</p><ul class=\\"cherry-list__default\\"><li><p>2.1</p></li><li><p>2.2</p></li></ul></li></ul><ol data-lines=\\"2\\" data-sign=\\"567632d2786fbb27c9e5df62189c44a7\\" class=\\"cherry-list__default\\"><li><p>test</p><ol class=\\"cherry-list__default\\"><li><p>2</p></li></ol></li></ol>"`;
exports[`core/hooks/list checklist replace 4`] = `"<ol data-lines=\\"8\\" data-sign=\\"a7f4c176bd29fce6eed2db95c7433f88\\" class=\\"cherry-list__default\\"><li><p>test</p><ol class=\\"cherry-list__default\\"><li><p>test</p></li></ol></li><li><p>test</p><ol class=\\"cherry-list__cjk-ideographic\\"><li><p>test</p></li><li><p>test<br><br></p></li><li><p>test</p></li></ol></li></ol><ul data-lines=\\"1\\" data-sign=\\"a7f4c176bd29fce6eed2db95c7433f88\\" class=\\"cherry-list__default\\"><li><p>test</p></li></ul>"`;
exports[`core/hooks/list list hook 4`] = `"<ol data-lines=\\"8\\" data-sign=\\"a7f4c176bd29fce6eed2db95c7433f88\\" class=\\"cherry-list__default\\"><li><p>test</p><ol class=\\"cherry-list__default\\"><li><p>test</p></li></ol></li><li><p>test</p><ol class=\\"cherry-list__cjk-ideographic\\"><li><p>test</p></li><li><p>test<br><br></p></li><li><p>test</p></li></ol></li></ol><ul data-lines=\\"1\\" data-sign=\\"a7f4c176bd29fce6eed2db95c7433f88\\" class=\\"cherry-list__default\\"><li><p>test</p></li></ul>"`;
exports[`core/hooks/list list hook 5`] = `"<ul data-lines=\\"6\\" data-sign=\\"1352e6eb28a6f713e316fbe3733cdee7\\" class=\\"cherry-list__default\\"><li class=\\"check-list-item\\"><p><span class=\\"ch-icon ch-icon-square\\"></span> checklist 1</p></li><li><p>test</p><ul class=\\"cherry-list__default\\"><li class=\\"check-list-item\\"><p><span class=\\"ch-icon ch-icon-check\\"></span> checklist 2</p></li></ul></li><li class=\\"check-list-item\\"><p><span class=\\"ch-icon ch-icon-square\\"></span> checklist 3</p></li><li><p>test</p><ul class=\\"cherry-list__default\\"><li class=\\"check-list-item\\"><p><span class=\\"ch-icon ch-icon-square\\"></span> checklist 4</p></li></ul></li></ul>"`;

0 comments on commit f4d6a2a

Please sign in to comment.