Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blocks): add plain text block adapter for latex #8845

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion packages/blocks/src/__tests__/adapters/plain-text.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ describe('snapshot to plain text', () => {
const plainTextAdapter = new PlainTextAdapter(createJob([middleware]));

const plainText =
'aaa: https://affine.pro/\ntest: https://example.com/deadbeef?mode=page&blockIds=abc%2C123&elementIds=def%2C456\nLaTex, with value: E=mc^2\n';
'aaa: https://affine.pro/\ntest: https://example.com/deadbeef?mode=page&blockIds=abc%2C123&elementIds=def%2C456\nE=mc^2\n';
const target = await plainTextAdapter.fromBlockSnapshot({
snapshot: blockSnapshot,
});
Expand Down Expand Up @@ -672,4 +672,37 @@ describe('snapshot to plain text', () => {
});
}
});

test('latex block', async () => {
const blockSnapshot: BlockSnapshot = {
type: 'block',
id: 'matchesReplaceMap[0]',
flavour: 'affine:note',
props: {
xywh: '[0,0,800,95]',
background: DEFAULT_NOTE_BACKGROUND_COLOR,
index: 'a0',
hidden: false,
displayMode: NoteDisplayMode.DocAndEdgeless,
},
children: [
{
type: 'block',
id: 'matchesReplaceMap[1]',
flavour: 'affine:latex',
props: {
latex: 'E=mc^2',
},
children: [],
},
],
};

const plainText = 'LaTex, with value: E=mc^2\n';
const plainTextAdapter = new PlainTextAdapter(createJob());
const target = await plainTextAdapter.fromBlockSnapshot({
snapshot: blockSnapshot,
});
expect(target.file).toBe(plainText);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import {
DividerBlockPlainTextAdapterExtension,
dividerBlockPlainTextAdapterMatcher,
} from '../../../divider-block/adapters/plain-text.js';
import {
LatexBlockPlainTextAdapterExtension,
latexBlockPlainTextAdapterMatcher,
} from '../../../latex-block/adapters/plain-text.js';

export const defaultBlockPlainTextAdapterMatchers: BlockPlainTextAdapterMatcher[] =
[
Expand All @@ -44,6 +48,7 @@ export const defaultBlockPlainTextAdapterMatchers: BlockPlainTextAdapterMatcher[
embedGithubBlockPlainTextAdapterMatcher,
embedLoomBlockPlainTextAdapterMatcher,
embedYoutubeBlockPlainTextAdapterMatcher,
latexBlockPlainTextAdapterMatcher,
];

export const BlockPlainTextAdapterExtensions: ExtensionType[] = [
Expand All @@ -56,4 +61,5 @@ export const BlockPlainTextAdapterExtensions: ExtensionType[] = [
EmbedGithubBlockPlainTextAdapterExtension,
EmbedLoomBlockPlainTextAdapterExtension,
EmbedYoutubeBlockPlainTextAdapterExtension,
LatexBlockPlainTextAdapterExtension,
];
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const latexDeltaMarkdownAdapterMatch: InlineDeltaToPlainTextAdapterMatche
return node;
}
return {
content: `LaTex, with value: ${delta.attributes?.latex}`,
content: delta.attributes?.latex,
};
},
};
Expand Down
2 changes: 2 additions & 0 deletions packages/blocks/src/latex-block/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './markdown.js';
export * from './plain-text.js';
29 changes: 29 additions & 0 deletions packages/blocks/src/latex-block/adapters/plain-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { LatexBlockSchema } from '@blocksuite/affine-model';
import {
BlockPlainTextAdapterExtension,
type BlockPlainTextAdapterMatcher,
} from '@blocksuite/affine-shared/adapters';

const latexPrefix = 'LaTex, with value: ';

export const latexBlockPlainTextAdapterMatcher: BlockPlainTextAdapterMatcher = {
flavour: LatexBlockSchema.model.flavour,
toMatch: () => false,
fromMatch: o => o.node.flavour === LatexBlockSchema.model.flavour,
toBlockSnapshot: {},
fromBlockSnapshot: {
enter: (o, context) => {
const latex =
'latex' in o.node.props ? (o.node.props.latex as string) : '';

const { textBuffer } = context;
if (latex) {
textBuffer.content += `${latexPrefix}${latex}`;
textBuffer.content += '\n';
}
},
},
};

export const LatexBlockPlainTextAdapterExtension =
BlockPlainTextAdapterExtension(latexBlockPlainTextAdapterMatcher);
2 changes: 1 addition & 1 deletion packages/blocks/src/latex-block/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './adapters/markdown.js';
export * from './adapters/index.js';
export * from './latex-block.js';
export * from './latex-spec.js';
Loading