Skip to content

Commit ff49e5a

Browse files
authored
Merge pull request #13 from lpsinger/gcn-circulars-crossreferences
Refactor/rewrite to add GCN Circulars cross references
2 parents 497dd74 + a910e90 commit ff49e5a

21 files changed

+7189
-9034
lines changed

.github/workflows/node.js.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ jobs:
2020
node-version: ${{ matrix.node-version }}
2121
- name: NPM Install
2222
run: npm ci
23-
# - name: Run eslint linter
24-
# run: npx eslint --max-warnings 0 .
23+
- name: Run eslint linter
24+
run: npx eslint --max-warnings 0 .
2525
- name: Run Prettier code style checks
2626
run: npx prettier -c .
2727
- name: Run unit tests

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node_modules
22
/coverage
33
/*.js
4-
/*.d.ts
4+
*.d.ts
55
.eslintcache
66
*.tgz

.vscode/settings.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"editor.tabSize": 2,
3+
"javascript.format.semicolons": "remove",
4+
"typescript.preferences.quoteStyle": "single",
5+
"javascript.preferences.quoteStyle": "single",
6+
"typescript.format.semicolons": "remove"
7+
}

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ Astro Flavored Markdown supports the following types.
2626
## datetime
2727

2828
A UTC date with an optional time, normalized to [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601).
29+
30+
## gcn-circular
31+
32+
A reference to a GCN Circular.

package-lock.json

+6,547-8,930
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+29-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
"version": "0.0.1",
44
"description": "An mdast plugin for Astro Flavored Markdown",
55
"main": "index.js",
6+
"files": [
7+
"types/*.d.ts"
8+
],
9+
"types": "types/index.d.ts",
610
"scripts": {
711
"prepare:husky": "husky install",
12+
"prepare:esbuild": "esbuild src/index.ts --bundle --packages=external --platform=neutral --outfile=index.js",
813
"prepare:tsc": "tsc",
914
"prepare": "run-p prepare:*",
1015
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest"
@@ -29,37 +34,55 @@
2934
"mdast-util-find-and-replace": "^3.0.0"
3035
},
3136
"devDependencies": {
32-
"@tsconfig/node14": "^1.0.3",
37+
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
38+
"@tsconfig/node16": "^16.1.1",
3339
"@types/jest": "^29.2.5",
3440
"@types/mdast": "^4.0.0",
3541
"@typescript-eslint/eslint-plugin": "^5.48.1",
3642
"@typescript-eslint/parser": "^5.48.1",
3743
"cross-env": "^7.0.3",
44+
"esbuild": "^0.19.2",
3845
"eslint": "^8.31.0",
3946
"eslint-config-prettier": "^8.6.0",
4047
"eslint-plugin-jest": "^27.2.1",
48+
"glob": "^10.3.4",
4149
"husky": "^8.0.3",
42-
"jest": "^29.3.1",
50+
"jest": "^29.6.4",
4351
"lint-staged": "^13.1.0",
4452
"mdast-util-from-markdown": "^2.0.0",
4553
"npm-run-all": "^4.1.5",
4654
"prettier": "^2.8.2",
47-
"ts-jest": "^29.0.4",
48-
"typescript": "^4.9.4"
55+
"ts-dedent": "^2.2.0",
56+
"ts-jest": "^29.1.1",
57+
"typescript": "^5.2.2"
4958
},
5059
"type": "module",
5160
"sideEffects": false,
5261
"prettier": {
62+
"plugins": [
63+
"@trivago/prettier-plugin-sort-imports"
64+
],
65+
"importOrder": [
66+
"^[@a-zA-Z].*(?<!(?:css|gif|json|png|svg))$",
67+
"^[.~/].*(?<!(?:css|gif|json|png|svg))$",
68+
""
69+
],
70+
"importOrderSeparation": true,
71+
"importOrderSortSpecifiers": true,
5372
"semi": false,
54-
"singleQuote": true
73+
"singleQuote": true,
74+
"trailingComma": "es5"
5575
},
5676
"jest": {
5777
"preset": "ts-jest/presets/default-esm",
5878
"collectCoverage": true,
5979
"coverageReporters": [
6080
"text",
6181
"cobertura"
62-
]
82+
],
83+
"moduleNameMapper": {
84+
"^(\\.{1,2}/.*)\\.js$": "$1"
85+
}
6386
},
6487
"eslintConfig": {
6588
"env": {

src/index.ts

+6-45
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,11 @@
1-
import type { Nodes, Text } from 'mdast'
2-
import { type Find, findAndReplace } from 'mdast-util-find-and-replace'
1+
import type { Nodes } from 'mdast'
2+
import { findAndReplace } from 'mdast-util-find-and-replace'
33

4-
type AstroData = {
5-
/** Astro Flavored Markdown data type */
6-
type: string
7-
/** Normalized value */
8-
value: string
9-
}
10-
11-
export interface AstroText extends Text {
12-
data: {
13-
astromd: AstroData
14-
}
15-
}
16-
17-
interface VisitorSpec {
18-
/** Name of the Astro Flavored Markdown type. */
19-
type: string
20-
/** Regular expression to search for. */
21-
pattern: Find
22-
/** Replacement function to generate normalized value.
23-
* The function should be suitable for passing to String.replace().
24-
*/
25-
replacement: (value: string, ...groups: string[]) => string
26-
}
4+
import replacements from './replacements/index.js'
275

