Skip to content

Commit

Permalink
refactor(editor): extract markdown adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Dec 31, 2024
1 parent 6883cc2 commit 52994a0
Show file tree
Hide file tree
Showing 25 changed files with 333 additions and 181 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { ExtensionType } from '@blocksuite/block-std';

import { EmbedSyncedDocBlockHtmlAdapterExtension } from './html.js';
import { EmbedSyncedDocBlockMarkdownAdapterExtension } from './markdown.js';
import { EmbedSyncedDocMarkdownAdapterExtension } from './markdown.js';
import { EmbedSyncedDocBlockPlainTextAdapterExtension } from './plain-text.js';

export const EmbedSyncedDocBlockAdapterExtensions: ExtensionType[] = [
EmbedSyncedDocBlockHtmlAdapterExtension,
EmbedSyncedDocBlockMarkdownAdapterExtension,
EmbedSyncedDocMarkdownAdapterExtension,
EmbedSyncedDocBlockPlainTextAdapterExtension,
];
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ export const embedSyncedDocBlockMarkdownAdapterMatcher: BlockMarkdownAdapterMatc
},
};

export const EmbedSyncedDocBlockMarkdownAdapterExtension =
export const EmbedSyncedDocMarkdownAdapterExtension =
BlockMarkdownAdapterExtension(embedSyncedDocBlockMarkdownAdapterMatcher);
12 changes: 12 additions & 0 deletions blocksuite/affine/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@
"lit": "^3.2.0",
"lodash.clonedeep": "^4.5.0",
"lodash.mergewith": "^4.6.2",
"mdast-util-gfm-autolink-literal": "^2.0.1",
"mdast-util-gfm-strikethrough": "^2.0.0",
"mdast-util-gfm-table": "^2.0.0",
"mdast-util-gfm-task-list-item": "^2.0.0",
"micromark-extension-gfm-autolink-literal": "^2.1.0",
"micromark-extension-gfm-strikethrough": "^2.1.0",
"micromark-extension-gfm-table": "^2.1.0",
"micromark-extension-gfm-task-list-item": "^2.1.0",
"micromark-util-combine-extensions": "^2.0.0",
"minimatch": "^10.0.1",
"rehype-parse": "^9.0.0",
"rehype-stringify": "^10.0.0",
"remark-math": "^6.0.0",
"remark-parse": "^11.0.0",
"remark-stringify": "^11.0.0",
"unified": "^11.0.5",
"zod": "^3.23.8"
},
Expand Down
5 changes: 5 additions & 0 deletions blocksuite/affine/shared/src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ export {
BlockMarkdownAdapterExtension,
type BlockMarkdownAdapterMatcher,
BlockMarkdownAdapterMatcherIdentifier,
InlineDeltaToMarkdownAdapterExtension,
type InlineDeltaToMarkdownAdapterMatcher,
InlineDeltaToMarkdownAdapterMatcherIdentifier,
isMarkdownAST,
type Markdown,
MarkdownAdapter,
MarkdownAdapterFactoryExtension,
MarkdownAdapterFactoryIdentifier,
type MarkdownAST,
MarkdownASTToDeltaExtension,
type MarkdownASTToDeltaMatcher,
MarkdownASTToDeltaMatcherIdentifier,
MarkdownDeltaConverter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function gfmToMarkdown() {
}

export function remarkGfm(this: Processor) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
// oxlint-disable-next-line typescript/no-this-alias
const self = this;
const data = self.data();

Expand Down
1 change: 1 addition & 0 deletions blocksuite/affine/shared/src/adapters/markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './block-adapter.js';
export * from './delta-converter.js';
export * from './markdown.js';
export * from './type.js';
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { DefaultTheme, NoteDisplayMode } from '@blocksuite/affine-model';
import {
type AdapterContext,
AdapterFactoryIdentifier,
type BlockMarkdownAdapterMatcher,
BlockMarkdownAdapterMatcherIdentifier,
type Markdown,
type MarkdownAST,
MarkdownDeltaConverter,
} from '@blocksuite/affine-shared/adapters';
import type { ExtensionType } from '@blocksuite/block-std';
import type { ServiceProvider } from '@blocksuite/global/di';
import {
type AssetsManager,
ASTWalker,
Expand All @@ -34,10 +26,18 @@ import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import { unified } from 'unified';

import { defaultBlockMarkdownAdapterMatchers } from './block-matcher.js';
import { inlineDeltaToMarkdownAdapterMatchers } from './delta-converter/inline-delta.js';
import { markdownInlineToDeltaMatchers } from './delta-converter/markdown-inline.js';
import { remarkGfm } from './gfm.js';
import { type AdapterContext, AdapterFactoryIdentifier } from '../types';
import {
type BlockMarkdownAdapterMatcher,
BlockMarkdownAdapterMatcherIdentifier,
} from './block-adapter';
import {
InlineDeltaToMarkdownAdapterMatcherIdentifier,
MarkdownASTToDeltaMatcherIdentifier,
MarkdownDeltaConverter,
} from './delta-converter';
import { remarkGfm } from './gfm';
import type { Markdown, MarkdownAST } from './type';

type MarkdownToSliceSnapshotPayload = {
file: Markdown;
Expand Down Expand Up @@ -164,11 +164,20 @@ export class MarkdownAdapter extends BaseAdapter<Markdown> {

deltaConverter: MarkdownDeltaConverter;

constructor(
job: Job,
readonly blockMatchers: BlockMarkdownAdapterMatcher[] = defaultBlockMarkdownAdapterMatchers
) {
readonly blockMatchers: BlockMarkdownAdapterMatcher[];

constructor(job: Job, provider: ServiceProvider) {
super(job);
const blockMatchers = Array.from(
provider.getAll(BlockMarkdownAdapterMatcherIdentifier).values()
);
const inlineDeltaToMarkdownAdapterMatchers = Array.from(
provider.getAll(InlineDeltaToMarkdownAdapterMatcherIdentifier).values()
);
const markdownInlineToDeltaMatchers = Array.from(
provider.getAll(MarkdownASTToDeltaMatcherIdentifier).values()
);
this.blockMatchers = blockMatchers;
this.deltaConverter = new MarkdownDeltaConverter(
job.adapterConfigs,
inlineDeltaToMarkdownAdapterMatchers,
Expand Down Expand Up @@ -440,13 +449,7 @@ export const MarkdownAdapterFactoryIdentifier =
export const MarkdownAdapterFactoryExtension: ExtensionType = {
setup: di => {
di.addImpl(MarkdownAdapterFactoryIdentifier, provider => ({
get: (job: Job) =>
new MarkdownAdapter(
job,
Array.from(
provider.getAll(BlockMarkdownAdapterMatcherIdentifier).values()
)
),
get: (job: Job) => new MarkdownAdapter(job, provider),
}));
},
};
Loading

0 comments on commit 52994a0

Please sign in to comment.