Skip to content

Commit

Permalink
feat(page): support table rich-text in markdown adapter (#7077)
Browse files Browse the repository at this point in the history
  • Loading branch information
fourdim committed May 17, 2024
1 parent f0fb4ad commit 8b9b569
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 80 deletions.
10 changes: 9 additions & 1 deletion packages/blocks/src/__tests__/adapters/markdown.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,9 @@ hhh
delta: [
{
insert: 'test2',
attributes: {
link: 'https://google.com',
},
},
],
},
Expand Down Expand Up @@ -1364,7 +1367,12 @@ hhh
],
};

const md = `| Title | Status | Date | Number | Progress | MultiSelect | RichText | Link | Checkbox |\n| ------ | ----------- | ---------- | ------ | -------- | ----------- | -------- | ------------------ | -------- |\n| Task 1 | TODO | 2023-12-15 | 1 | 65 | test1,test2 | test2 | https://google.com | true |\n| Task 2 | In Progress | 2023-12-20 | | | | test1 | | |\n`;
const md = `\
| Title | Status | Date | Number | Progress | MultiSelect | RichText | Link | Checkbox |
| ------ | ----------- | ---------- | ------ | -------- | ----------- | --------------------------- | ------------------ | -------- |
| Task 1 | TODO | 2023-12-15 | 1 | 65 | test1,test2 | [test2](https://google.com) | https://google.com | true |
| Task 2 | In Progress | 2023-12-20 | | | | test1 | | |
`;
const mdAdapter = new MarkdownAdapter();
const target = await mdAdapter.fromBlockSnapshot({
snapshot: blockSnapshot,
Expand Down
142 changes: 63 additions & 79 deletions packages/blocks/src/_common/adapters/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,7 @@ export class MarkdownAdapter extends BaseAdapter<Markdown> {
const columns = o.node.props.columns as Array<Column>;
const children = o.node.children;
const cells = o.node.props.cells as SerializedCells;
const createAstCell = (
children: Record<string, string | undefined | unknown>[]
) => ({
const createAstCell = (children: MarkdownAST[]) => ({
type: 'tableCell',
children,
});
Expand All @@ -516,84 +514,70 @@ export class MarkdownAdapter extends BaseAdapter<Markdown> {
(v: BlockSnapshot) =>
Array.prototype.map.call(columns, col => {
const cell = cells[v.id]?.[col.id];
let r;
if (cell || col.type === 'title') {
switch (col.type) {
case 'link':
case 'progress':
case 'number':
r = createAstCell([
{
type: 'text',
value: cell.value,
},
]);
break;
case 'rich-text':
r = createAstCell([
{
type: 'text',
value: (cell.value as { delta: DeltaInsert[] }).delta
.map(v => v.insert)
.join(),
},
]);
break;
case 'title':
r = createAstCell([
{
type: 'text',
value: (
v.props.text as { delta: DeltaInsert[] }
).delta
.map(v => v.insert)
.join(''),
},
]);
break;
case 'date':
r = createAstCell([
{
type: 'text',
value: format(
new Date(cell.value as number),
'yyyy-MM-dd'
),
},
]);
break;
case 'select': {
const value = col.data.options.find(
(opt: Record<string, string>) => opt.id === cell.value
)?.value;
r = createAstCell([{ type: 'text', value }]);
break;
}
case 'multi-select': {
const value = Array.prototype.map
.call(
cell.value,
val =>
col.data.options.find(
(opt: Record<string, string>) => val === opt.id
).value
)
.filter(Boolean)
.join(',');
r = createAstCell([{ type: 'text', value }]);
break;
}
case 'checkbox': {
r = createAstCell([{ type: 'text', value: cell.value }]);
break;
}
default:
r = createAstCell([{ type: 'text', value: '' }]);
if (!cell && col.type !== 'title') {
return createAstCell([{ type: 'text', value: '' }]);
}
switch (col.type) {
case 'link':
case 'progress':
case 'number':
return createAstCell([
{
type: 'text',
value: cell.value as string,
},
]);
case 'rich-text':
return createAstCell(
this._deltaToMdAST(
(cell.value as { delta: DeltaInsert[] }).delta
)
);
case 'title':
return createAstCell(
this._deltaToMdAST(
(v.props.text as { delta: DeltaInsert[] }).delta
)
);
case 'date':
return createAstCell([
{
type: 'text',
value: format(
new Date(cell.value as number),
'yyyy-MM-dd'
),
},
]);
case 'select': {
const value = col.data.options.find(
(opt: Record<string, string>) => opt.id === cell.value
)?.value;
return createAstCell([{ type: 'text', value }]);
}
case 'multi-select': {
const value = Array.prototype.map
.call(
cell.value,
val =>
col.data.options.find(
(opt: Record<string, string>) => val === opt.id
).value
)
.filter(Boolean)
.join(',');
return createAstCell([{ type: 'text', value }]);
}
case 'checkbox': {
return createAstCell([
{ type: 'text', value: cell.value as string },
]);
}
} else {
r = createAstCell([{ type: 'text', value: '' }]);
default:
return createAstCell([
{ type: 'text', value: cell.value as string },
]);
}
return r;
})
);

Expand Down

0 comments on commit 8b9b569

Please sign in to comment.