Skip to content

Commit a638e2a

Browse files
committed
Add indentLines helper on state
1 parent 5142972 commit a638e2a

File tree

9 files changed

+62
-20
lines changed

9 files changed

+62
-20
lines changed

index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,10 @@ export type {
314314
Handlers,
315315
Info,
316316
Join,
317+
Map,
317318
Options,
318319
State,
319-
Unsafe,
320-
Map
320+
Unsafe
321321
} from './lib/types.js'
322322
// Deprecated.
323323
export type SafeOptions = Info

lib/configure.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
export function configure(base, extension) {
1212
let index = -1
13-
/** @type {string} */
13+
/** @type {keyof Options} */
1414
let key
1515

1616
// First do subextensions.

lib/handle/blockquote.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* @typedef {import('../types.js').Parent} Parent
44
* @typedef {import('../types.js').State} State
55
* @typedef {import('../types.js').Info} Info
6-
* @typedef {import('../util/indent-lines.js').Map} Map
6+
* @typedef {import('../types.js').Map} Map
77
*/
88

99
import {containerFlow} from '../util/container-flow.js'
10-
import {indentLines} from '../util/indent-lines.js'
1110
import {track} from '../util/track.js'
1211

1312
/**
@@ -22,7 +21,10 @@ export function blockquote(node, _, state, info) {
2221
const tracker = track(info)
2322
tracker.move('> ')
2423
tracker.shift(2)
25-
const value = indentLines(containerFlow(node, state, tracker.current()), map)
24+
const value = state.indentLines(
25+
containerFlow(node, state, tracker.current()),
26+
map
27+
)
2628
exit()
2729
return value
2830
}

lib/handle/code.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
* @typedef {import('../types.js').Parent} Parent
44
* @typedef {import('../types.js').State} State
55
* @typedef {import('../types.js').Info} Info
6-
* @typedef {import('../util/indent-lines.js').Map} Map
6+
* @typedef {import('../types.js').Map} Map
77
*/
88

99
import {longestStreak} from 'longest-streak'
1010
import {formatCodeAsIndented} from '../util/format-code-as-indented.js'
1111
import {checkFence} from '../util/check-fence.js'
12-
import {indentLines} from '../util/indent-lines.js'
1312
import {safe} from '../util/safe.js'
1413
import {track} from '../util/track.js'
1514

@@ -27,7 +26,7 @@ export function code(node, _, state, info) {
2726

2827
if (formatCodeAsIndented(node, state)) {
2928
const exit = state.enter('codeIndented')
30-
const value = indentLines(raw, map)
29+
const value = state.indentLines(raw, map)
3130
exit()
3231
return value
3332
}

lib/handle/list-item.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @typedef {import('mdast').ListItem} ListItem
3-
* @typedef {import('../util/indent-lines.js').Map} Map
3+
* @typedef {import('../types.js').Map} Map
44
* @typedef {import('../types.js').Parent} Parent
55
* @typedef {import('../types.js').State} State
66
* @typedef {import('../types.js').Info} Info
@@ -9,7 +9,6 @@
99
import {checkBullet} from '../util/check-bullet.js'
1010
import {checkListItemIndent} from '../util/check-list-item-indent.js'
1111
import {containerFlow} from '../util/container-flow.js'
12-
import {indentLines} from '../util/indent-lines.js'
1312
import {track} from '../util/track.js'
1413

1514
/**
@@ -49,7 +48,10 @@ export function listItem(node, parent, state, info) {
4948
tracker.move(bullet + ' '.repeat(size - bullet.length))
5049
tracker.shift(size)
5150
const exit = state.enter('listItem')
52-
const value = indentLines(containerFlow(node, state, tracker.current()), map)
51+
const value = state.indentLines(
52+
containerFlow(node, state, tracker.current()),
53+
map
54+
)
5355
exit()
5456

5557
return value

lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {configure} from './configure.js'
1111
import {handle as handlers} from './handle/index.js'
1212
import {join} from './join.js'
1313
import {unsafe} from './unsafe.js'
14+
import {indentLines} from './util/indent-lines.js'
1415

1516
/**
1617
* Turn an mdast syntax tree into markdown.
@@ -26,6 +27,7 @@ export function toMarkdown(tree, options = {}) {
2627
/** @type {State} */
2728
const state = {
2829
enter,
30+
indentLines,
2931
stack: [],
3032
unsafe: [],
3133
join: [],

lib/types.js

+23
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@
3232
* @typedef {TrackFields & SafeFields} Info
3333
* Info on the surrounding of the node that is serialized.
3434
*
35+
* @callback Map
36+
* Map function to pad a single line.
37+
* @param {string} value
38+
* A single line of serialized markdown.
39+
* @param {number} line
40+
* Line number relative to the fragment.
41+
* @param {boolean} blank
42+
* Whether the line is considered blank in markdown.
43+
* @returns {string}
44+
* Padded line.
45+
*
46+
47+
* @callback IndentLines
48+
* Pad serialized markdown.
49+
* @param {string} value
50+
* Whole fragment of serialized markdown.
51+
* @param {Map} map
52+
* Map function.
53+
* @returns {string}
54+
* Padded value.
55+
*
3556
* @callback Enter
3657
* Enter something.
3758
* @param {ConstructName} name
@@ -50,6 +71,8 @@
5071
* Stack of constructs we’re in.
5172
* @property {Array<number>} indexStack
5273
* Positions of child nodes in their parents.
74+
* @property {IndentLines} indentLines
75+
* Pad serialized markdown.
5376
* @property {Enter} enter
5477
* Enter a construct (returns a corresponding exit function).
5578
* @property {Options} options

lib/util/indent-lines.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
/**
2-
* @callback Map
3-
* @param {string} value
4-
* @param {number} line
5-
* @param {boolean} blank
6-
* @returns {string}
2+
* @typedef {import('../types.js').IndentLines} IndentLines
73
*/
84

95
const eol = /\r?\n|\r/g
106

117
/**
12-
* @param {string} value
13-
* @param {Map} map
14-
* @returns {string}
8+
* @type {IndentLines}
159
*/
1610
export function indentLines(value, map) {
1711
/** @type {Array<string>} */

readme.md

+20
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,21 @@ after a list, in which case a comment will be injected to break them up:
277277
278278
### `Map`
279279

280+
Map function to pad a single line (TypeScript type).
281+
282+
###### Parameters
283+
284+
* `value` (`string`)
285+
— a single line of serialized markdown
286+
* `line` (`number`)
287+
— line number relative to the fragment
288+
* `blank` (`boolean`)
289+
— whether the line is considered blank in markdown
290+
291+
###### Returns
292+
293+
Padded line (`string`).
294+
280295
### `Options`
281296

282297
Configuration (TypeScript type).
@@ -447,6 +462,9 @@ Info passed around about the current state (TypeScript type).
447462
— positions of child nodes in their parents
448463
* `enter` (`(construct: ConstructName) => () => void`)
449464
— enter a construct (returns a corresponding exit function)
465+
(see [`ConstructName`][constructname])
466+
* `indentLines` (`(value: string, map: Map) => string`)
467+
— pad serialized markdown (see [`Map`][map])
450468
* `options` ([`Options`][options])
451469
— applied user configuration
452470
* `unsafe` ([`Array<Unsafe>`][unsafe])
@@ -647,6 +665,8 @@ abide by its terms.
647665

648666
[join]: #join
649667

668+
[map]: #map
669+
650670
[options]: #options
651671

652672
[state]: #state

0 commit comments

Comments
 (0)