Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(page): support table rich-text in markdown adapter #7077

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading