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 0fe8416
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
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 0fe8416

Please sign in to comment.