Skip to content

Commit 6f555a0

Browse files
committed
Add supported data fields to Data of mdast
1 parent 64004ba commit 6f555a0

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

index.d.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type {Data, Literal} from 'hast'
2-
import type {State} from './lib/state.js'
1+
import type {Data, ElementContent, Literal, Properties} from 'hast'
32

43
// Expose types.
54
export type {Handler, Handlers, Options, State} from './lib/state.js'
@@ -44,3 +43,35 @@ declare module 'hast' {
4443
raw: Raw
4544
}
4645
}
46+
47+
// Register data on mdast.
48+
declare module 'mdast' {
49+
interface Data {
50+
/**
51+
* Field supported by `mdast-util-to-hast` to signal that a node should
52+
* result in something with these children.
53+
*
54+
* When this is defined, when a parent is created, these children will
55+
* be used.
56+
*/
57+
hChildren?: ElementContent[] | undefined
58+
59+
/**
60+
* Field supported by `mdast-util-to-hast` to signal that a node should
61+
* result in a particular element, instead of its default behavior.
62+
*
63+
* When this is defined, an element with the given tag name is created.
64+
* For example, when setting `hName` to `'b'`, a `<b>` element is created.
65+
*/
66+
hName?: string | undefined
67+
68+
/**
69+
* Field supported by `mdast-util-to-hast` to signal that a node should
70+
* result in an element with these properties.
71+
*
72+
* When this is defined, when an element is created, these properties will
73+
* be used.
74+
*/
75+
hProperties?: Properties | undefined
76+
}
77+
}

lib/state.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,6 @@
1313
*/
1414

1515
/**
16-
* @typedef EmbeddedHastFields
17-
* hast fields.
18-
* @property {string | undefined} [hName]
19-
* Generate a specific element with this tag name instead.
20-
* @property {HastProperties | undefined} [hProperties]
21-
* Generate an element with these properties instead.
22-
* @property {Array<HastElementContent> | undefined} [hChildren]
23-
* Generate an element with this content instead.
24-
*
25-
* To do: type data!
26-
*
27-
* @typedef {Record<string, unknown> & EmbeddedHastFields} MdastData
28-
* mdast data with embedded hast fields.
29-
*
30-
* To do: type data!
31-
*
32-
* @typedef {MdastNodes & {data?: MdastData | undefined}} MdastNodeWithData
33-
* mdast node with embedded hast data.
34-
*
35-
* To do: type data!
36-
*
3716
* @callback Handler
3817
* Handle a node.
3918
* @param {State} state
@@ -309,7 +288,6 @@ function applyData(from, to) {
309288
hChildren !== null &&
310289
hChildren !== undefined
311290
) {
312-
// @ts-expect-error: assume valid children are defined.
313291
result.children = hChildren
314292
}
315293
}

readme.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,14 @@ Raw string of HTML embedded into HTML AST (TypeScript).
269269
###### Type
270270
271271
```ts
272-
import type {Literal} from 'hast'
272+
import type {Data, Literal} from 'hast'
273273

274274
interface Raw extends Literal {
275275
type: 'raw'
276+
data?: RawData | undefined
276277
}
278+
279+
interface RawData extends Data {}
277280
```
278281

279282
### `State`
@@ -1341,10 +1344,10 @@ Raw nodes are typically ignored but are handled by
13411344
## Types
13421345

13431346
This package is fully typed with [TypeScript][].
1344-
It also exports [`Handler`][api-handler], [`Handlers`][api-handlers],
1347+
It exports the [`Handler`][api-handler], [`Handlers`][api-handlers],
13451348
[`Options`][api-options], [`Raw`][api-raw], and [`State`][api-state] types.
13461349

1347-
It also registers the `Raw` node type with `@types/mdast`.
1350+
It also registers the `Raw` node type with `@types/hast`.
13481351
If you’re working with the syntax tree (and you pass
13491352
`allowDangerousHtml: true`), make sure to import this utility somewhere in your
13501353
types, as that registers the new node type in the tree.
@@ -1364,6 +1367,24 @@ visit(tree, function (node) {
13641367
})
13651368
```
13661369

1370+
Finally, it also registers the `hChildren`, `hName`, and `hProperties` fields
1371+
on `Data` of `@types/mdast`.
1372+
If you’re working with the syntax tree, make sure to import this utility
1373+
somewhere in your types, as that registers the data fields in the tree.
1374+
1375+
```js
1376+
/**
1377+
* @typedef {import('mdast-util-to-hast')}
1378+
*/
1379+
1380+
import {visit} from 'unist-util-visit'
1381+
1382+
/** @type {import('hast').Root} */
1383+
const tree = { /**/ }
1384+
1385+
console.log(tree.data?.hName) // Types as `string | undefined`.
1386+
```
1387+
13671388
## Compatibility
13681389
13691390
Projects maintained by the unified collective are compatible with maintained

test/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ test('toHast', async function (t) {
3939
assert.deepEqual(
4040
toHast({
4141
type: 'strong',
42+
// @ts-expect-error: remove when `h` is updated to support new hast.
4243
data: {hChildren: [h('i', 'bravo')]},
4344
children: [{type: 'text', value: 'charlie'}]
4445
}),

0 commit comments

Comments
 (0)