diff --git a/src/replacements/gcn-circular/index.ts b/src/replacements/gcn-circular/index.ts index dc0d013..222d16e 100644 --- a/src/replacements/gcn-circular/index.ts +++ b/src/replacements/gcn-circular/index.ts @@ -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)) { diff --git a/src/replacements/gcn-circular/test.html b/src/replacements/gcn-circular/test.html index 45b24fb..990277c 100644 --- a/src/replacements/gcn-circular/test.html +++ b/src/replacements/gcn-circular/test.html @@ -1,4 +1,6 @@

Hello, world

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

\ No newline at end of file diff --git a/src/replacements/gcn-circular/test.json b/src/replacements/gcn-circular/test.json index 964e7ef..72b549e 100644 --- a/src/replacements/gcn-circular/test.json +++ b/src/replacements/gcn-circular/test.json @@ -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": [ diff --git a/src/replacements/gcn-circular/test.md b/src/replacements/gcn-circular/test.md index 8646b70..feeb040 100644 --- a/src/replacements/gcn-circular/test.md +++ b/src/replacements/gcn-circular/test.md @@ -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