Skip to content

Commit

Permalink
fix: AST rendered wrapping node
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdabbs committed Nov 15, 2023
1 parent 22211ae commit c76e069
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
39 changes: 10 additions & 29 deletions packages/core/src/Parser/unnest.ts
Original file line number Diff line number Diff line change
@@ -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 <p><singleton/></p>. This transformer removes the wrapping
* <p/> node
*
* root:
* children:
* - type: singleton
*
* to allow for inline-only <singleton/> 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> = 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<Root> = 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'
}
}

Expand Down
16 changes: 15 additions & 1 deletion packages/core/test/Parser.test.ts
Original file line number Diff line number Diff line change
@@ -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]) {
Expand Down Expand Up @@ -89,3 +89,17 @@ it.todo('expands math in linked titles', async () => {
'<a href="" title="$S_0$" class="internal-link"><span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>S</mi><mn>0</mn></msub></mrow><annotation encoding="application/x-tex">S_0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.05764em;">S</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.0576em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">0</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></a>',
)
})

describe('unwrapping', () => {
it('unwraps math', () => {
expect(parse('$\\sigma$')).resolves.toEqual(
'<span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>σ</mi></mrow><annotation encoding="application/x-tex">\\sigma</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">σ</span></span></span></span></span>',
)
})

it('unwraps mixed math and text', () => {
expect(parse('$\\sigma$-locally finite')).resolves.toEqual(
'<span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>σ</mi></mrow><annotation encoding="application/x-tex">\\sigma</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">σ</span></span></span></span></span>-locally finite',
)
})
})
2 changes: 1 addition & 1 deletion packages/viewer/src/components/Shared/Formula.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import Atom from './Formula/Atom.svelte'
import Compound from './Formula/Compound.svelte'
export let value: Formula<Property, null>
export let value: Formula<Property>
export let link = true
</script>

Expand Down
2 changes: 1 addition & 1 deletion packages/viewer/src/components/Shared/Formula/Atom.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Link, Typeset } from '@/components/Shared'
import type { Property } from '@/models'
export let value: Atom<Property, null>
export let value: Atom<Property>
export let link: boolean = true
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Formula from '../Formula.svelte'
export let value: And<Property, null> | Or<Property, null>
export let value: And<Property> | Or<Property>
export let link = true
$: connector = value.kind === 'and' ? '' : ''
Expand Down

0 comments on commit c76e069

Please sign in to comment.