mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-19 23:37:15 +08:00
refactor(editor): extract markdown adapter (#9443)
This commit is contained in:
@@ -30,10 +30,14 @@ import {
|
||||
} from '@affine/core/modules/workspace-engine';
|
||||
import { I18n } from '@affine/i18n';
|
||||
import {
|
||||
defaultBlockMarkdownAdapterMatchers,
|
||||
docLinkBaseURLMiddleware,
|
||||
inlineDeltaToMarkdownAdapterMatchers,
|
||||
MarkdownAdapter,
|
||||
markdownInlineToDeltaMatchers,
|
||||
titleMiddleware,
|
||||
} from '@blocksuite/affine/blocks';
|
||||
import { Container } from '@blocksuite/affine/global/di';
|
||||
import { Job } from '@blocksuite/affine/store';
|
||||
import { App as CapacitorApp } from '@capacitor/app';
|
||||
import { Browser } from '@capacitor/browser';
|
||||
@@ -175,7 +179,17 @@ const frameworkProvider = framework.provider();
|
||||
});
|
||||
const snapshot = job.docToSnapshot(blockSuiteDoc);
|
||||
|
||||
const adapter = new MarkdownAdapter(job);
|
||||
const container = new Container();
|
||||
[
|
||||
...markdownInlineToDeltaMatchers,
|
||||
...defaultBlockMarkdownAdapterMatchers,
|
||||
...inlineDeltaToMarkdownAdapterMatchers,
|
||||
].forEach(ext => {
|
||||
ext.setup(container);
|
||||
});
|
||||
const provider = container.provider();
|
||||
|
||||
const adapter = new MarkdownAdapter(job, provider);
|
||||
if (!snapshot) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -10,10 +10,14 @@ import type {
|
||||
} from '@blocksuite/affine/blocks';
|
||||
import {
|
||||
CodeBlockComponent,
|
||||
defaultBlockMarkdownAdapterMatchers,
|
||||
DividerBlockComponent,
|
||||
inlineDeltaToMarkdownAdapterMatchers,
|
||||
ListBlockComponent,
|
||||
markdownInlineToDeltaMatchers,
|
||||
ParagraphBlockComponent,
|
||||
} from '@blocksuite/affine/blocks';
|
||||
import { Container, type ServiceProvider } from '@blocksuite/affine/global/di';
|
||||
import { WithDisposable } from '@blocksuite/affine/global/utils';
|
||||
import {
|
||||
BlockViewType,
|
||||
@@ -192,8 +196,28 @@ export class TextRenderer extends WithDisposable(ShadowlessElement) {
|
||||
const latestAnswer = this._answers.pop();
|
||||
this._answers = [];
|
||||
const schema = this.schema ?? this.host?.std.doc.collection.schema;
|
||||
let provider: ServiceProvider;
|
||||
if (this.host) {
|
||||
provider = this.host.std.provider;
|
||||
} else {
|
||||
const container = new Container();
|
||||
[
|
||||
...markdownInlineToDeltaMatchers,
|
||||
...defaultBlockMarkdownAdapterMatchers,
|
||||
...inlineDeltaToMarkdownAdapterMatchers,
|
||||
].forEach(ext => {
|
||||
ext.setup(container);
|
||||
});
|
||||
|
||||
provider = container.provider();
|
||||
}
|
||||
if (latestAnswer && schema) {
|
||||
markDownToDoc(schema, latestAnswer, this.options.additionalMiddlewares)
|
||||
markDownToDoc(
|
||||
provider,
|
||||
schema,
|
||||
latestAnswer,
|
||||
this.options.additionalMiddlewares
|
||||
)
|
||||
.then(doc => {
|
||||
this.disposeDoc();
|
||||
this._doc = doc.blockCollection.getDoc({
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
PlainTextAdapter,
|
||||
titleMiddleware,
|
||||
} from '@blocksuite/affine/blocks';
|
||||
import type { ServiceProvider } from '@blocksuite/affine/global/di';
|
||||
import type { JobMiddleware, Schema } from '@blocksuite/affine/store';
|
||||
import { DocCollection, Job } from '@blocksuite/affine/store';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
@@ -87,7 +88,7 @@ export async function getContentFromSlice(
|
||||
processTextInSnapshot(snapshot, host);
|
||||
const adapter =
|
||||
type === 'markdown'
|
||||
? new MarkdownAdapter(job)
|
||||
? new MarkdownAdapter(job, host.std.provider)
|
||||
: new PlainTextAdapter(job, host.std.provider);
|
||||
const content = await adapter.fromSliceSnapshot({
|
||||
snapshot,
|
||||
@@ -122,7 +123,7 @@ export const markdownToSnapshot = async (
|
||||
collection: host.std.doc.collection,
|
||||
middlewares: [defaultImageProxyMiddleware, pasteMiddleware(host.std)],
|
||||
});
|
||||
const markdownAdapter = new MixTextAdapter(job);
|
||||
const markdownAdapter = new MixTextAdapter(job, host.std.provider);
|
||||
const { blockVersions, workspaceVersion, pageVersion } =
|
||||
host.std.doc.collection.meta;
|
||||
if (!blockVersions || !workspaceVersion || !pageVersion)
|
||||
@@ -188,6 +189,7 @@ export async function replaceFromMarkdown(
|
||||
}
|
||||
|
||||
export async function markDownToDoc(
|
||||
provider: ServiceProvider,
|
||||
schema: Schema,
|
||||
answer: string,
|
||||
additionalMiddlewares?: JobMiddleware[]
|
||||
@@ -205,7 +207,7 @@ export async function markDownToDoc(
|
||||
collection,
|
||||
middlewares,
|
||||
});
|
||||
const mdAdapter = new MarkdownAdapter(job);
|
||||
const mdAdapter = new MarkdownAdapter(job, provider);
|
||||
const doc = await mdAdapter.toDoc({
|
||||
file: answer,
|
||||
assets: job.assetsManager,
|
||||
|
||||
@@ -64,7 +64,7 @@ export const createMindmapExecuteRenderer: (
|
||||
}
|
||||
|
||||
ctx.set({
|
||||
node: markdownToMindmap(answer, host.doc),
|
||||
node: markdownToMindmap(answer, host.doc, host.std.provider),
|
||||
});
|
||||
|
||||
handler(host, ctx);
|
||||
|
||||
@@ -143,7 +143,11 @@ export const copyTextAnswer = async (panel: AffineAIPanelWidget) => {
|
||||
};
|
||||
|
||||
export const copyText = async (host: EditorHost, text: string) => {
|
||||
const previewDoc = await markDownToDoc(host.std.doc.schema, text);
|
||||
const previewDoc = await markDownToDoc(
|
||||
host.std.provider,
|
||||
host.std.doc.schema,
|
||||
text
|
||||
);
|
||||
const models = previewDoc
|
||||
.getBlocksByFlavour('affine:note')
|
||||
.map(b => b.model)
|
||||
|
||||
@@ -5,7 +5,13 @@ import type {
|
||||
EmbedBlockModel,
|
||||
ImageBlockModel,
|
||||
} from '@blocksuite/affine/blocks';
|
||||
import { MarkdownAdapter } from '@blocksuite/affine/blocks';
|
||||
import {
|
||||
defaultBlockMarkdownAdapterMatchers,
|
||||
inlineDeltaToMarkdownAdapterMatchers,
|
||||
MarkdownAdapter,
|
||||
markdownInlineToDeltaMatchers,
|
||||
} from '@blocksuite/affine/blocks';
|
||||
import { Container } from '@blocksuite/affine/global/di';
|
||||
import {
|
||||
createYProxy,
|
||||
DocCollection,
|
||||
@@ -174,11 +180,22 @@ function generateMarkdownPreviewBuilder(
|
||||
adapterConfigs.set('docLinkBaseUrl', baseUrl);
|
||||
};
|
||||
|
||||
const container = new Container();
|
||||
[
|
||||
...markdownInlineToDeltaMatchers,
|
||||
...defaultBlockMarkdownAdapterMatchers,
|
||||
...inlineDeltaToMarkdownAdapterMatchers,
|
||||
].forEach(ext => {
|
||||
ext.setup(container);
|
||||
});
|
||||
|
||||
const provider = container.provider();
|
||||
const markdownAdapter = new MarkdownAdapter(
|
||||
new Job({
|
||||
collection: markdownPreviewDocCollection,
|
||||
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
|
||||
})
|
||||
}),
|
||||
provider
|
||||
);
|
||||
|
||||
const markdownPreviewCache = new WeakMap<BlockDocumentInfo, string | null>();
|
||||
|
||||
Reference in New Issue
Block a user