Skip to content

Commit 8179548

Browse files
committed
Rename H to State
Backwards compatible for now, but the overloads will be removed next major, and so will many other fields on the object.
1 parent 6d1021e commit 8179548

34 files changed

+636
-585
lines changed

index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import type {Literal} from 'hast'
2+
import type {State} from './lib/state.js'
23

34
// Expose types.
4-
export type {H, Handler, Handlers, Options} from './lib/index.js'
5+
export type {State, Handler, Handlers, Options} from './lib/state.js'
6+
7+
// To do: next major: remove.
8+
/**
9+
* Deprecated: use `State`.
10+
*/
11+
export type H = State
512

613
// Expose JS API.
714
export {one, all} from './lib/traverse.js'

lib/footer.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('hast').ElementContent} ElementContent
44
*
5-
* @typedef {import('./index.js').H} H
5+
* @typedef {import('./state.js').State} State
66
*/
77

88
import {normalizeUri} from 'micromark-util-sanitize-uri'
@@ -12,45 +12,45 @@ import {wrap} from './wrap.js'
1212
/**
1313
* Generate a hast footer for called footnote definitions.
1414
*
15-
* @param {H} h
15+
* @param {State} state
1616
* Info passed around.
1717
* @returns {Element | undefined}
1818
* `section` element or `undefined`.
1919
*/
20-
export function footer(h) {
20+
export function footer(state) {
2121
/** @type {Array<ElementContent>} */
2222
const listItems = []
2323
let index = -1
2424

25-
while (++index < h.footnoteOrder.length) {
26-
const def = h.footnoteById[h.footnoteOrder[index].toUpperCase()]
25+
while (++index < state.footnoteOrder.length) {
26+
const def = state.footnoteById[state.footnoteOrder[index].toUpperCase()]
2727

2828
if (!def) {
2929
continue
3030
}
3131

32-
const content = all(h, def)
32+
const content = all(state, def)
3333
const id = String(def.identifier)
3434
const safeId = normalizeUri(id.toLowerCase())
3535
let referenceIndex = 0
3636
/** @type {Array<ElementContent>} */
3737
const backReferences = []
3838

39-
while (++referenceIndex <= h.footnoteCounts[id]) {
39+
while (++referenceIndex <= state.footnoteCounts[id]) {
4040
/** @type {Element} */
4141
const backReference = {
4242
type: 'element',
4343
tagName: 'a',
4444
properties: {
4545
href:
4646
'#' +
47-
h.clobberPrefix +
47+
state.clobberPrefix +
4848
'fnref-' +
4949
safeId +
5050
(referenceIndex > 1 ? '-' + referenceIndex : ''),
5151
dataFootnoteBackref: true,
5252
className: ['data-footnote-backref'],
53-
ariaLabel: h.footnoteBackLabel
53+
ariaLabel: state.footnoteBackLabel
5454
},
5555
children: [{type: 'text', value: '↩'}]
5656
}
@@ -89,11 +89,11 @@ export function footer(h) {
8989
const listItem = {
9090
type: 'element',
9191
tagName: 'li',
92-
properties: {id: h.clobberPrefix + 'fn-' + safeId},
92+
properties: {id: state.clobberPrefix + 'fn-' + safeId},
9393
children: wrap(content, true)
9494
}
9595

96-
h.patch(def, listItem)
96+
state.patch(def, listItem)
9797

9898
listItems.push(listItem)
9999
}
@@ -109,13 +109,13 @@ export function footer(h) {
109109
children: [
110110
{
111111
type: 'element',
112-
tagName: h.footnoteLabelTagName,
112+
tagName: state.footnoteLabelTagName,
113113
properties: {
114114
// To do: use structured clone.
115-
...JSON.parse(JSON.stringify(h.footnoteLabelProperties)),
115+
...JSON.parse(JSON.stringify(state.footnoteLabelProperties)),
116116
id: 'footnote-label'
117117
},
118-
children: [{type: 'text', value: h.footnoteLabel}]
118+
children: [{type: 'text', value: state.footnoteLabel}]
119119
},
120120
{type: 'text', value: '\n'},
121121
{

lib/handlers/blockquote.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Blockquote} Blockquote
4-
* @typedef {import('../index.js').H} H
4+
* @typedef {import('../state.js').State} State
55
*/
66

77
import {wrap} from '../wrap.js'
@@ -10,21 +10,21 @@ import {all} from '../traverse.js'
1010
/**
1111
* Turn an mdast `blockquote` node into hast.
1212
*
13-
* @param {H} h
13+
* @param {State} state
1414
* Info passed around.
1515
* @param {Blockquote} node
1616
* mdast node.
1717
* @returns {Element}
1818
* hast node.
1919
*/
20-
export function blockquote(h, node) {
20+
export function blockquote(state, node) {
2121
/** @type {Element} */
2222
const result = {
2323
type: 'element',
2424
tagName: 'blockquote',
2525
properties: {},
26-
children: wrap(all(h, node), true)
26+
children: wrap(all(state, node), true)
2727
}
28-
h.patch(node, result)
29-
return h.applyData(node, result)
28+
state.patch(node, result)
29+
return state.applyData(node, result)
3030
}

lib/handlers/break.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('hast').Text} Text
44
* @typedef {import('mdast').Break} Break
5-
* @typedef {import('../index.js').H} H
5+
* @typedef {import('../state.js').State} State
66
*/
77

88
/**
99
* Turn an mdast `break` node into hast.
1010
*
11-
* @param {H} h
11+
* @param {State} state
1212
* Info passed around.
1313
* @param {Break} node
1414
* mdast node.
1515
* @returns {Array<Element | Text>}
1616
* hast element content.
1717
*/
18-
export function hardBreak(h, node) {
18+
export function hardBreak(state, node) {
1919
/** @type {Element} */
2020
const result = {type: 'element', tagName: 'br', properties: {}, children: []}
21-
h.patch(node, result)
22-
return [h.applyData(node, result), {type: 'text', value: '\n'}]
21+
state.patch(node, result)
22+
return [state.applyData(node, result), {type: 'text', value: '\n'}]
2323
}

lib/handlers/code.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('hast').Properties} Properties
44
* @typedef {import('mdast').Code} Code
5-
* @typedef {import('../index.js').H} H
5+
* @typedef {import('../state.js').State} State
66
77
*/
88

99
/**
1010
* Turn an mdast `code` node into hast.
1111
*
12-
* @param {H} h
12+
* @param {State} state
1313
* Info passed around.
1414
* @param {Code} node
1515
* mdast node.
1616
* @returns {Element}
1717
* hast node.
1818
*/
19-
export function code(h, node) {
19+
export function code(state, node) {
2020
const value = node.value ? node.value + '\n' : ''
2121
// To do: next major, use `node.lang` w/o regex, the splitting’s been going
2222
// on for years in remark now.
@@ -41,11 +41,11 @@ export function code(h, node) {
4141
result.data = {meta: node.meta}
4242
}
4343

44-
h.patch(node, result)
45-
result = h.applyData(node, result)
44+
state.patch(node, result)
45+
result = state.applyData(node, result)
4646

4747
// Create `<pre>`.
4848
result = {type: 'element', tagName: 'pre', properties: {}, children: [result]}
49-
h.patch(node, result)
49+
state.patch(node, result)
5050
return result
5151
}

lib/handlers/delete.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Delete} Delete
4-
* @typedef {import('../index.js').H} H
4+
* @typedef {import('../state.js').State} State
55
66
*/
77

@@ -10,21 +10,21 @@ import {all} from '../traverse.js'
1010
/**
1111
* Turn an mdast `delete` node into hast.
1212
*
13-
* @param {H} h
13+
* @param {State} state
1414
* Info passed around.
1515
* @param {Delete} node
1616
* mdast node.
1717
* @returns {Element}
1818
* hast node.
1919
*/
20-
export function strikethrough(h, node) {
20+
export function strikethrough(state, node) {
2121
/** @type {Element} */
2222
const result = {
2323
type: 'element',
2424
tagName: 'del',
2525
properties: {},
26-
children: all(h, node)
26+
children: all(state, node)
2727
}
28-
h.patch(node, result)
29-
return h.applyData(node, result)
28+
state.patch(node, result)
29+
return state.applyData(node, result)
3030
}

lib/handlers/emphasis.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Emphasis} Emphasis
4-
* @typedef {import('../index.js').H} H
4+
* @typedef {import('../state.js').State} State
55
*/
66

77
import {all} from '../traverse.js'
88

99
/**
1010
* Turn an mdast `emphasis` node into hast.
1111
*
12-
* @param {H} h
12+
* @param {State} state
1313
* Info passed around.
1414
* @param {Emphasis} node
1515
* mdast node.
1616
* @returns {Element}
1717
* hast node.
1818
*/
19-
export function emphasis(h, node) {
19+
export function emphasis(state, node) {
2020
/** @type {Element} */
2121
const result = {
2222
type: 'element',
2323
tagName: 'em',
2424
properties: {},
25-
children: all(h, node)
25+
children: all(state, node)
2626
}
27-
h.patch(node, result)
28-
return h.applyData(node, result)
27+
state.patch(node, result)
28+
return state.applyData(node, result)
2929
}

lib/handlers/footnote-reference.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
/**
22
* @typedef {import('mdast').FootnoteReference} FootnoteReference
33
* @typedef {import('hast').Element} Element
4-
* @typedef {import('../index.js').H} H
4+
* @typedef {import('../state.js').State} State
55
*/
66

77
import {normalizeUri} from 'micromark-util-sanitize-uri'
88

99
/**
1010
* Turn an mdast `footnoteReference` node into hast.
1111
*
12-
* @param {H} h
12+
* @param {State} state
1313
* Info passed around.
1414
* @param {FootnoteReference} node
1515
* mdast node.
1616
* @returns {Element}
1717
* hast node.
1818
*/
19-
export function footnoteReference(h, node) {
19+
export function footnoteReference(state, node) {
2020
const id = String(node.identifier)
2121
const safeId = normalizeUri(id.toLowerCase())
22-
const index = h.footnoteOrder.indexOf(id)
22+
const index = state.footnoteOrder.indexOf(id)
2323
/** @type {number} */
2424
let counter
2525

2626
if (index === -1) {
27-
h.footnoteOrder.push(id)
28-
h.footnoteCounts[id] = 1
29-
counter = h.footnoteOrder.length
27+
state.footnoteOrder.push(id)
28+
state.footnoteCounts[id] = 1
29+
counter = state.footnoteOrder.length
3030
} else {
31-
h.footnoteCounts[id]++
31+
state.footnoteCounts[id]++
3232
counter = index + 1
3333
}
3434

35-
const reuseCounter = h.footnoteCounts[id]
35+
const reuseCounter = state.footnoteCounts[id]
3636

3737
/** @type {Element} */
3838
const link = {
3939
type: 'element',
4040
tagName: 'a',
4141
properties: {
42-
href: '#' + h.clobberPrefix + 'fn-' + safeId,
42+
href: '#' + state.clobberPrefix + 'fn-' + safeId,
4343
id:
44-
h.clobberPrefix +
44+
state.clobberPrefix +
4545
'fnref-' +
4646
safeId +
4747
(reuseCounter > 1 ? '-' + reuseCounter : ''),
@@ -50,7 +50,7 @@ export function footnoteReference(h, node) {
5050
},
5151
children: [{type: 'text', value: String(counter)}]
5252
}
53-
h.patch(node, link)
53+
state.patch(node, link)
5454

5555
/** @type {Element} */
5656
const sup = {
@@ -59,6 +59,6 @@ export function footnoteReference(h, node) {
5959
properties: {},
6060
children: [link]
6161
}
62-
h.patch(node, sup)
63-
return h.applyData(node, sup)
62+
state.patch(node, sup)
63+
return state.applyData(node, sup)
6464
}

lib/handlers/footnote.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').Footnote} Footnote
4-
* @typedef {import('../index.js').H} H
4+
* @typedef {import('../state.js').State} State
55
*/
66

77
import {footnoteReference} from './footnote-reference.js'
@@ -15,15 +15,15 @@ import {footnoteReference} from './footnote-reference.js'
1515
/**
1616
* Turn an mdast `footnote` node into hast.
1717
*
18-
* @param {H} h
18+
* @param {State} state
1919
* Info passed around.
2020
* @param {Footnote} node
2121
* mdast node.
2222
* @returns {Element}
2323
* hast node.
2424
*/
25-
export function footnote(h, node) {
26-
const footnoteById = h.footnoteById
25+
export function footnote(state, node) {
26+
const footnoteById = state.footnoteById
2727
let no = 1
2828

2929
while (no in footnoteById) no++
@@ -37,7 +37,7 @@ export function footnote(h, node) {
3737
position: node.position
3838
}
3939

40-
return footnoteReference(h, {
40+
return footnoteReference(state, {
4141
type: 'footnoteReference',
4242
identifier,
4343
position: node.position

0 commit comments

Comments
 (0)