Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse GCN Circular URLs #18

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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