Skip to content

Commit

Permalink
Add indentLines helper on state
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 3, 2023
1 parent 5142972 commit a638e2a
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 20 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ export type {
Handlers,
Info,
Join,
Map,
Options,
State,
Unsafe,
Map
Unsafe
} from './lib/types.js'
// Deprecated.
export type SafeOptions = Info
Expand Down
2 changes: 1 addition & 1 deletion lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
export function configure(base, extension) {
let index = -1
/** @type {string} */
/** @type {keyof Options} */
let key

// First do subextensions.
Expand Down
8 changes: 5 additions & 3 deletions lib/handle/blockquote.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* @typedef {import('../types.js').Parent} Parent
* @typedef {import('../types.js').State} State
* @typedef {import('../types.js').Info} Info
* @typedef {import('../util/indent-lines.js').Map} Map
* @typedef {import('../types.js').Map} Map
*/

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

/**
Expand All @@ -22,7 +21,10 @@ export function blockquote(node, _, state, info) {
const tracker = track(info)
tracker.move('> ')
tracker.shift(2)
const value = indentLines(containerFlow(node, state, tracker.current()), map)
const value = state.indentLines(
containerFlow(node, state, tracker.current()),
map
)
exit()
return value
}
Expand Down
5 changes: 2 additions & 3 deletions lib/handle/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
* @typedef {import('../types.js').Parent} Parent
* @typedef {import('../types.js').State} State
* @typedef {import('../types.js').Info} Info
* @typedef {import('../util/indent-lines.js').Map} Map
* @typedef {import('../types.js').Map} Map
*/

import {longestStreak} from 'longest-streak'
import {formatCodeAsIndented} from '../util/format-code-as-indented.js'
import {checkFence} from '../util/check-fence.js'
import {indentLines} from '../util/indent-lines.js'
import {safe} from '../util/safe.js'
import {track} from '../util/track.js'

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

if (formatCodeAsIndented(node, state)) {
const exit = state.enter('codeIndented')
const value = indentLines(raw, map)
const value = state.indentLines(raw, map)
exit()
return value
}
Expand Down
8 changes: 5 additions & 3 deletions lib/handle/list-item.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @typedef {import('mdast').ListItem} ListItem
* @typedef {import('../util/indent-lines.js').Map} Map
* @typedef {import('../types.js').Map} Map
* @typedef {import('../types.js').Parent} Parent
* @typedef {import('../types.js').State} State
* @typedef {import('../types.js').Info} Info
Expand All @@ -9,7 +9,6 @@
import {checkBullet} from '../util/check-bullet.js'
import {checkListItemIndent} from '../util/check-list-item-indent.js'
import {containerFlow} from '../util/container-flow.js'
import {indentLines} from '../util/indent-lines.js'
import {track} from '../util/track.js'

/**
Expand Down Expand Up @@ -49,7 +48,10 @@ export function listItem(node, parent, state, info) {
tracker.move(bullet + ' '.repeat(size - bullet.length))
tracker.shift(size)
const exit = state.enter('listItem')
const value = indentLines(containerFlow(node, state, tracker.current()), map)
const value = state.indentLines(
containerFlow(node, state, tracker.current()),
map
)
exit()

return value
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {configure} from './configure.js'
import {handle as handlers} from './handle/index.js'
import {join} from './join.js'
import {unsafe} from './unsafe.js'
import {indentLines} from './util/indent-lines.js'

/**
* Turn an mdast syntax tree into markdown.
Expand All @@ -26,6 +27,7 @@ export function toMarkdown(tree, options = {}) {
/** @type {State} */
const state = {
enter,
indentLines,
stack: [],
unsafe: [],
join: [],
Expand Down
23 changes: 23 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@
* @typedef {TrackFields & SafeFields} Info
* Info on the surrounding of the node that is serialized.
*
* @callback Map
* Map function to pad a single line.
* @param {string} value
* A single line of serialized markdown.
* @param {number} line
* Line number relative to the fragment.
* @param {boolean} blank
* Whether the line is considered blank in markdown.
* @returns {string}
* Padded line.
*
* @callback IndentLines
* Pad serialized markdown.
* @param {string} value
* Whole fragment of serialized markdown.
* @param {Map} map
* Map function.
* @returns {string}
* Padded value.
*
* @callback Enter
* Enter something.
* @param {ConstructName} name
Expand All @@ -50,6 +71,8 @@
* Stack of constructs we’re in.
* @property {Array<number>} indexStack
* Positions of child nodes in their parents.
* @property {IndentLines} indentLines
* Pad serialized markdown.
* @property {Enter} enter
* Enter a construct (returns a corresponding exit function).
* @property {Options} options
Expand Down
10 changes: 2 additions & 8 deletions lib/util/indent-lines.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
/**
* @callback Map
* @param {string} value
* @param {number} line
* @param {boolean} blank
* @returns {string}
* @typedef {import('../types.js').IndentLines} IndentLines
*/

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

/**
* @param {string} value
* @param {Map} map
* @returns {string}
* @type {IndentLines}
*/
export function indentLines(value, map) {
/** @type {Array<string>} */
Expand Down
20 changes: 20 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ after a list, in which case a comment will be injected to break them up:
### `Map`

Map function to pad a single line (TypeScript type).

###### Parameters

* `value` (`string`)
— a single line of serialized markdown
* `line` (`number`)
— line number relative to the fragment
* `blank` (`boolean`)
— whether the line is considered blank in markdown

###### Returns

Padded line (`string`).

### `Options`

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

[join]: #join

[map]: #map

[options]: #options

[state]: #state
Expand Down

0 comments on commit a638e2a

Please sign in to comment.