Skip to content

Commit

Permalink
Fix to not generate blank lines for phrasing roots
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 3, 2023
1 parent e812c79 commit 122101f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 36 deletions.
8 changes: 7 additions & 1 deletion lib/handle/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* @typedef {import('../types.js').Info} Info
*/

import {phrasing} from 'mdast-util-phrasing'
import {containerFlow} from '../util/container-flow.js'
import {containerPhrasing} from '../util/container-phrasing.js'

/**
* @param {Root} node
Expand All @@ -15,5 +17,9 @@ import {containerFlow} from '../util/container-flow.js'
* @returns {string}
*/
export function root(node, _, state, info) {
return containerFlow(node, state, info)
// Note: `html` nodes are ambiguous.
const hasPhrasing = node.children.some((d) => phrasing(d))
const fn = hasPhrasing ? containerPhrasing : containerFlow
// @ts-expect-error: `root`s are supposed to have one type of content
return fn(node, state, info)
}
49 changes: 17 additions & 32 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,46 @@
* @typedef {import('unist').Point} Point
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root | Content} Node
* @typedef {Extract<Node, UnistParent>} Parent
* @typedef {import('mdast').TopLevelContent} TopLevelContent
* @typedef {import('mdast').ListContent} ListContent
* @typedef {import('mdast').PhrasingContent} PhrasingContent
*/

/**
* @typedef {Root | Content} Node
* @typedef {Extract<Node, UnistParent>} Parent
* @typedef {TopLevelContent | ListContent} FlowContent
*
* @typedef TrackFields
* Info on where we are in the document we are generating.
* @property {Point} now
* Current point.
* @property {number} lineShift
* Number of columns each line will be shifted by wrapping nodes.
*/

/**
*
* @typedef SafeFields
* Info on the characters that are around the current thing we are
* generating.
* @property {string} before
* Characters before this (guaranteed to be one, can be more).
* @property {string} after
* Characters after this (guaranteed to be one, can be more).
*/

/**
*
* @typedef {TrackFields & SafeFields} Info
* Info on the surrounding of the node that is serialized.
*/

/**
*
* @callback Enter
* Enter something.
* @param {string} type
* Label, more similar to a micromark event than an mdast node type.
* @returns {Exit}
* Revert.
*/

/**
*
* @callback Exit
* Exit something.
* @returns {void}
* Nothing.
*/

/**
*
* @typedef State
* Info passed around about the current state.
* @property {Array<string>} stack
Expand All @@ -70,9 +65,7 @@
* List marker currently in use.
* @property {string | undefined} bulletLastUsed
* List marker previously in use.
*/

/**
*
* @callback Handle
* Handle a particular node.
* @param {any} node
Expand All @@ -85,16 +78,12 @@
* Info on the surrounding of the node that is serialized.
* @returns {string}
* Serialized markdown representing `node`.
*/

/**
*
* @typedef {Record<string, Handle>} Handlers
* Handle particular nodes.
*
* Each key is a node type, each value its corresponding handler.
*/

/**
*
* @callback Join
* How to join two blocks.
*
Expand Down Expand Up @@ -131,9 +120,7 @@
* > One such example is when returning `0` for two paragraphs, which will
* > result in the text running together, and in the future to be seen as
* > one paragraph.
*/

/**
*
* @typedef Unsafe
* Schema that defines when a character cannot occur.
* @property {string} character
Expand All @@ -153,9 +140,7 @@
* The unsafe pattern (this whole object) compiled as a regex.
*
* This is internal and must not be defined.
*/

/**
*
* @typedef Options
* Configuration (optional).
* @property {'-' | '*' | '+' | null | undefined} [bullet='*']
Expand Down
3 changes: 2 additions & 1 deletion lib/util/container-flow.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* @typedef {import('../types.js').FlowContent} FlowContent
* @typedef {import('../types.js').Node} Node
* @typedef {import('../types.js').Parent} Parent
* @typedef {import('../types.js').State} State
Expand All @@ -8,7 +9,7 @@
import {track} from './track.js'

/**
* @param {Parent} parent
* @param {Parent & {children: Array<FlowContent>}} parent
* @param {State} state
* @param {TrackFields} info
* @returns {string}
Expand Down
3 changes: 2 additions & 1 deletion lib/util/container-phrasing.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* @typedef {import('../types.js').PhrasingContent} PhrasingContent
* @typedef {import('../types.js').Parent} Parent
* @typedef {import('../types.js').Info} Info
* @typedef {import('../types.js').Handle} Handle
Expand All @@ -8,7 +9,7 @@
import {track} from './track.js'

/**
* @param {Parent} parent
* @param {Parent & {children: Array<PhrasingContent>}} parent
* @param {State} state
* @param {Info} info
* @returns {string}
Expand Down
2 changes: 1 addition & 1 deletion lib/util/indent-lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function indentLines(value, map) {
const result = []
let start = 0
let line = 0
/** @type {RegExpExecArray|null} */
/** @type {RegExpExecArray | null} */
let match

while ((match = eol.exec(value))) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@types/mdast": "^3.0.0",
"@types/unist": "^2.0.0",
"longest-streak": "^3.0.0",
"mdast-util-phrasing": "^3.0.0",
"mdast-util-to-string": "^3.0.0",
"micromark-util-decode-string": "^1.0.0",
"unist-util-visit": "^4.0.0",
Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ test('core', () => {
'should support a `root`'
)

assert.equal(
to({
type: 'root',
children: [
{type: 'text', value: 'a'},
{type: 'break'},
{type: 'text', value: 'b'}
]
}),
'a\\\nb\n',
'should not use blank lines between nodes when given phrasing'
)

assert.equal(
to({
type: 'root',
Expand Down

0 comments on commit 122101f

Please sign in to comment.