28-
const visitorSpecs: VisitorSpec[] = [
29-
{
30-
type: 'datetime',
31-
pattern:
32-
/(\d{4}-\d{2}-\d{2})(?:[ T](\d{2}(?::\d{2}(?::\d{2}(?:\.\d+)?)?))?)?Z?/,
33-
replacement: (_, date, time) => (time ? `${date}T${time}Z` : `${date}Z`),
34-
},
35-
]
6+
export type { AstroData, AstroText } from './nodes.js'
367

37-
export default function mdastAstroMd<T extends Nodes>(tree: T): T {
38-
findAndReplace(
39-
tree,
40-
visitorSpecs.map(({ type, pattern, replacement }) => [
41-
pattern,
42-
(value: string, ...groups: string[]) => ({
43-
type: 'text',
44-
value,
45-
data: { astromd: { type, value: replacement(value, ...groups) } },
46-
}),
47-
])
48-
)
8+
export default function <T extends Nodes>(tree: T): T {
9+
findAndReplace(tree, replacements)
4910
return tree
5011
}

src/nodes.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Text } from 'mdast'
2+
3+
type AstroValue = boolean | number | string
4+
5+
export type AstroData = {
6+
/** Astro Flavored Markdown data type */
7+
type: string
8+
/** Normalized value */
9+
value: AstroValue
10+
}
11+
12+
export interface AstroText extends Text {
13+
data: {
14+
astromd: AstroData
15+
}
16+
}
17+
18+
export function text(value: string): Text {
19+
return { type: 'text', value }
20+
}
21+
22+
export function astroText(
23+
value: string,
24+
type: string,
25+
astroValue?: AstroValue
26+
): AstroText {
27+
return {
28+
...text(value),
29+
data: { astromd: { type, value: astroValue ?? value } },
30+
}
31+
}

src/replacements/arXiv/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { FindAndReplaceTuple } from 'mdast-util-find-and-replace'
2+
3+
import { astroText } from '../../nodes.js'
4+
5+
export default [
6+
/arXiv:(?:\d{4}\.\d{5}|[a-z-]+\/\d{7})(?:v\d+)?/g,
7+
(value: string) => astroText(value, 'arXiv'),
8+
] satisfies FindAndReplaceTuple

src/replacements/arXiv/test.json

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"type": "root",
3+
"children": [
4+
{
5+
"type": "paragraph",
6+
"children": [
7+
{
8+
"type": "text",
9+
"value": "Old style arXiv reference: "
10+
},
11+
{
12+
"type": "text",
13+
"value": "arXiv:astro-ph/1234567",
14+
"data": {
15+
"astromd": {
16+
"type": "arXiv",
17+
"value": "arXiv:astro-ph/1234567"
18+
}
19+
}
20+
}
21+
],
22+
"position": {
23+
"start": {
24+
"line": 1,
25+
"column": 1,
26+
"offset": 0
27+
},
28+
"end": {
29+
"line": 1,
30+
"column": 50,
31+
"offset": 49
32+
}
33+
}
34+
},
35+
{
36+
"type": "paragraph",
37+
"children": [
38+
{
39+
"type": "text",
40+
"value": "New style arXiv reference: "
41+
},
42+
{
43+
"type": "text",
44+
"value": "arXiv:1234.56789",
45+
"data": {
46+
"astromd": {
47+
"type": "arXiv",
48+
"value": "arXiv:1234.56789"
49+
}
50+
}
51+
}
52+
],
53+
"position": {
54+
"start": {
55+
"line": 3,
56+
"column": 1,
57+
"offset": 51
58+
},
59+
"end": {
60+
"line": 3,
61+
"column": 44,
62+
"offset": 94
63+
}
64+
}
65+
},
66+
{
67+
"type": "paragraph",
68+
"children": [
69+
{
70+
"type": "text",
71+
"value": "Versioned arXiv reference: "
72+
},
73+
{
74+
"type": "text",
75+
"value": "arXiv:1234.56789v2",
76+
"data": {
77+
"astromd": {
78+
"type": "arXiv",
79+
"value": "arXiv:1234.56789v2"
80+
}
81+
}
82+
}
83+
],
84+
"position": {
85+
"start": {
86+
"line": 5,
87+
"column": 1,
88+
"offset": 96
89+
},
90+
"end": {
91+
"line": 5,
92+
"column": 46,
93+
"offset": 141
94+
}
95+
}
96+
}
97+
],
98+
"position": {
99+
"start": {
100+
"line": 1,
101+
"column": 1,
102+
"offset": 0
103+
},
104+
"end": {
105+
"line": 6,
106+
"column": 1,
107+
"offset": 142
108+
}
109+
}
110+
}

src/replacements/arXiv/test.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Old style arXiv reference: arXiv:astro-ph/1234567
2+
3+
New style arXiv reference: arXiv:1234.56789
4+
5+
Versioned arXiv reference: arXiv:1234.56789v2

src/replacements/datetime/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { FindAndReplaceTuple } from 'mdast-util-find-and-replace'
2+
3+
import { astroText } from '../../nodes.js'
4+
5+
export default [
6+
/(\d{4}-\d{2}-\d{2})(?:[ T](\d{2}(?::\d{2}(?::\d{2}(?:\.\d+)?)?)))?Z?/g,
7+
(value: string, date: string, time?: string) =>
8+
astroText(value, 'datetime', time ? `${date}T${time}Z` : `${date}Z`),
9+
] satisfies FindAndReplaceTuple

0 commit comments

Comments
 (0)