Skip to content

Commit

Permalink
Refactor/rewrite to add GCN Circulars cross references
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsinger committed Sep 13, 2023
1 parent 497dd74 commit 833bfcb
Show file tree
Hide file tree
Showing 21 changed files with 7,189 additions and 9,034 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: NPM Install
run: npm ci
# - name: Run eslint linter
# run: npx eslint --max-warnings 0 .
- name: Run eslint linter
run: npx eslint --max-warnings 0 .
- name: Run Prettier code style checks
run: npx prettier -c .
- name: Run unit tests
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node_modules
/coverage
/*.js
/*.d.ts
*.d.ts
.eslintcache
*.tgz
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"editor.tabSize": 2,
"javascript.format.semicolons": "remove",
"typescript.preferences.quoteStyle": "single",
"javascript.preferences.quoteStyle": "single",
"typescript.format.semicolons": "remove"
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ Astro Flavored Markdown supports the following types.
## datetime

A UTC date with an optional time, normalized to [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601).

## gcn-circular

A reference to a GCN Circular.
15,477 changes: 6,547 additions & 8,930 deletions package-lock.json

Large diffs are not rendered by default.

35 changes: 29 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
"version": "0.0.1",
"description": "An mdast plugin for Astro Flavored Markdown",
"main": "index.js",
"files": [
"types/**/*.d.ts"
],
"types": "types/index.d.ts",
"scripts": {
"prepare:husky": "husky install",
"prepare:esbuild": "esbuild src/index.ts --bundle --packages=external --platform=neutral --outfile=index.js",
"prepare:tsc": "tsc",
"prepare": "run-p prepare:*",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest"
Expand All @@ -29,37 +34,55 @@
"mdast-util-find-and-replace": "^3.0.0"
},
"devDependencies": {
"@tsconfig/node14": "^1.0.3",
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
"@tsconfig/node16": "^16.1.1",
"@types/jest": "^29.2.5",
"@types/mdast": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"cross-env": "^7.0.3",
"esbuild": "^0.19.2",
"eslint": "^8.31.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-jest": "^27.2.1",
"glob": "^10.3.4",
"husky": "^8.0.3",
"jest": "^29.3.1",
"jest": "^29.6.4",
"lint-staged": "^13.1.0",
"mdast-util-from-markdown": "^2.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.2",
"ts-jest": "^29.0.4",
"typescript": "^4.9.4"
"ts-dedent": "^2.2.0",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
},
"type": "module",
"sideEffects": false,
"prettier": {
"plugins": [
"@trivago/prettier-plugin-sort-imports"
],
"importOrder": [
"^[@a-zA-Z].*(?<!(?:css|gif|json|png|svg))$",
"^[.~/].*(?<!(?:css|gif|json|png|svg))$",
""
],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"semi": false,
"singleQuote": true
"singleQuote": true,
"trailingComma": "es5"
},
"jest": {
"preset": "ts-jest/presets/default-esm",
"collectCoverage": true,
"coverageReporters": [
"text",
"cobertura"
]
],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
}
},
"eslintConfig": {
"env": {
Expand Down
51 changes: 6 additions & 45 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
import type { Nodes, Text } from 'mdast'
import { type Find, findAndReplace } from 'mdast-util-find-and-replace'
import type { Nodes } from 'mdast'
import { findAndReplace } from 'mdast-util-find-and-replace'

type AstroData = {
/** Astro Flavored Markdown data type */
type: string
/** Normalized value */
value: string
}

export interface AstroText extends Text {
data: {
astromd: AstroData
}
}

interface VisitorSpec {
/** Name of the Astro Flavored Markdown type. */
type: string
/** Regular expression to search for. */
pattern: Find
/** Replacement function to generate normalized value.
* The function should be suitable for passing to String.replace().
*/
replacement: (value: string, ...groups: string[]) => string
}
import replacements from './replacements/index.js'

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

export default function mdastAstroMd<T extends Nodes>(tree: T): T {
findAndReplace(
tree,
visitorSpecs.map(({ type, pattern, replacement }) => [
pattern,
(value: string, ...groups: string[]) => ({
type: 'text',
value,
data: { astromd: { type, value: replacement(value, ...groups) } },
}),
])
)
export default function <T extends Nodes>(tree: T): T {
findAndReplace(tree, replacements)
return tree
}
31 changes: 31 additions & 0 deletions src/nodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Text } from 'mdast'

type AstroValue = boolean | number | string

export type AstroData = {
/** Astro Flavored Markdown data type */
type: string
/** Normalized value */
value: AstroValue
}

export interface AstroText extends Text {
data: {
astromd: AstroData
}
}

export function text(value: string): Text {
return { type: 'text', value }
}

export function astroText(
value: string,
type: string,
astroValue?: AstroValue
): AstroText {
return {
...text(value),
data: { astromd: { type, value: astroValue ?? value } },
}
}
8 changes: 8 additions & 0 deletions src/replacements/arXiv/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { FindAndReplaceTuple } from 'mdast-util-find-and-replace'

import { astroText } from '../../nodes.js'

export default [
/arXiv:(?:\d{4}\.\d{5}|[a-z-]+\/\d{7})(?:v\d+)?/g,
(value: string) => astroText(value, 'arXiv'),
] satisfies FindAndReplaceTuple
110 changes: 110 additions & 0 deletions src/replacements/arXiv/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Old style arXiv reference: "
},
{
"type": "text",
"value": "arXiv:astro-ph/1234567",
"data": {
"astromd": {
"type": "arXiv",
"value": "arXiv:astro-ph/1234567"
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 50,
"offset": 49
}
}
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "New style arXiv reference: "
},
{
"type": "text",
"value": "arXiv:1234.56789",
"data": {
"astromd": {
"type": "arXiv",
"value": "arXiv:1234.56789"
}
}
}
],
"position": {
"start": {
"line": 3,
"column": 1,
"offset": 51
},
"end": {
"line": 3,
"column": 44,
"offset": 94
}
}
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Versioned arXiv reference: "
},
{
"type": "text",
"value": "arXiv:1234.56789v2",
"data": {
"astromd": {
"type": "arXiv",
"value": "arXiv:1234.56789v2"
}
}
}
],
"position": {
"start": {
"line": 5,
"column": 1,
"offset": 96
},
"end": {
"line": 5,
"column": 46,
"offset": 141
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 6,
"column": 1,
"offset": 142
}
}
}
5 changes: 5 additions & 0 deletions src/replacements/arXiv/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Old style arXiv reference: arXiv:astro-ph/1234567

New style arXiv reference: arXiv:1234.56789

Versioned arXiv reference: arXiv:1234.56789v2
9 changes: 9 additions & 0 deletions src/replacements/datetime/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { FindAndReplaceTuple } from 'mdast-util-find-and-replace'

import { astroText } from '../../nodes.js'

export default [
/(\d{4}-\d{2}-\d{2})(?:[ T](\d{2}(?::\d{2}(?::\d{2}(?:\.\d+)?)?)))?Z?/g,
(value: string, date: string, time?: string) =>
astroText(value, 'datetime', time ? `${date}T${time}Z` : `${date}Z`),
] satisfies FindAndReplaceTuple
Loading

0 comments on commit 833bfcb

Please sign in to comment.