Skip to content

Commit

Permalink
Parse GCN Circular URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsinger committed Sep 19, 2023
1 parent 9618683 commit a6cbde4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/replacements/gcn-circular/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ const preamble = /GCN(?:s|\s+Circulars?|\s+Circs?\.)?\s*/.source
const conjunction = /(?:\s*|,|and)+/.source
const circularIdRegExp = /\d+(?:a|\.5)?/gi
const circularId = circularIdRegExp.source
const legacyUrlOrigin = /https?:\/\/gcn\.gsfc\.nasa\.gov/.source
const legacyUrlExtension = /\.gcn3/.source
const legacyUrl = `${legacyUrlOrigin}/gcn3/(${circularId})${legacyUrlExtension}`
const urlOrigin = /https?:\/\/gcn.nasa.gov/.source
const url = `${urlOrigin}/circulars/(${circularId})`

export default {
find: new RegExp(
`${preamble}${circularId}(?:${conjunction}${circularId})*`,
`${url}|${legacyUrl}|${preamble}${circularId}(?:${conjunction}${circularId})*`,
'gi'
),
replace(data, value) {
replace(data, value, circularIdFromUrl, circularIdFromLegacyUrl) {
circularIdFromUrl ||= circularIdFromLegacyUrl
if (circularIdFromUrl)
return data('gcn-circular', value, normalize(circularIdFromUrl))

const result = []
let lastIndex = 0
for (const match of value.matchAll(circularIdRegExp)) {
Expand Down
2 changes: 2 additions & 0 deletions src/replacements/gcn-circular/test.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<h1>Hello, world</h1>
<p>This paragraph refers to GCN Circular <data class="gcn-circular" value="123">123</data> and GCN <data class="gcn-circular" value="456">456</data></p>
<p>You can refer to a Circular by its URL: <data class="gcn-circular" value="26640">https://gcn.nasa.gov/circulars/26640</data></p>
<p>A legacy URL works too: <data class="gcn-circular" value="26640">https://gcn.gsfc.nasa.gov/gcn3/26640.gcn3</data></p>
<p>GCN Circ. <data class="gcn-circular" value="789">789</data> and GCN Circs. <data class="gcn-circular" value="3">3</data> and <data class="gcn-circular" value="4">4</data></p>
<p>And this one refers to GCNs <data class="gcn-circular" value="1">1</data>, <data class="gcn-circular" value="23.5">23a</data>, <data class="gcn-circular" value="45.5">45.5</data> and <data class="gcn-circular" value="42">42</data></p>
34 changes: 34 additions & 0 deletions src/replacements/gcn-circular/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@
}
]
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "You can refer to a Circular by its URL: "
},
{
"type": "text",
"value": "https://gcn.nasa.gov/circulars/26640",
"data": {
"class": "gcn-circular",
"value": 26640
}
}
]
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "A legacy URL works too: "
},
{
"type": "text",
"value": "https://gcn.gsfc.nasa.gov/gcn3/26640.gcn3",
"data": {
"class": "gcn-circular",
"value": 26640
}
}
]
},
{
"type": "paragraph",
"children": [
Expand Down
4 changes: 4 additions & 0 deletions src/replacements/gcn-circular/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This paragraph refers to GCN Circular 123 and GCN 456

You can refer to a Circular by its URL: https://gcn.nasa.gov/circulars/26640

A legacy URL works too: https://gcn.gsfc.nasa.gov/gcn3/26640.gcn3

GCN Circ. 789 and GCN Circs. 3 and 4

And this one refers to GCNs 1, 23a, 45.5 and 42

0 comments on commit a6cbde4

Please sign in to comment.