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

fix: import notion database with title #8661

Merged
merged 1 commit into from
Nov 4, 2024
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
158 changes: 83 additions & 75 deletions packages/blocks/src/__tests__/adapters/notion-html.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,80 +1198,84 @@ describe('notion html to snapshot', () => {

test('table', async () => {
const html = `<div class="page-body">
<table class="collection-content">
<thead>
<tr>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesTitle">
</svg></span>Name</th>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesMultipleSelect">
</svg></span>Tags</th>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesMultipleSelect">
</svg></span>Multi-select</th>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesNumber">
</svg></span>Number</th>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesStatus">
</svg></span>Status</th>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesCheckbox">
</svg></span>Checkbox</th>
</tr>
</thead>
<tbody>
<tr id="ed3d2ae9-62f5-433a-9049-9ddbd1c81ac5">
<td class="cell-title"><a
href="https://www.notion.so/https-affine-pro-ed3d2ae962f5433a90499ddbd1c81ac5?pvs=21">https://affine.pro</a>
</td>
<td class="cell-tC]_"><span class="selected-value select-value-color-red">aaa</span></td>
<td class="cell-GFq@"><span class="selected-value select-value-color-gray">aaa</span><span
class="selected-value select-value-color-blue">bbb</span></td>
<td class="cell-Kruh">5</td>
<td class="cell-S_z"><span class="status-value">
<div class="status-dot"></div>Not started
</span></td>
<td class="cell-Wgk@">
<div class="checkbox checkbox-off"></div>
</td>
</tr>
<tr id="ed3d2ae9-62f5-433a-9049-9ddbd1c81ac5">
<td class="cell-title"><a
href="https://www.notion.so/ed3d2ae962f5433a90499ddbd1c81ac5?pvs=21">Untitled</a></td>
<td class="cell-tC]_"><span class="selected-value select-value-color-green">bbb</span></td>
<td class="cell-GFq@"><span class="selected-value select-value-color-gray">aaa</span><span
class="selected-value select-value-color-brown">ccc</span></td>
<td class="cell-Kruh">7</td>
<td class="cell-S_z"><span class="status-value">
<div class="status-dot"></div>Not started
</span></td>
<td class="cell-Wgk@">
<div class="checkbox checkbox-on"></div>
</td>
</tr>
<tr id="ed3d2ae9-62f5-433a-9049-9ddbd1c81ac5">
<td class="cell-title"><a
href="https://www.notion.so/ed3d2ae962f5433a90499ddbd1c81ac5?pvs=21">Untitled</a></td>
<td class="cell-tC]_"><span class="selected-value select-value-color-red">aaa</span></td>
<td class="cell-GFq@"><span class="selected-value select-value-color-blue">bbb</span></td>
<td class="cell-Kruh">9</td>
<td class="cell-S_z"><span class="status-value">
<div class="status-dot"></div>Not started
</span></td>
<td class="cell-Wgk@">
<div class="checkbox checkbox-off"></div>
</td>
</tr>
</tbody>
</table><br /><br />
<div id="11f088dd-6fdb-80d1-82fa-cbdf6e445620" class="collection-content">
<h4 class="collection-title">Table View</h4>
<table class="collection-content">
<thead>
<tr>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesTitle">
</svg></span>Name</th>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesMultipleSelect">
</svg></span>Tags</th>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesMultipleSelect">
</svg></span>Multi-select</th>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesNumber">
</svg></span>Number</th>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesStatus">
</svg></span>Status</th>
<th><span class="icon property-icon"><svg role="graphics-symbol" viewBox="0 0 16 16"
style="width:14px;height:14px;display:block;fill:rgba(55, 53, 47, 0.45);flex-shrink:0"
class="typesCheckbox">
</svg></span>Checkbox</th>
</tr>
</thead>
<tbody>
<tr id="ed3d2ae9-62f5-433a-9049-9ddbd1c81ac5">
<td class="cell-title"><a
href="https://www.notion.so/https-affine-pro-ed3d2ae962f5433a90499ddbd1c81ac5?pvs=21">https://affine.pro</a>
</td>
<td class="cell-tC]_"><span class="selected-value select-value-color-red">aaa</span></td>
<td class="cell-GFq@"><span class="selected-value select-value-color-gray">aaa</span><span
class="selected-value select-value-color-blue">bbb</span></td>
<td class="cell-Kruh">5</td>
<td class="cell-S_z"><span class="status-value">
<div class="status-dot"></div>Not started
</span></td>
<td class="cell-Wgk@">
<div class="checkbox checkbox-off"></div>
</td>
</tr>
<tr id="ed3d2ae9-62f5-433a-9049-9ddbd1c81ac5">
<td class="cell-title"><a
href="https://www.notion.so/ed3d2ae962f5433a90499ddbd1c81ac5?pvs=21">Untitled</a></td>
<td class="cell-tC]_"><span class="selected-value select-value-color-green">bbb</span></td>
<td class="cell-GFq@"><span class="selected-value select-value-color-gray">aaa</span><span
class="selected-value select-value-color-brown">ccc</span></td>
<td class="cell-Kruh">7</td>
<td class="cell-S_z"><span class="status-value">
<div class="status-dot"></div>Not started
</span></td>
<td class="cell-Wgk@">
<div class="checkbox checkbox-on"></div>
</td>
</tr>
<tr id="ed3d2ae9-62f5-433a-9049-9ddbd1c81ac5">
<td class="cell-title"><a
href="https://www.notion.so/ed3d2ae962f5433a90499ddbd1c81ac5?pvs=21">Untitled</a></td>
<td class="cell-tC]_"><span class="selected-value select-value-color-red">aaa</span></td>
<td class="cell-GFq@"><span class="selected-value select-value-color-blue">bbb</span></td>
<td class="cell-Kruh">9</td>
<td class="cell-S_z"><span class="status-value">
<div class="status-dot"></div>Not started
</span></td>
<td class="cell-Wgk@">
<div class="checkbox checkbox-off"></div>
</td>
</tr>
</tbody>
</table>
</div>
<br /><br />
</div>`;

const blockSnapshot: BlockSnapshot = {
Expand Down Expand Up @@ -1310,7 +1314,11 @@ describe('notion html to snapshot', () => {
],
title: {
'$blocksuite:internal:text$': true,
delta: [],
delta: [
{
insert: 'Table View',
},
],
},
columns: [
{
Expand Down
22 changes: 21 additions & 1 deletion packages/blocks/src/_common/adapters/notion-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const ColumnClassMap: Record<string, string> = {
const NotionInlineEquationToken = 'notion-text-equation-token';
const NotionUnderlineStyleToken = 'border-bottom:0.05em solid';
const NotionCheckboxToken = '.checkbox';
const NotionDatabaseToken = '.collection-content';
const NotionDatabaseTitleToken = '.collection-title';

type BlocksuiteTableColumn = {
type: string;
Expand Down Expand Up @@ -477,6 +479,9 @@ export class NotionHtmlAdapter extends BaseAdapter<NotionHtml> {
case 'h4':
case 'h5':
case 'h6': {
if (hastQuerySelector(o.node, NotionDatabaseTitleToken)) {
break;
}
context
.openNode(
{
Expand Down Expand Up @@ -1082,6 +1087,15 @@ export class NotionHtmlAdapter extends BaseAdapter<NotionHtml> {
cells[children.at(i)?.id ?? nanoid()] = row;
});
context.setGlobalContextStack('hast:table:cells', []);
let databaseTitle = '';
if (
o.parent?.node.type === 'element' &&
hastQuerySelector(o.parent.node, NotionDatabaseToken)
) {
databaseTitle = hastGetTextContent(
hastQuerySelector(o.parent.node, NotionDatabaseTitleToken)
);
}
context.openNode(
{
type: 'block',
Expand Down Expand Up @@ -1109,7 +1123,13 @@ export class NotionHtmlAdapter extends BaseAdapter<NotionHtml> {
],
title: {
'$blocksuite:internal:text$': true,
delta: [],
delta: databaseTitle
? [
{
insert: databaseTitle,
},
]
: [],
},
columns,
cells,
Expand Down
Loading