diff --git a/packages/core/src/Parser.ts b/packages/core/src/Parser.ts index ff4a6a69..55b2f536 100644 --- a/packages/core/src/Parser.ts +++ b/packages/core/src/Parser.ts @@ -47,14 +47,14 @@ export function parser({ // Convert {internal} and {{external}} mdast nodes to standard mdast link // nodes, using passed down linkers to generate href and title .use(references(link)) - // Automatically remove outermost paragraph wrapper - .use(unnest) // Parse $ and $$ blocks in text as math .use(remarkMath) // Convert standard mdast nodes to hast .use(remarkRehype) // Convert math nodes to hast .use(rehypeKatex) + // Automatically remove outermost paragraph wrapper + .use(unnest) // Optionally trim mdast to a minimal preview .use(truncator, { maxChars: 100, @@ -70,7 +70,7 @@ export function parser({ * Utility plugin. Use noOp(true) to log the current syntax tree at this point * in the chain. */ -function noOp(log = false) { +function noOp({ log = false } = {}) { return (tree: Node) => { if (log) { console.log(tree) diff --git a/packages/core/src/Parser/unnest.ts b/packages/core/src/Parser/unnest.ts index 5c693119..673e5a3a 100644 --- a/packages/core/src/Parser/unnest.ts +++ b/packages/core/src/Parser/unnest.ts @@ -1,37 +1,18 @@ -import { Node } from 'unist' +import { Root } from 'hast' import { Transformer } from 'unified' -type Tree = Node & { children?: Tree[] } - /** - * Many default parsers create an AST that looks like - * - * root: - * children: - * - type: paragraph - * children: - * - type: singleton - * - * which renders as

. This transformer removes the wrapping - *

node - * - * root: - * children: - * - type: singleton - * - * to allow for inline-only elements. + * Simplifies outer wrapping nodes of ASTs, and + * ensures that they render an inline / non-block + * elvel elements. */ export const unnest = () => { - const transformer: Transformer = tree => { - if ( - tree && - tree.children?.length === 1 && - tree.children[0].type === 'paragraph' && - tree.children[0].children?.length === 1 - ) { - return { - ...tree, - children: tree.children[0].children, + const transformer: Transformer = tree => { + if (tree && tree.children?.length === 1 && 'tagName' in tree.children[0]) { + tree.children = tree.children[0].children + + if ('tagName' in tree.children[0] && tree.children[0].tagName === 'p') { + tree.children[0].tagName = 'span' } } diff --git a/packages/core/test/Parser.test.ts b/packages/core/test/Parser.test.ts index b3728a2c..4adc3f9c 100644 --- a/packages/core/test/Parser.test.ts +++ b/packages/core/test/Parser.test.ts @@ -1,4 +1,4 @@ -import { expect, it } from 'vitest' +import { describe, expect, it } from 'vitest' import { parser } from '../src/Parser' function link([kind, id]: [unknown, unknown]) { @@ -89,3 +89,17 @@ it.todo('expands math in linked titles', async () => { 'S0S_0', ) }) + +describe('unwrapping', () => { + it('unwraps math', () => { + expect(parse('$\\sigma$')).resolves.toEqual( + 'σ\\sigma', + ) + }) + + it('unwraps mixed math and text', () => { + expect(parse('$\\sigma$-locally finite')).resolves.toEqual( + 'σ\\sigma-locally finite', + ) + }) +}) diff --git a/packages/viewer/src/components/Shared/Formula.svelte b/packages/viewer/src/components/Shared/Formula.svelte index 6d6a12f2..1279dd96 100644 --- a/packages/viewer/src/components/Shared/Formula.svelte +++ b/packages/viewer/src/components/Shared/Formula.svelte @@ -4,7 +4,7 @@ import Atom from './Formula/Atom.svelte' import Compound from './Formula/Compound.svelte' - export let value: Formula + export let value: Formula export let link = true diff --git a/packages/viewer/src/components/Shared/Formula/Atom.svelte b/packages/viewer/src/components/Shared/Formula/Atom.svelte index c5fcf1f9..ab12100a 100644 --- a/packages/viewer/src/components/Shared/Formula/Atom.svelte +++ b/packages/viewer/src/components/Shared/Formula/Atom.svelte @@ -4,7 +4,7 @@ import { Link, Typeset } from '@/components/Shared' import type { Property } from '@/models' - export let value: Atom + export let value: Atom export let link: boolean = true diff --git a/packages/viewer/src/components/Shared/Formula/Compound.svelte b/packages/viewer/src/components/Shared/Formula/Compound.svelte index 8ca8c555..184da754 100644 --- a/packages/viewer/src/components/Shared/Formula/Compound.svelte +++ b/packages/viewer/src/components/Shared/Formula/Compound.svelte @@ -5,7 +5,7 @@ import Formula from '../Formula.svelte' - export let value: And | Or + export let value: And | Or export let link = true $: connector = value.kind === 'and' ? '∧' : '∨'