-
Notifications
You must be signed in to change notification settings - Fork 75
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
⬆️ Add simple upgrade/downgrade package for MyST AST #1802
Open
agoose77
wants to merge
21
commits into
main
Choose a base branch
from
agoose77/feat-downgrade-upgrade-simple
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7551450
wip: initial myst-compat package
agoose77 07d8263
wip: ast up and downgrading
agoose77 e7ac627
test: add simple test
agoose77 5f610ff
chore: run linter
agoose77 eabe58a
fix: get tests working
agoose77 47cadc4
fix: try to copy children where there is only one "output" object
agoose77 44fc61f
fix: allow missing children
agoose77 a2dccaf
fix: improve types, support placeholders
agoose77 427d85c
fix: add label and identifier information to outputs
agoose77 362008b
wip: footnotes
agoose77 b3542f1
chore: add changeset
agoose77 87c938c
fix: update tests
agoose77 57d552f
test: test upgrade
agoose77 35376ff
chore: remove utils
agoose77 5cda3e9
feat: add export for spec version
agoose77 95f0a94
wip: prepare to upgrade documents
agoose77 c7203e5
fix: update tests
agoose77 8e44066
fix: plumb-in SPEC_VERSION to file writing
agoose77 fb8715f
fix: add missing file
agoose77 f3a4f89
chore: run linter
agoose77 d01c5a7
chore: run linter
agoose77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"myst-compat": patch | ||
--- | ||
|
||
Add myst-compat package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['curvenote'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# myst-compat | ||
|
||
Utilites for up/downgrading MyST ASTs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "myst-compat", | ||
"sideEffects": false, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"exports": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"license": "MIT", | ||
"scripts": { | ||
"clean": "rimraf dist", | ||
"lint": "eslint \"src/**/!(*.spec).ts\" -c ./.eslintrc.cjs", | ||
"lint:format": "prettier --check \"src/**/*.{ts,tsx,md}\"", | ||
"test": "vitest run", | ||
"test:watch": "vitest watch", | ||
"build:esm": "tsc", | ||
"build": "npm-run-all -l clean -p build:esm" | ||
}, | ||
"dependencies": { | ||
"unist-util-select": "^4.0.3", | ||
"unist-util-visit": "^4.1.2", | ||
"vfile": "^5.0.0", | ||
"vfile-message": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@jupyterlab/nbformat": "^3.5.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { Parent } from 'mdast'; | ||
|
||
import { downgrade as downgrade2To1 } from './version_2_1.js'; | ||
|
||
export function downgrade(from: string, to: string, ast: Parent): void { | ||
if (to === '1' && from === '2') { | ||
downgrade2To1(ast); | ||
return; | ||
} else if (to === from) { | ||
return; | ||
} else { | ||
throw new Error(`Unable to downgrade between ${from} and ${to}`); | ||
} | ||
} | ||
|
||
export { downgrade2To1 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { downgrade } from './version_2_1.js'; | ||
import type { Parent } from 'mdast'; | ||
|
||
const SIMPLE_AST: Parent = { | ||
type: 'root', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'block', | ||
children: [ | ||
{ | ||
// @ts-expect-error: invalid child type | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'This is an ', | ||
}, | ||
{ | ||
type: 'emphasis', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'interesting', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'text', | ||
value: ' file. See ', | ||
}, | ||
{ | ||
type: 'link', | ||
url: '#other', | ||
children: [], | ||
// @ts-expect-error: unknown field | ||
urlSource: '#other', | ||
}, | ||
{ | ||
type: 'text', | ||
value: ' for more.', | ||
}, | ||
], | ||
}, | ||
{ | ||
// @ts-expect-error: invalid child type | ||
type: 'code', | ||
lang: 'python', | ||
value: 'Some code', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const SIMPLE_V2_AST_WITH_FOOTNOTE: Parent = { | ||
type: 'root', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'block', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'See the footnote', | ||
}, | ||
{ | ||
type: 'footnoteReference', | ||
identifier: '1', | ||
label: '1', | ||
// @ts-expect-error: unknown type | ||
enumerator: '1', | ||
}, | ||
], | ||
}, | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'footnoteDefinition', | ||
identifier: '1', | ||
label: '1', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'A footnote', | ||
}, | ||
], | ||
}, | ||
], | ||
enumerator: '1', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const SIMPLE_V1_AST_WITH_FOOTNOTE: Parent = { | ||
type: 'root', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'block', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'See the footnote', | ||
}, | ||
{ | ||
type: 'footnoteReference', | ||
identifier: '1', | ||
label: '1', | ||
// @ts-expect-error: unknown type | ||
number: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'footnoteDefinition', | ||
identifier: '1', | ||
label: '1', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'A footnote', | ||
}, | ||
], | ||
}, | ||
], | ||
number: 1, | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
const SIMPLE_V2_AST_WITH_INVALID_FOOTNOTE: Parent = { | ||
type: 'root', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'block', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'See the footnote', | ||
}, | ||
{ | ||
type: 'footnoteReference', | ||
identifier: '1', | ||
label: '1', | ||
// @ts-expect-error: unknown type | ||
enumerator: '%s.1', | ||
}, | ||
], | ||
}, | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'footnoteDefinition', | ||
identifier: '1', | ||
label: '1', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'A footnote', | ||
}, | ||
], | ||
}, | ||
], | ||
enumerator: '%s.1', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const SIMPLE_V1_AST_WITH_INVALID_FOOTNOTE: Parent = { | ||
type: 'root', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'block', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'See the footnote', | ||
}, | ||
{ | ||
type: 'footnoteReference', | ||
identifier: '1', | ||
label: '1', | ||
}, | ||
], | ||
}, | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'footnoteDefinition', | ||
identifier: '1', | ||
label: '1', | ||
children: [ | ||
{ | ||
// @ts-expect-error: unknown type | ||
type: 'paragraph', | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: 'A footnote', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
describe('downgrade 2->1', () => { | ||
it('leaves a simple AST unchanged', () => { | ||
const ast = structuredClone(SIMPLE_AST); | ||
downgrade(ast); | ||
expect(ast).toStrictEqual(SIMPLE_AST); | ||
}); | ||
it('downgrades a v2 AST with footnotes', () => { | ||
const ast = structuredClone(SIMPLE_V2_AST_WITH_FOOTNOTE); | ||
downgrade(ast); | ||
expect(ast).toStrictEqual(SIMPLE_V1_AST_WITH_FOOTNOTE); | ||
}); | ||
it('downgrades a v2 AST with invalid footnotes', () => { | ||
const ast = structuredClone(SIMPLE_V2_AST_WITH_INVALID_FOOTNOTE); | ||
downgrade(ast); | ||
expect(ast).toStrictEqual(SIMPLE_V1_AST_WITH_INVALID_FOOTNOTE); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { Parent } from 'mdast'; | ||
import type { | ||
FootnoteDefinition as FootnoteDefinition2, | ||
FootnoteReference as FootnoteReference2, | ||
} from '../types/v2.js'; | ||
import type { | ||
FootnoteDefinition as FootnoteDefinition1, | ||
FootnoteReference as FootnoteReference1, | ||
} from '../types/v1.js'; | ||
import { visit, CONTINUE, SKIP } from 'unist-util-visit'; | ||
|
||
function maybeParseInt(value: string | undefined): number | undefined { | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
const result = parseInt(value); | ||
if (String(result) === value) { | ||
return result; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
|
||
export function downgrade(ast: Parent) { | ||
// Walk over footnote nodes | ||
visit( | ||
ast as any, | ||
['footnoteDefinition', 'footnoteReference'], | ||
( | ||
node: FootnoteDefinition2 | FootnoteReference2, | ||
index: number | null, | ||
parent: Parent | null, | ||
) => { | ||
if (parent) { | ||
switch (node.type) { | ||
case 'footnoteDefinition': { | ||
const { enumerator, ...rest } = node; | ||
const nextNode: FootnoteDefinition1 = rest; | ||
const maybeNumber = maybeParseInt(enumerator); | ||
if (maybeNumber !== undefined) { | ||
nextNode.number = maybeNumber; | ||
} | ||
parent.children[index!] = nextNode as any; | ||
return CONTINUE; | ||
} | ||
case 'footnoteReference': { | ||
const { enumerator, ...rest } = node; | ||
const nextNode: FootnoteReference1 = rest; | ||
const maybeNumber = maybeParseInt(enumerator); | ||
if (maybeNumber !== undefined) { | ||
nextNode.number = maybeNumber; | ||
} | ||
parent.children[index!] = nextNode as any; | ||
return SKIP; | ||
} | ||
} | ||
} | ||
}, | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it matters for now (as in we can get this in asap), however, I think this would be easier to manage with separate test dir with json files and then a global test runner.