Skip to content

Commit

Permalink
feat: support {S#|P#} syntax for linking to traits
Browse files Browse the repository at this point in the history
fixes #66
  • Loading branch information
jamesdabbs committed Nov 7, 2023
1 parent 2f510a5 commit f896960
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/viewer/cypress/e2e/typesetting.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { deduce, setup } from '../support'

beforeEach(setup)

it('renders internal links', () => {
cy.visit('dev/preview')
deduce()

cy.get('[data-testid=input]').type(
// {{} is Cypress escaping for {. See https://docs.cypress.io/api/commands/type#Arguments
`{{}S000001} is {{}P000001} as noted in {{}S000001|P000001}`,
)

cy.get('[data-testid=output]').contains(
'Discrete topology on a two-point set is $T_0$ as noted in Discrete topology on a two-point set | $T_0$',
)
})
9 changes: 7 additions & 2 deletions packages/viewer/src/components/Dev/Preview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
<div class="row">
<div class="col-sm">
<div class="input-group">
<textarea bind:value={body} class="form-control" {rows} />
<textarea
bind:value={body}
class="form-control"
data-testid="input"
{rows}
/>
</div>
<div class="input-group">
<div class="form-check">
Expand All @@ -31,7 +36,7 @@
}}
/>
</div>
<div class="col-sm">
<div class="col-sm" data-testid="output">
<Typeset {body} {truncated} />
</div>
</div>
13 changes: 12 additions & 1 deletion packages/viewer/src/components/Shared/Typeset.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@
return
}
container.innerHTML = await $typeset(text, truncated_)
try {
container.innerHTML = await $typeset(text, truncated_)
} catch (e: any) {
// FIXME: this is a kludgey fix for the fact that the {id} parser throws
// assertion errors on incomplete / unbalanced sets of {}s. The better fix
// is to make it more robust.
if (e?.name === 'Assertion') {
console.warn(e)
} else {
throw e
}
}
/**
* We're adding "real" <a/> tags to the DOM, even for parsed internal
Expand Down
14 changes: 14 additions & 0 deletions packages/viewer/src/parser/internalLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ export function internal(
return function linker([kind, id]: ['S' | 'P' | 'T', string]) {
switch (kind) {
case 'S':
// Support e.g. {S001|P002} as a link to the trait
const match = /(?<sid>\d+)\|P(?<pid>\d+)/.exec(id)
if (match?.groups) {
const { sid, pid } = match.groups
const space = spaces.find(Number(sid))
const property = properties.find(Number(pid))
return {
href: `/spaces/S${sid}/properties/P${pid}`,
title: `${space ? space.name : 'S' + sid} | ${
property ? property.name : 'P' + pid
}`,
}
}

const space = spaces.find(Number(id))
return {
href: `/spaces/S${id}`,
Expand Down

0 comments on commit f896960

Please sign in to comment.