Skip to content

Commit

Permalink
External markdown enhancements (#193)
Browse files Browse the repository at this point in the history
* Added names to embedded spaces, proprties, and theorems

* Update packages/viewer/src/components/Shared/ExtMarkdown.svelte

Co-authored-by: Steven Clontz <[email protected]>

* fix typo

* Added support for traits

* Update packages/viewer/src/components/Shared/ExtMarkdown.svelte

Co-authored-by: Steven Clontz <[email protected]>

---------

Co-authored-by: Steven Clontz <[email protected]>
  • Loading branch information
Not-Abram and StevenClontz authored Nov 22, 2024
1 parent 0c340a5 commit 9ae41f2
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions packages/viewer/src/components/Shared/ExtMarkdown.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<script lang="ts">
import { Ref } from '@pi-base/core'
import context from '@/context'
const { spaces, theorems, properties, traits } = context()
export let source: string
const modify = (s: string) => {
const singlecurly = /{(?<type>[SPT])(?<id>\d+)}/g
const doublecurlies = /{{(?<prefix>\w+):(?<id>[^{]+)}}/g
const internal_trait = /{S(?<spaceID>\d+)\|P(?<propID>\d+)}/g
const internal_SPT = /{(?<type>[SPT])(?<id>\d+)}/g
const external = /{{(?<prefix>\w+):(?<id>[^{]+)}}/g
// Replace double curly braces
s = s.replace(doublecurlies, (_, prefix, id) => {
s = s.replace(external, (_, prefix, id) => {
// Perform replacement based on prefix and id
const ref = Ref.format({
kind: prefix,
Expand All @@ -16,15 +19,33 @@
return `[${ref.title}](${ref.href})`
})
// Replace single curly braces
s = s.replace(singlecurly, (_, type, id) => {
// Replace single curly braces - check traits, then space/property/theorem
// Reuses logic from https://github.com/pi-base/web/blob/0c340a5e5c400047acd21acc1ea7a3f75c06733d/packages/viewer/src/parser/internalLinks.ts
// TODO: replace duplicate code
s = s.replace(internal_trait, (_, sid, pid) => {
const space = $spaces.find(Number(sid))
const property = $properties.find(Number(pid))
const trait = space && property && $traits.find(space, property)
const connector =
trait?.value === true ? 'is' : trait?.value === false ? 'is not' : '?'
return `[S${sid}|P${pid}: ${
space ? space.name : 'S' + sid
} ${connector} ${
property ? property.name : 'P' + pid
}](https://topology.pi-base.org/spaces/S${sid}/properties/P${pid})`
})
s = s.replace(internal_SPT, (_, type, id) => {
// Perform replacement based on type and id
if (type === 'S') {
return `[S${id}](https://topology.pi-base.org/spaces/S${id})`
const name = $spaces.find(`S${id}`)?.name
return `[S${id}: ${name}](https://topology.pi-base.org/spaces/S${id})`
} else if (type === 'P') {
return `[P${id}](https://topology.pi-base.org/properties/P${id})`
const name = $properties.find(`P${id}`)?.name
return `[P${id}: ${name}](https://topology.pi-base.org/properties/P${id})`
} else {
return `[T${id}](https://topology.pi-base.org/theorems/T${id})`
const name = $theorems.find(`T${id}`)?.name
return `[T${id}: ${name}](https://topology.pi-base.org/theorems/T${id})`
}
})
Expand Down

0 comments on commit 9ae41f2

Please sign in to comment.