mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-23 21:38:44 +08:00
a555df0200
Close [BS-3049](https://linear.app/affine-design/issue/BS-3049/chat引用的样式坏了) [BS-3024](https://linear.app/affine-design/issue/BS-3024/footnote-在容器边缘时,hover-抽搐)
53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
import { FootNoteReferenceParamsSchema } from '@blocksuite/affine-model';
|
|
import {
|
|
FOOTNOTE_DEFINITION_PREFIX,
|
|
MarkdownASTToDeltaExtension,
|
|
} from '@blocksuite/affine-shared/adapters';
|
|
|
|
export const markdownFootnoteReferenceToDeltaMatcher =
|
|
MarkdownASTToDeltaExtension({
|
|
name: 'footnote-reference',
|
|
match: ast => ast.type === 'footnoteReference',
|
|
toDelta: (ast, context) => {
|
|
if (ast.type !== 'footnoteReference') {
|
|
return [];
|
|
}
|
|
try {
|
|
const { configs } = context;
|
|
const footnoteDefinitionKey = `${FOOTNOTE_DEFINITION_PREFIX}${ast.identifier}`;
|
|
const footnoteDefinition = configs.get(footnoteDefinitionKey);
|
|
if (!footnoteDefinition) {
|
|
return [];
|
|
}
|
|
const footnoteDefinitionJson = JSON.parse(footnoteDefinition);
|
|
// If the footnote definition contains url, decode it
|
|
if (footnoteDefinitionJson.url) {
|
|
footnoteDefinitionJson.url = decodeURIComponent(
|
|
footnoteDefinitionJson.url
|
|
);
|
|
}
|
|
const footnoteReference = FootNoteReferenceParamsSchema.parse(
|
|
footnoteDefinitionJson
|
|
);
|
|
|
|
// If the footnote reference is an attachment, and the file type is not set,
|
|
// Try to infer the file type from the file name.
|
|
const { type: referenceType, fileName, fileType } = footnoteReference;
|
|
if (referenceType === 'attachment' && fileName && !fileType) {
|
|
const ext = fileName.split('.').pop()?.toLowerCase();
|
|
if (ext) {
|
|
footnoteReference.fileType = ext;
|
|
}
|
|
}
|
|
|
|
const footnote = {
|
|
label: ast.identifier,
|
|
reference: footnoteReference,
|
|
};
|
|
return [{ insert: ' ', attributes: { footnote } }];
|
|
} catch {
|
|
return [];
|
|
}
|
|
},
|
|
});
|