-
Notifications
You must be signed in to change notification settings - Fork 128
Add myst-spec as TypeScript types
#2579
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
base: main
Are you sure you want to change the base?
Conversation
|
8a9991d to
5c88fd4
Compare
| export function mergeTextNodes(node: Content): Content { | ||
| const children = nodeHasChildren(node) | ||
| ? node.children.reduce((c, n) => { | ||
| if (n?.type !== 'text') { | ||
| c.push(mergeTextNodes(n)); | ||
| return c; | ||
| } | ||
| const last = c[c.length - 1]; | ||
| if (last?.type !== 'text') { | ||
| c.push(n); | ||
| return c; | ||
| } | ||
| if (n.position?.end) { | ||
| if (!last.position) last.position = {} as Required<Node>['position']; | ||
| last.position.end = n.position.end; | ||
| } | ||
| if (!last.value) last.value = ''; | ||
| if (n.value) last.value += n.value; | ||
| return c; | ||
| }, [] as Content[]) | ||
| : []; | ||
| if (children) (node as Parent).children = children; | ||
| return node; |
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 suspect this should actually be generic, e.g.
export function mergeTextNodes<T extends Nodes>(node: T): T {although that won't be a lower bound
| records: SearchRecord[]; | ||
| }; | ||
|
|
||
| export type * from 'myst-spec'; |
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.
For compatibility.
| enumerator: string; | ||
| } | ||
|
|
||
| /// Types ////////////////////////////// |
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.
First we define the custom MyST types.
| } | ||
| type Visibility = 'show' | 'hide' | 'remove'; | ||
|
|
||
| declare module 'mdast' { |
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.
Then we define the customisations to the mdast types
These are coming down the wire in mdast4
These types just don't exist at all!
Closes #2582, as a part of #2563.
How
mdastis typedmdast@^3is defined in text on the GitHub Repo, and in TypeScript in@types/mdastvia Definitely Typed.The type system is designed to be extensible through the use of mapping types, e.g.
The reason for this approach over directly specifying
type BlockContent = Paragraph | ...is that this type set is open to extension. Third-party code can extend the members ofBlockContentMapby using declaration merging via module augmentation: https://www.typescriptlang.org/docs/handbook/declaration-merging.htmlWhat this means for
mystPresently, we use two type families in MyST:
myst-spec-ext, which defines our effective spec (rather than our published one)myst-common, which defines ourGenericParentandGenericNodeworkaroundsAs
mystmdhas been developed, we've worked around type problems with type casts, and our types are fragile. The goal with this PR is to set the foundations for us to be stricter with these types. I suspect future PRs will have to chip away at removingGenericParentaltogether.