Skip to content

Commit

Permalink
Add smarter types for passThrough
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 17, 2023
1 parent cfa4c80 commit 52905eb
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {thematicBreak} from './thematic-break.js'

/**
* Default handlers for nodes.
*
* @satisfies {import('../state.js').Handlers}
*/
export const handlers = {
blockquote,
Expand All @@ -42,6 +44,7 @@ export const handlers = {
listItem,
list,
paragraph,
// @ts-expect-error: root is different, but hard to type.
root,
strong,
table,
Expand Down
1 change: 1 addition & 0 deletions lib/handlers/table-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function tableRow(state, node, parent) {
// Generate a body row when without parent.
const rowIndex = siblings ? siblings.indexOf(node) : 1
const tagName = rowIndex === 0 ? 'th' : 'td'
// To do: option to use `style`?
const align = parent && parent.type === 'table' ? parent.align : undefined
const length = align ? align.length : node.children.length
let cellIndex = -1
Expand Down
9 changes: 5 additions & 4 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @returns {Array<HastElementContent> | HastElementContent | undefined}
* hast node.
*
* @typedef {Record<string, Handler>} Handlers
* @typedef {Partial<Record<MdastNodes['type'], Handler>>} Handlers
* Handle nodes.
*
* @typedef Options
Expand Down Expand Up @@ -135,7 +135,7 @@
* pass different properties with the `footnoteLabelProperties` option.
* @property {Handlers | null | undefined} [handlers]
* Extra handlers for nodes (optional).
* @property {Array<string> | null | undefined} [passThrough]
* @property {Array<MdastNodes['type']> | null | undefined} [passThrough]
* List of custom mdast node types to pass through (keep) in hast (note that
* the node itself is passed, but eventual children are transformed)
* (optional).
Expand Down Expand Up @@ -243,9 +243,10 @@ export function createState(tree, options) {
*/
function one(node, parent) {
const type = node.type
const handle = state.handlers[type]

if (own.call(state.handlers, type)) {
return state.handlers[type](state, node, parent)
if (own.call(state.handlers, type) && handle) {
return handle(state, node, parent)
}

if (state.options.passThrough && state.options.passThrough.includes(type)) {
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ Handle nodes (TypeScript).
###### Type

```ts
type Handlers = Record<string, Handler>
type Handlers = Partial<Record<Nodes['type'], Handler>>
```
### `Options`
Expand Down Expand Up @@ -372,7 +372,7 @@ Configuration (TypeScript).
— tag name to use for the footnote label
* `handlers` ([`Handlers`][api-handlers], optional)
— extra handlers for nodes
* `passThrough` (`Array<string>`, optional)
* `passThrough` (`Array<Nodes['type']>`, optional)
— list of custom mdast node types to pass through (keep) in hast (note that
the node itself is passed, but eventual children are transformed)
* `unknownHandler` ([`Handler`][api-handler], optional)
Expand Down
1 change: 0 additions & 1 deletion test/core.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @typedef {import('hast').Element} Element
* @typedef {import('mdast').Paragraph} Paragraph
* @typedef {import('mdast').Nodes} Nodes
*/

import assert from 'node:assert/strict'
Expand Down

0 comments on commit 52905eb

Please sign in to comment.