Skip to content

Commit

Permalink
feat(blocks): add plain text block adapter for latex
Browse files Browse the repository at this point in the history
  • Loading branch information
donteatfriedrice committed Dec 3, 2024
1 parent 12293a7 commit 895ba19
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
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';

0 comments on commit 895ba19

Please sign in to comment.