Skip to content

Commit 52905eb

Browse files
committed
Add smarter types for passThrough
1 parent cfa4c80 commit 52905eb

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed

lib/handlers/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {thematicBreak} from './thematic-break.js'
2424

2525
/**
2626
* Default handlers for nodes.
27+
*
28+
* @satisfies {import('../state.js').Handlers}
2729
*/
2830
export const handlers = {
2931
blockquote,
@@ -42,6 +44,7 @@ export const handlers = {
4244
listItem,
4345
list,
4446
paragraph,
47+
// @ts-expect-error: root is different, but hard to type.
4548
root,
4649
strong,
4750
table,

lib/handlers/table-row.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export function tableRow(state, node, parent) {
2727
// Generate a body row when without parent.
2828
const rowIndex = siblings ? siblings.indexOf(node) : 1
2929
const tagName = rowIndex === 0 ? 'th' : 'td'
30+
// To do: option to use `style`?
3031
const align = parent && parent.type === 'table' ? parent.align : undefined
3132
const length = align ? align.length : node.children.length
3233
let cellIndex = -1

lib/state.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @returns {Array<HastElementContent> | HastElementContent | undefined}
2828
* hast node.
2929
*
30-
* @typedef {Record<string, Handler>} Handlers
30+
* @typedef {Partial<Record<MdastNodes['type'], Handler>>} Handlers
3131
* Handle nodes.
3232
*
3333
* @typedef Options
@@ -135,7 +135,7 @@
135135
* pass different properties with the `footnoteLabelProperties` option.
136136
* @property {Handlers | null | undefined} [handlers]
137137
* Extra handlers for nodes (optional).
138-
* @property {Array<string> | null | undefined} [passThrough]
138+
* @property {Array<MdastNodes['type']> | null | undefined} [passThrough]
139139
* List of custom mdast node types to pass through (keep) in hast (note that
140140
* the node itself is passed, but eventual children are transformed)
141141
* (optional).
@@ -243,9 +243,10 @@ export function createState(tree, options) {
243243
*/
244244
function one(node, parent) {
245245
const type = node.type
246+
const handle = state.handlers[type]
246247

247-
if (own.call(state.handlers, type)) {
248-
return state.handlers[type](state, node, parent)
248+
if (own.call(state.handlers, type) && handle) {
249+
return handle(state, node, parent)
249250
}
250251

251252
if (state.options.passThrough && state.options.passThrough.includes(type)) {

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ Handle nodes (TypeScript).
337337
###### Type
338338

339339
```ts
340-
type Handlers = Record<string, Handler>
340+
type Handlers = Partial<Record<Nodes['type'], Handler>>
341341
```
342342
343343
### `Options`
@@ -372,7 +372,7 @@ Configuration (TypeScript).
372372
— tag name to use for the footnote label
373373
* `handlers` ([`Handlers`][api-handlers], optional)
374374
— extra handlers for nodes
375-
* `passThrough` (`Array<string>`, optional)
375+
* `passThrough` (`Array<Nodes['type']>`, optional)
376376
— list of custom mdast node types to pass through (keep) in hast (note that
377377
the node itself is passed, but eventual children are transformed)
378378
* `unknownHandler` ([`Handler`][api-handler], optional)

test/core.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Paragraph} Paragraph
4-
* @typedef {import('mdast').Nodes} Nodes
54
*/
65

76
import assert from 'node:assert/strict'

0 commit comments

Comments
 (0)