Skip to content

Commit 070ad5f

Browse files
committed
Add associationId helper to state
1 parent 35ceafc commit 070ad5f

File tree

7 files changed

+50
-18
lines changed

7 files changed

+50
-18
lines changed

lib/handle/definition.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* @typedef {import('../types.js').Info} Info
66
*/
77

8-
import {association} from '../util/association.js'
98
import {checkQuote} from '../util/check-quote.js'
109

1110
/**
@@ -23,7 +22,7 @@ export function definition(node, _, state, info) {
2322
const tracker = state.createTracker(info)
2423
let value = tracker.move('[')
2524
value += tracker.move(
26-
state.safe(association(node), {
25+
state.safe(state.associationId(node), {
2726
before: value,
2827
after: ']',
2928
...tracker.current()

lib/handle/image-reference.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* @typedef {import('../types.js').Info} Info
66
*/
77

8-
import {association} from '../util/association.js'
9-
108
imageReference.peek = imageReferencePeek
119

1210
/**
@@ -38,7 +36,7 @@ export function imageReference(node, _, state, info) {
3836
// up making a `shortcut` reference, because then there is no brace output.
3937
// Practically, in that case, there is no content, so it doesn’t matter that
4038
// we’ve tracked one too many characters.
41-
const reference = state.safe(association(node), {
39+
const reference = state.safe(state.associationId(node), {
4240
before: value,
4341
after: ']',
4442
...tracker.current()

lib/handle/link-reference.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* @typedef {import('../types.js').Info} Info
66
*/
77

8-
import {association} from '../util/association.js'
9-
108
linkReference.peek = linkReferencePeek
119

1210
/**
@@ -38,7 +36,7 @@ export function linkReference(node, _, state, info) {
3836
// up making a `shortcut` reference, because then there is no brace output.
3937
// Practically, in that case, there is no content, so it doesn’t matter that
4038
// we’ve tracked one too many characters.
41-
const reference = state.safe(association(node), {
39+
const reference = state.safe(state.associationId(node), {
4240
before: value,
4341
after: ']',
4442
...tracker.current()

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {configure} from './configure.js'
1717
import {handle as handlers} from './handle/index.js'
1818
import {join} from './join.js'
1919
import {unsafe} from './unsafe.js'
20+
import {association} from './util/association.js'
2021
import {containerPhrasing} from './util/container-phrasing.js'
2122
import {containerFlow} from './util/container-flow.js'
2223
import {indentLines} from './util/indent-lines.js'
@@ -38,6 +39,7 @@ export function toMarkdown(tree, options = {}) {
3839
const state = {
3940
enter,
4041
indentLines,
42+
associationId: association,
4143
containerPhrasing: containerPhrasingBound,
4244
containerFlow: containerFlowBound,
4345
createTracker: track,

lib/types.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
22
* @typedef {import('unist').Parent} UnistParent
33
* @typedef {import('unist').Point} Point
4-
* @typedef {import('mdast').Root} Root
4+
* @typedef {import('mdast').Association} Association
55
* @typedef {import('mdast').Content} Content
6-
* @typedef {import('mdast').TopLevelContent} TopLevelContent
76
* @typedef {import('mdast').ListContent} ListContent
87
* @typedef {import('mdast').PhrasingContent} PhrasingContent
8+
* @typedef {import('mdast').Root} Root
9+
* @typedef {import('mdast').TopLevelContent} TopLevelContent
910
* @typedef {import('../index.js').ConstructName} ConstructName
1011
*/
1112

@@ -73,6 +74,28 @@
7374
* @returns {Tracker}
7475
* Tracker.
7576
*
77+
* @callback AssociationId
78+
* Get an identifier from an association to match it to others.
79+
*
80+
* Associations are nodes that match to something else through an ID:
81+
* <https://github.com/syntax-tree/mdast#association>.
82+
*
83+
* The `label` of an association is the string value: character escapes and
84+
* references work, and casing is intact.
85+
* The `identifier` is used to match one association to another:
86+
* controversially, character escapes and references don’t work in this
87+
* matching: `&copy;` does not match `©`, and `\+` does not match `+`.
88+
*
89+
* But casing is ignored (and whitespace) is trimmed and collapsed: ` A\nb`
90+
* matches `a b`.
91+
* So, we do prefer the label when figuring out how we’re going to serialize:
92+
* it has whitespace, casing, and we can ignore most useless character
93+
* escapes and all character references.
94+
* @param {Association} node
95+
* Node that includes an association.
96+
* @returns {string}
97+
* ID.
98+
*
7699
* @callback Map
77100
* Map function to pad a single line.
78101
* @param {string} value
@@ -169,6 +192,8 @@
169192
* Positions of child nodes in their parents.
170193
* @property {IndentLines} indentLines
171194
* Pad serialized markdown.
195+
* @property {AssociationId} associationId
196+
* Get an identifier from an association to match it to others.
172197
* @property {ContainerPhrasing} containerPhrasing
173198
* Serialize the children of a parent that contains phrasing children.
174199
* @property {ContainerFlow} containerFlow

lib/util/association.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
/**
2-
* @typedef {import('mdast').Association} Association
2+
* @typedef {import('../types.js').AssociationId} AssociationId
33
*/
44

55
import {decodeString} from 'micromark-util-decode-string'
66

77
/**
8+
* Get an identifier from an association to match it to others.
9+
*
10+
* Associations are nodes that match to something else through an ID:
11+
* <https://github.com/syntax-tree/mdast#association>.
12+
*
813
* The `label` of an association is the string value: character escapes and
914
* references work, and casing is intact.
10-
* The `identifier` is used to match one association to another: controversially,
11-
* character escapes and references don’t work in this matching: `&copy;` does
12-
* not match `©`, and `\+` does not match `+`.
15+
* The `identifier` is used to match one association to another:
16+
* controversially, character escapes and references don’t work in this
17+
* matching: `&copy;` does not match `©`, and `\+` does not match `+`.
18+
*
1319
* But casing is ignored (and whitespace) is trimmed and collapsed: ` A\nb`
1420
* matches `a b`.
1521
* So, we do prefer the label when figuring out how we’re going to serialize:
16-
* it has whitespace, casing, and we can ignore most useless character escapes
17-
* and all character references.
22+
* it has whitespace, casing, and we can ignore most useless character
23+
* escapes and all character references.
1824
*
19-
* @param {Association} node
20-
* @returns {string}
25+
* @type {AssociationId}
2126
*/
2227
export function association(node) {
2328
if (node.label || !node.identifier) {

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ Info passed around about the current state (TypeScript type).
478478
— stack of constructs we’re in
479479
* `indexStack` (`Array<number>`)
480480
— positions of child nodes in their parents
481+
* `associationId` (`(node: Association) => string`)
482+
— get an identifier from an association to match it to others (see
483+
[`Association`][association])
481484
* `enter` (`(construct: ConstructName) => () => void`)
482485
— enter a construct (returns a corresponding exit function)
483486
(see [`ConstructName`][constructname])
@@ -685,6 +688,8 @@ abide by its terms.
685688

686689
[node]: https://github.com/syntax-tree/mdast#nodes
687690

691+
[association]: https://github.com/syntax-tree/mdast#association
692+
688693
[mdast-util-gfm]: https://github.com/syntax-tree/mdast-util-gfm
689694

690695
[mdast-util-mdx]: https://github.com/syntax-tree/mdast-util-mdx

0 commit comments

Comments
 (0)