Skip to content

Commit

Permalink
feat(blocks): add support for non consecutive list in adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
fourdim committed Nov 26, 2024
1 parent ed802f3 commit 160abd7
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/blocks/src/__tests__/adapters/html.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@ describe('html to snapshot', () => {
},
checked: false,
collapsed: false,
order: null,
},
children: [],
},
Expand Down
141 changes: 141 additions & 0 deletions packages/blocks/src/__tests__/adapters/markdown.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2754,6 +2754,7 @@ hhh
},
checked: false,
collapsed: false,
order: null,
},
children: [
{
Expand All @@ -2772,6 +2773,7 @@ hhh
},
checked: false,
collapsed: false,
order: null,
},
children: [
{
Expand All @@ -2790,6 +2792,7 @@ hhh
},
checked: false,
collapsed: false,
order: null,
},
children: [],
},
Expand All @@ -2811,6 +2814,7 @@ hhh
},
checked: false,
collapsed: false,
order: null,
},
children: [],
},
Expand All @@ -2832,6 +2836,7 @@ hhh
},
checked: false,
collapsed: false,
order: null,
},
children: [],
},
Expand Down Expand Up @@ -2885,6 +2890,7 @@ hhh
},
checked: false,
collapsed: false,
order: null,
},
children: [
{
Expand All @@ -2903,6 +2909,7 @@ hhh
},
checked: true,
collapsed: false,
order: null,
},
children: [
{
Expand All @@ -2921,6 +2928,7 @@ hhh
},
checked: false,
collapsed: false,
order: null,
},
children: [],
},
Expand All @@ -2942,6 +2950,7 @@ hhh
},
checked: true,
collapsed: false,
order: null,
},
children: [],
},
Expand All @@ -2963,6 +2972,138 @@ hhh
},
checked: false,
collapsed: false,
order: null,
},
children: [],
},
],
};

const mdAdapter = new MarkdownAdapter(createJob());
const rawBlockSnapshot = await mdAdapter.toBlockSnapshot({
file: markdown,
});
expect(nanoidReplacement(rawBlockSnapshot)).toEqual(blockSnapshot);
});

test('non consecutive numbered list', async () => {
const markdown = `
1. aaa
3. bbb
5. ccc
7. ddd
8. eee
`;

const blockSnapshot: BlockSnapshot = {
type: 'block',
id: 'matchesReplaceMap[0]',
flavour: 'affine:note',
props: {
xywh: '[0,0,800,95]',
background: DEFAULT_NOTE_BACKGROUND_COLOR,
index: 'a0',
hidden: false,
displayMode: NoteDisplayMode.DocAndEdgeless,
},
children: [
{
type: 'block',
id: 'matchesReplaceMap[1]',
flavour: 'affine:list',
props: {
type: 'numbered',
text: {
'$blocksuite:internal:text$': true,
delta: [
{
insert: 'aaa',
},
],
},
checked: false,
collapsed: false,
order: 1,
},
children: [],
},
{
type: 'block',
id: 'matchesReplaceMap[2]',
flavour: 'affine:list',
props: {
type: 'numbered',
text: {
'$blocksuite:internal:text$': true,
delta: [
{
insert: 'eee',
},
],
},
checked: false,
collapsed: false,
order: 3,
},
children: [],
},
{
type: 'block',
id: 'matchesReplaceMap[3]',
flavour: 'affine:list',
props: {
type: 'numbered',
text: {
'$blocksuite:internal:text$': true,
delta: [
{
insert: 'eee',
},
],
},
checked: false,
collapsed: false,
order: 5,
},
children: [],
},
{
type: 'block',
id: 'matchesReplaceMap[4]',
flavour: 'affine:list',
props: {
type: 'numbered',
text: {
'$blocksuite:internal:text$': true,
delta: [
{
insert: 'eee',
},
],
},
checked: false,
collapsed: false,
order: 7,
},
children: [],
},
{
type: 'block',
id: 'matchesReplaceMap[5]',
flavour: 'affine:list',
props: {
type: 'numbered',
text: {
'$blocksuite:internal:text$': true,
delta: [
{
insert: 'eee',
},
],
},
checked: false,
collapsed: false,
order: 8,
},
children: [],
},
Expand Down
36 changes: 20 additions & 16 deletions packages/blocks/src/_common/adapters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,25 +484,28 @@ export class HtmlAdapter extends BaseAdapter<Html> {
);
break;
}
case 'ul':
case 'ol': {
context.setNodeContext('hast:list:type', 'bulleted');
if (o.node.tagName === 'ol') {
context.setNodeContext('hast:list:type', 'numbered');
} else if (Array.isArray(o.node.properties?.className)) {
if (o.node.properties.className.includes('to-do-list')) {
context.setNodeContext('hast:list:type', 'todo');
} else if (o.node.properties.className.includes('toggle')) {
context.setNodeContext('hast:list:type', 'toggle');
} else if (o.node.properties.className.includes('bulleted-list')) {
context.setNodeContext('hast:list:type', 'bulleted');
case 'li': {
const parentList = o.parent?.node as Element;
let listType = 'bulleted';
if (parentList.tagName === 'ol') {
listType = 'numbered';
} else if (Array.isArray(parentList.properties?.className)) {
if (parentList.properties.className.includes('to-do-list')) {
listType = 'todo';
} else if (parentList.properties.className.includes('toggle')) {
listType = 'toggle';
} else if (
parentList.properties.className.includes('bulleted-list')
) {
listType = 'bulleted';
}
}
break;
}
case 'li': {
const listNumber =
typeof parentList.properties.start === 'number'
? parentList.properties.start +
parentList.children.indexOf(o.node)
: null;
const firstElementChild = hastGetElementChildren(o.node)[0];
const listType = context.getNodeContext('hast:list:type');
o.node = hastFlatNodes(
o.node,
tagName => tagName === 'div' || tagName === 'p'
Expand Down Expand Up @@ -537,6 +540,7 @@ export class HtmlAdapter extends BaseAdapter<Html> {
firstElementChild.tagName === 'details' &&
firstElementChild.properties.open === undefined
: false,
order: listNumber,
},
children: [],
},
Expand Down
13 changes: 7 additions & 6 deletions packages/blocks/src/_common/adapters/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AffineTextAttributes } from '@blocksuite/affine-components/rich-text';
import type { DeltaInsert } from '@blocksuite/inline/types';
import type { Heading, Root, RootContentMap, TableRow } from 'mdast';
import type { Heading, List, Root, RootContentMap, TableRow } from 'mdast';

import {
type Column,
Expand Down Expand Up @@ -193,11 +193,11 @@ export class MarkdownAdapter extends BaseAdapter<Markdown> {
context.skipAllChildren();
break;
}
case 'list': {
context.setNodeContext('mdast:list:ordered', o.node.ordered);
break;
}
case 'listItem': {
const parentList = o.parent?.node as List;
const listNumber = parentList.start
? parentList.start + parentList.children.indexOf(o.node)
: null;
context.openNode(
{
type: 'block',
Expand All @@ -207,7 +207,7 @@ export class MarkdownAdapter extends BaseAdapter<Markdown> {
type:
o.node.checked !== null
? 'todo'
: context.getNodeContext('mdast:list:ordered')
: parentList.ordered
? 'numbered'
: 'bulleted',
text: {
Expand All @@ -220,6 +220,7 @@ export class MarkdownAdapter extends BaseAdapter<Markdown> {
},
checked: o.node.checked ?? false,
collapsed: false,
order: listNumber,
},
children: [],
},
Expand Down

0 comments on commit 160abd7

Please sign in to comment.