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 Dec 3, 2024
1 parent 6e0bc08 commit 20d22f5
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/affine/block-list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@lit/context": "^1.1.2",
"@preact/signals-core": "^1.8.0",
"@toeverything/theme": "^1.0.19",
"@types/mdast": "^4.0.4",
"lit": "^3.2.0",
"minimatch": "^10.0.1",
"zod": "^3.23.8"
Expand Down
12 changes: 7 additions & 5 deletions packages/affine/block-list/src/adapters/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DeltaInsert } from '@blocksuite/inline';
import type { List } from 'mdast';

import { ListBlockSchema } from '@blocksuite/affine-model';
import {
Expand All @@ -20,11 +21,11 @@ export const listBlockMarkdownAdapterMatcher: BlockMarkdownAdapterMatcher = {
toBlockSnapshot: {
enter: (o, context) => {
const { walkerContext, deltaConverter } = context;
if (o.node.type === 'list') {
walkerContext.setNodeContext('mdast:list:ordered', o.node.ordered);
return;
}
if (o.node.type === 'listItem') {
const parentList = o.parent?.node as List;
const listNumber = parentList.start
? parentList.start + parentList.children.indexOf(o.node)
: null;
walkerContext.openNode(
{
type: 'block',
Expand All @@ -34,7 +35,7 @@ export const listBlockMarkdownAdapterMatcher: BlockMarkdownAdapterMatcher = {
type:
o.node.checked !== null
? 'todo'
: walkerContext.getNodeContext('mdast:list:ordered')
: parentList.ordered
? 'numbered'
: 'bulleted',
text: {
Expand All @@ -46,6 +47,7 @@ export const listBlockMarkdownAdapterMatcher: BlockMarkdownAdapterMatcher = {
},
checked: o.node.checked ?? false,
collapsed: false,
order: listNumber,
},
children: [],
},
Expand Down
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
119 changes: 119 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,116 @@ 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
bbb
3. ccc
4. ddd
`;

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:paragraph',
props: {
type: 'text',
text: {
'$blocksuite:internal:text$': true,
delta: [
{
insert: 'bbb',
},
],
},
},
children: [],
},
{
type: 'block',
id: 'matchesReplaceMap[3]',
flavour: 'affine:list',
props: {
type: 'numbered',
text: {
'$blocksuite:internal:text$': true,
delta: [
{
insert: 'ccc',
},
],
},
checked: false,
collapsed: false,
order: 3,
},
children: [],
},
{
type: 'block',
id: 'matchesReplaceMap[4]',
flavour: 'affine:list',
props: {
type: 'numbered',
text: {
'$blocksuite:internal:text$': true,
delta: [
{
insert: 'ddd',
},
],
},
checked: false,
collapsed: false,
order: 4,
},
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
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,7 @@ __metadata:
"@lit/context": "npm:^1.1.2"
"@preact/signals-core": "npm:^1.8.0"
"@toeverything/theme": "npm:^1.0.19"
"@types/mdast": "npm:^4.0.4"
lit: "npm:^3.2.0"
minimatch: "npm:^10.0.1"
zod: "npm:^3.23.8"
Expand Down

0 comments on commit 20d22f5

Please sign in to comment.