Skip to content

Commit b932f81

Browse files
committed
Add export of MdxjsEsmHast type, fix types
* while this is practically equivalent to the current node type, this is meant for the hast space, which in the future could start diverging from mdast literals * Fix types to only allow `MdxjsEsm`, in hast, in root nodes: they’re not allowed in elements * Fix types to only allow `MdxjsEsm`, in mdast, in root nodes: they’re not allowed in other things that allow blocks (list items, block quotes, footnote definitions)
1 parent 671dee9 commit b932f81

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

index.d.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import type {Literal} from 'mdast'
21
import type {Program} from 'estree-jsx'
2+
import type {Literal as HastLiteral} from 'hast'
3+
import type {Literal as MdastLiteral} from 'mdast'
34

45
export {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from './lib/index.js'
56

67
/**
78
* MDX ESM (import/export) node.
89
*/
910
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
10-
export interface MdxjsEsm extends Literal {
11+
export interface MdxjsEsm extends MdastLiteral {
1112
/**
1213
* Node type.
1314
*/
@@ -31,31 +32,46 @@ export interface MdxjsEsm extends Literal {
3132
// eslint-disable-next-line @typescript-eslint/naming-convention
3233
export type MDXJSEsm = MdxjsEsm
3334

34-
// Add nodes to content.
35-
declare module 'mdast' {
36-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
37-
interface BlockContentMap {
35+
/**
36+
* MDX ESM (import/export) node (for hast).
37+
*/
38+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
39+
export interface MdxjsEsmHast extends HastLiteral {
40+
/**
41+
* Node type.
42+
*/
43+
type: 'mdxjsEsm'
44+
45+
/**
46+
* Data.
47+
*/
48+
data?: {
3849
/**
39-
* MDX ESM.
50+
* Program node from estree.
4051
*/
41-
mdxjsEsm: MdxjsEsm
52+
// eslint-disable-next-line @typescript-eslint/ban-types
53+
estree?: Program | null | undefined
4254
}
4355
}
4456

45-
declare module 'hast' {
57+
// Add nodes to mdast content.
58+
declare module 'mdast' {
4659
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
47-
interface RootContentMap {
60+
interface FrontmatterContentMap {
4861
/**
4962
* MDX ESM.
5063
*/
5164
mdxjsEsm: MdxjsEsm
5265
}
66+
}
5367

68+
// Add nodes to hast content.
69+
declare module 'hast' {
5470
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
55-
interface ElementContentMap {
71+
interface RootContentMap {
5672
/**
5773
* MDX ESM.
5874
*/
59-
mdxjsEsm: MdxjsEsm
75+
mdxjsEsm: MdxjsEsmHast
6076
}
6177
}

readme.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [`mdxjsEsmFromMarkdown`](#mdxjsesmfrommarkdown)
2121
* [`mdxjsEsmToMarkdown`](#mdxjsesmtomarkdown)
2222
* [`MdxjsEsm`](#mdxjsesm)
23+
* [`MdxjsEsmHast`](#mdxjsesmhast)
2324
* [HTML](#html)
2425
* [Syntax](#syntax)
2526
* [Syntax tree](#syntax-tree)
@@ -197,8 +198,25 @@ MDX ESM (import/export) node (TypeScript type).
197198
###### Type
198199

199200
```ts
201+
import type {Program} from 'estree-jsx'
200202
import type {Literal} from 'mdast'
203+
204+
interface MdxjsEsm extends Literal {
205+
type: 'mdxjsEsm'
206+
data?: {estree?: Program | null | undefined}
207+
}
208+
```
209+
210+
### `MdxjsEsmHast`
211+
212+
Same as [`MdxjsEsm`][api-mdxjs-esm], but registered with `@types/hast`
213+
(TypeScript type).
214+
215+
###### Type
216+
217+
```ts
201218
import type {Program} from 'estree-jsx'
219+
import type {Literal} from 'hast'
202220

203221
interface MdxjsEsm extends Literal {
204222
type: 'mdxjsEsm'
@@ -227,7 +245,7 @@ The following interfaces are added to **[mdast][]** by this utility.
227245

228246
```idl
229247
interface MdxjsEsm <: Literal {
230-
type: "mdxjsEsm"
248+
type: 'mdxjsEsm'
231249
}
232250
```
233251

@@ -265,9 +283,10 @@ a *[parent][dfn-parent]*, that parent must be **[Root][dfn-root]**.
265283
## Types
266284

267285
This package is fully typed with [TypeScript][].
268-
It exports the additional type [`MdxjsEsm`][api-mdxjs-esm].
286+
It exports the additional types [`MdxjsEsm`][api-mdxjs-esm] and
287+
[`MdxjsEsmHast`][api-mdxjs-esm-hast].
269288

270-
It also registers the node type with `@types/mdast`.
289+
It also registers the node type with `@types/mdast` and `@types/hast`.
271290
If you’re working with the syntax tree, make sure to import this utility
272291
somewhere in your types, as that registers the new node types in the tree.
273292

@@ -389,14 +408,16 @@ abide by its terms.
389408

390409
[dfn-root]: https://github.com/syntax-tree/mdast#root
391410

392-
[dfn-flow-content]: #flowcontent-mdxjs-esm
393-
394411
[remark-mdx]: https://mdxjs.com/packages/remark-mdx/
395412

396413
[mdx]: https://mdxjs.com
397414

415+
[dfn-flow-content]: #flowcontent-mdxjs-esm
416+
398417
[api-mdxjs-esm-from-markdown]: #mdxjsesmfrommarkdown
399418

400419
[api-mdxjs-esm-to-markdown]: #mdxjsesmtomarkdown
401420

402421
[api-mdxjs-esm]: #mdxjsesm
422+
423+
[api-mdxjs-esm-hast]: #mdxjsesmhast

0 commit comments

Comments
 (0)