Skip to content

Commit 22211ae

Browse files
authored
feat: support {S#|P#} syntax for linking to traits (#78)
fixes #66
1 parent 78ab5bf commit 22211ae

File tree

7 files changed

+66
-6
lines changed

7 files changed

+66
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { deduce, setup } from '../support'
2+
3+
beforeEach(setup)
4+
5+
it('renders internal links', () => {
6+
cy.visit('dev/preview')
7+
deduce()
8+
9+
cy.get('[data-testid=input]').type(
10+
// {{} is Cypress escaping for {. See https://docs.cypress.io/api/commands/type#Arguments
11+
`{{}S000001} is {{}P000001} as noted in {{}S000001|P000001}`,
12+
)
13+
14+
cy.get('[data-testid=output]').contains(
15+
'Discrete topology on a two-point set is $T_0$ as noted in Discrete topology on a two-point set | $T_0$',
16+
)
17+
})

packages/viewer/src/components/Dev/Example.svelte

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This is a list of links
88
* {S000123}
99
* {P000123}
1010
* {T000123}
11+
* {S000123|P000123}
1112
* {{doi:123}}
1213
* {{mr:123}}
1314
* {{wikipedia:123}}
@@ -37,6 +38,9 @@ This is a list of links
3738
- e.g.
3839
<code>{'{S000001}'}</code>
3940
for space 1
41+
<br />
42+
<code>{'{S000001|P000002}'}</code>
43+
for space 1's value of property 2
4044
</td>
4145
</tr>
4246
<tr>

packages/viewer/src/components/Dev/Preview.svelte

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
<div class="row">
1212
<div class="col-sm">
1313
<div class="input-group">
14-
<textarea bind:value={body} class="form-control" {rows} />
14+
<textarea
15+
bind:value={body}
16+
class="form-control"
17+
data-testid="input"
18+
{rows}
19+
/>
1520
</div>
1621
<div class="input-group">
1722
<div class="form-check">
@@ -31,7 +36,7 @@
3136
}}
3237
/>
3338
</div>
34-
<div class="col-sm">
39+
<div class="col-sm" data-testid="output">
3540
<Typeset {body} {truncated} />
3641
</div>
3742
</div>

packages/viewer/src/components/Shared/Typeset.svelte

+12-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,18 @@
1515
return
1616
}
1717
18-
container.innerHTML = await $typeset(text, truncated_)
18+
try {
19+
container.innerHTML = await $typeset(text, truncated_)
20+
} catch (e: any) {
21+
// FIXME: this is a kludgey fix for the fact that the {id} parser throws
22+
// assertion errors on incomplete / unbalanced sets of {}s. The better fix
23+
// is to make it more robust.
24+
if (e?.name === 'Assertion') {
25+
console.warn(e)
26+
} else {
27+
throw e
28+
}
29+
}
1930
2031
/**
2132
* We're adding "real" <a/> tags to the DOM, even for parsed internal

packages/viewer/src/parser/internalLinks.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@ describe('with ambient data', () => {
5555
expect(link(['S', '000003'])).toEqual('Could not find Space S000003')
5656
})
5757
})
58+
59+
describe('traits', () => {
60+
it('can link to traits', () => {
61+
expect(link(['S', '000001|P000002'])).toEqual({
62+
href: '/spaces/S000001/properties/P000002',
63+
title: 'S000001 | Two',
64+
})
65+
})
66+
})
5867
})

packages/viewer/src/parser/internalLinks.ts

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ export function internal(
99
return function linker([kind, id]: ['S' | 'P' | 'T', string]) {
1010
switch (kind) {
1111
case 'S':
12+
// Support e.g. {S001|P002} as a link to the trait
13+
const match = /(?<sid>\d+)\|P(?<pid>\d+)/.exec(id)
14+
if (match?.groups) {
15+
const { sid, pid } = match.groups
16+
const space = spaces.find(Number(sid))
17+
const property = properties.find(Number(pid))
18+
return {
19+
href: `/spaces/S${sid}/properties/P${pid}`,
20+
title: `${space ? space.name : 'S' + sid} | ${
21+
property ? property.name : 'P' + pid
22+
}`,
23+
}
24+
}
25+
1226
const space = spaces.find(Number(id))
1327
return {
1428
href: `/spaces/S${id}`,

packages/viewer/vite.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export default defineConfig({
77
test: {
88
include: ['src/**/*.{test,spec}.{js,ts}'],
99
coverage: {
10-
lines: 80.01,
11-
branches: 87.97,
12-
statements: 80.01,
10+
lines: 80.25,
11+
branches: 87.16,
12+
statements: 80.25,
1313
functions: 81,
1414
skipFull: true,
1515
thresholdAutoUpdate: true,

0 commit comments

Comments
 (0)