Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

refactor(editor): extract notion html adapter to shared (#9341)

donteatfriedrice4a3caaf766a9
diff --git a/blocksuite/affine/shared/src/adapters/index.ts b/blocksuite/affine/shared/src/adapters/index.ts
index 50edecbbf6..acd422798e 100644
--- a/blocksuite/affine/shared/src/adapters/index.ts
+++ b/blocksuite/affine/shared/src/adapters/index.ts
@@ -33,6 +33,9 @@ export {
BlockNotionHtmlAdapterMatcherIdentifier,
type InlineDeltaToNotionHtmlAdapterMatcher,
type NotionHtml,
+ NotionHtmlAdapter,
+ NotionHtmlAdapterFactoryExtension,
+ NotionHtmlAdapterFactoryIdentifier,
NotionHtmlASTToDeltaExtension,
type NotionHtmlASTToDeltaMatcher,
NotionHtmlASTToDeltaMatcherIdentifier,
diff --git a/blocksuite/affine/shared/src/adapters/notion-html/index.ts b/blocksuite/affine/shared/src/adapters/notion-html/index.ts
index e45efaf7c7..cf987aca9a 100644
--- a/blocksuite/affine/shared/src/adapters/notion-html/index.ts
+++ b/blocksuite/affine/shared/src/adapters/notion-html/index.ts
@@ -1,3 +1,3 @@
export * from './block-adapter.js';
export * from './delta-converter.js';
-export * from './type.js';
+export * from './notion-html.js';
diff --git a/blocksuite/blocks/src/_common/adapters/notion-html/notion-html.ts b/blocksuite/affine/shared/src/adapters/notion-html/notion-html.ts
similarity index 97%
rename from blocksuite/blocks/src/_common/adapters/notion-html/notion-html.ts
rename to blocksuite/affine/shared/src/adapters/notion-html/notion-html.ts
index 77a52fcab3..6c1a5fce32 100644
--- a/blocksuite/blocks/src/_common/adapters/notion-html/notion-html.ts
+++ b/blocksuite/affine/shared/src/adapters/notion-html/notion-html.ts
@@ -2,17 +2,6 @@ import {
DEFAULT_NOTE_BACKGROUND_COLOR,
NoteDisplayMode,
} from '@blocksuite/affine-model';
-import {
- type AdapterContext,
- AdapterFactoryIdentifier,
- type BlockNotionHtmlAdapterMatcher,
- BlockNotionHtmlAdapterMatcherIdentifier,
- HastUtils,
- type HtmlAST,
- type NotionHtml,
- NotionHtmlASTToDeltaMatcherIdentifier,
- NotionHtmlDeltaConverter,
-} from '@blocksuite/affine-shared/adapters';
import type { ExtensionType } from '@blocksuite/block-std';
import type { ServiceProvider } from '@blocksuite/global/di';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
@@ -35,6 +24,23 @@ import {
import rehypeParse from 'rehype-parse';
import { unified } from 'unified';
+import {
+ type AdapterContext,
+ AdapterFactoryIdentifier,
+} from '../types/adapter';
+import type { HtmlAST } from '../types/hast';
+import { HastUtils } from '../utils/hast';
+import {
+ type BlockNotionHtmlAdapterMatcher,
+ BlockNotionHtmlAdapterMatcherIdentifier,
+} from './block-adapter';
+import {
+ NotionHtmlASTToDeltaMatcherIdentifier,
+ NotionHtmlDeltaConverter,
+} from './delta-converter';
+
+export type NotionHtml = string;
+
type NotionHtmlToSliceSnapshotPayload = {
file: NotionHtml;
assets?: AssetsManager;
diff --git a/blocksuite/affine/shared/src/adapters/notion-html/type.ts b/blocksuite/affine/shared/src/adapters/notion-html/type.ts
deleted file mode 100644
index c2d00590b0..0000000000
--- a/blocksuite/affine/shared/src/adapters/notion-html/type.ts
+++ /dev/null
@@ -1 +0,0 @@
-export type NotionHtml = string;
diff --git a/blocksuite/blocks/src/__tests__/adapters/notion-html.unit.spec.ts b/blocksuite/blocks/src/__tests__/adapters/notion-html.unit.spec.ts
index 3807f00d33..f8b66a784a 100644
--- a/blocksuite/blocks/src/__tests__/adapters/notion-html.unit.spec.ts
+++ b/blocksuite/blocks/src/__tests__/adapters/notion-html.unit.spec.ts
@@ -2,6 +2,7 @@ import {
DEFAULT_NOTE_BACKGROUND_COLOR,
NoteDisplayMode,
} from '@blocksuite/affine-model';
+import { NotionHtmlAdapter } from '@blocksuite/affine-shared/adapters';
import { Container } from '@blocksuite/global/di';
import {
AssetsManager,
@@ -12,7 +13,6 @@ import { describe, expect, test } from 'vitest';
import { defaultBlockNotionHtmlAdapterMatchers } from '../../_common/adapters/notion-html/block-matcher.js';
import { notionHtmlInlineToDeltaMatchers } from '../../_common/adapters/notion-html/delta-converter/html-inline.js';
-import { NotionHtmlAdapter } from '../../_common/adapters/notion-html/notion-html.js';
import { nanoidReplacement } from '../../_common/test-utils/test-utils.js';
import { createJob } from '../utils/create-job.js';
diff --git a/blocksuite/blocks/src/_common/adapters/extension.ts b/blocksuite/blocks/src/_common/adapters/extension.ts
index f5291555dc..431ace5238 100644
--- a/blocksuite/blocks/src/_common/adapters/extension.ts
+++ b/blocksuite/blocks/src/_common/adapters/extension.ts
@@ -1,5 +1,6 @@
import {
HtmlAdapterFactoryExtension,
+ NotionHtmlAdapterFactoryExtension,
PlainTextAdapterFactoryExtension,
} from '@blocksuite/affine-shared/adapters';
import type { ExtensionType } from '@blocksuite/block-std';
@@ -11,7 +12,6 @@ import { ImageAdapterFactoryExtension } from './image.js';
import { MarkdownAdapterFactoryExtension } from './markdown/markdown.js';
import { MixTextAdapterFactoryExtension } from './mix-text.js';
import { notionHtmlInlineToDeltaMatchers } from './notion-html/delta-converter/html-inline.js';
-import { NotionHtmlAdapterFactoryExtension } from './notion-html/notion-html.js';
import { NotionTextAdapterFactoryExtension } from './notion-text.js';
import { inlineDeltaToPlainTextAdapterMatchers } from './plain-text/delta-converter/inline-delta.js';
diff --git a/blocksuite/blocks/src/_common/adapters/notion-html/index.ts b/blocksuite/blocks/src/_common/adapters/notion-html/index.ts
index 54badc175d..0e7d1f0faa 100644
--- a/blocksuite/blocks/src/_common/adapters/notion-html/index.ts
+++ b/blocksuite/blocks/src/_common/adapters/notion-html/index.ts
@@ -1,6 +1,2 @@
export { defaultBlockNotionHtmlAdapterMatchers } from './block-matcher.js';
-export {
- NotionHtmlAdapter,
- NotionHtmlAdapterFactoryExtension,
- NotionHtmlAdapterFactoryIdentifier,
-} from './notion-html.js';
+export { notionHtmlInlineToDeltaMatchers } from './delta-converter/html-inline.js';
diff --git a/blocksuite/blocks/src/_common/transformers/notion-html.ts b/blocksuite/blocks/src/_common/transformers/notion-html.ts
index aeecb097f6..c4cb0f7972 100644
--- a/blocksuite/blocks/src/_common/transformers/notion-html.ts
+++ b/blocksuite/blocks/src/_common/transformers/notion-html.ts
@@ -1,10 +1,10 @@
+import { NotionHtmlAdapter } from '@blocksuite/affine-shared/adapters';
import { Container } from '@blocksuite/global/di';
import { sha } from '@blocksuite/global/utils';
import { type DocCollection, extMimeMap, Job } from '@blocksuite/store';
import { defaultBlockNotionHtmlAdapterMatchers } from '../adapters/notion-html/block-matcher.js';
import { notionHtmlInlineToDeltaMatchers } from '../adapters/notion-html/delta-converter/html-inline.js';
-import { NotionHtmlAdapter } from '../adapters/notion-html/notion-html.js';
import { defaultImageProxyMiddleware } from './middlewares.js';
import { Unzip } from './utils.js';
diff --git a/blocksuite/playground/apps/_common/components/starter-debug-menu.ts b/blocksuite/playground/apps/_common/components/starter-debug-menu.ts
index b15130d8c2..c223e6d82b 100644
--- a/blocksuite/playground/apps/_common/components/starter-debug-menu.ts
+++ b/blocksuite/playground/apps/_common/components/starter-debug-menu.ts
@@ -17,6 +17,7 @@ import '@shoelace-style/shoelace/dist/themes/dark.css';
import './left-side-panel.js';
import './side-panel.js';
+import { NotionHtmlAdapter } from '@blocksuite/affine-shared/adapters';
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
import { ShadowlessElement } from '@blocksuite/block-std';
import {
@@ -35,7 +36,6 @@ import {
HtmlTransformer,
MarkdownAdapterFactoryIdentifier,
MarkdownTransformer,
- NotionHtmlAdapter,
NotionHtmlTransformer,
openFileOrFiles,
PlainTextAdapterFactoryIdentifier,
@@ -435,7 +435,7 @@ export class StarterDebugMenu extends ShadowlessElement {
collection: this.collection,
middlewares: [defaultImageProxyMiddleware],
});
- const htmlAdapter = new NotionHtmlAdapter(job);
+ const htmlAdapter = new NotionHtmlAdapter(job, this.editor.std.provider);
await htmlAdapter.toDoc({
file: await file.text(),
pageId: this.collection.idGenerator(),