mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-11 20:08:37 +00:00
fix(editor): markdown html and image import (#11712)
Close [BS-3145](https://linear.app/affine-design/issue/BS-3145/markdown-adapter-html-标签导入成-code-block) [BS-3154](https://linear.app/affine-design/issue/BS-3154/[bug]-使用-markdown-with-files-导入到-affine-图片丢失)
This commit is contained in:
@@ -3,25 +3,51 @@ import {
|
||||
BlockMarkdownAdapterExtension,
|
||||
type BlockMarkdownAdapterMatcher,
|
||||
CODE_BLOCK_WRAP_KEY,
|
||||
IN_PARAGRAPH_NODE_CONTEXT_KEY,
|
||||
type MarkdownAST,
|
||||
} from '@blocksuite/affine-shared/adapters';
|
||||
import type { DeltaInsert } from '@blocksuite/store';
|
||||
import { nanoid } from '@blocksuite/store';
|
||||
import type { Code } from 'mdast';
|
||||
import type { Code, Html } from 'mdast';
|
||||
|
||||
const isCodeNode = (node: MarkdownAST): node is Code => node.type === 'code';
|
||||
const isHtmlNode = (node: MarkdownAST): node is Html => node.type === 'html';
|
||||
|
||||
const isCodeOrHtmlNode = (node: MarkdownAST): node is Code | Html =>
|
||||
isCodeNode(node) || isHtmlNode(node);
|
||||
|
||||
export const codeBlockMarkdownAdapterMatcher: BlockMarkdownAdapterMatcher = {
|
||||
flavour: CodeBlockSchema.model.flavour,
|
||||
toMatch: o => isCodeNode(o.node),
|
||||
toMatch: o => isCodeOrHtmlNode(o.node),
|
||||
fromMatch: o => o.node.flavour === 'affine:code',
|
||||
toBlockSnapshot: {
|
||||
enter: (o, context) => {
|
||||
if (!isCodeNode(o.node)) {
|
||||
if (!isCodeOrHtmlNode(o.node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { walkerContext, configs } = context;
|
||||
const wrap = configs.get(CODE_BLOCK_WRAP_KEY) === 'true';
|
||||
let language = 'plain text';
|
||||
switch (o.node.type) {
|
||||
case 'code': {
|
||||
if (o.node.lang) {
|
||||
language = o.node.lang;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'html': {
|
||||
const inParagraphNode = !!walkerContext.getGlobalContext(
|
||||
IN_PARAGRAPH_NODE_CONTEXT_KEY
|
||||
);
|
||||
// only handle top level html node
|
||||
if (inParagraphNode) {
|
||||
return;
|
||||
}
|
||||
language = 'html';
|
||||
break;
|
||||
}
|
||||
}
|
||||
walkerContext
|
||||
.openNode(
|
||||
{
|
||||
@@ -29,7 +55,7 @@ export const codeBlockMarkdownAdapterMatcher: BlockMarkdownAdapterMatcher = {
|
||||
id: nanoid(),
|
||||
flavour: 'affine:code',
|
||||
props: {
|
||||
language: o.node.lang ?? 'Plain Text',
|
||||
language,
|
||||
wrap,
|
||||
text: {
|
||||
'$blocksuite:internal:text$': true,
|
||||
|
||||
Reference in New Issue
Block a user