Skip to content

Commit

Permalink
stringify: add support for tight definitions
Browse files Browse the repository at this point in the history
Related-to GH-55.
Closes GH-501.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
spl authored Jun 8, 2020
1 parent 31905d1 commit 31ac691
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/remark-stringify/lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
tablePipeAlign: true,
stringLength: stringLength,
incrementListMarker: true,
tightDefinitions: false,
fences: false,
fence: '`',
bullet: '-',
Expand Down
6 changes: 6 additions & 0 deletions packages/remark-stringify/lib/macro/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function block(node) {
var options = self.options
var fences = options.fences
var gap = options.commonmark ? comment : triple
var definitionGap = options.tightDefinitions ? lineFeed : blank
var values = []
var children = node.children
var length = children.length
Expand All @@ -42,6 +43,11 @@ function block(node) {
(child.type === 'code' && !child.lang && !fences))
) {
values.push(gap)
} else if (
previous.type === 'definition' &&
child.type === 'definition'
) {
values.push(definitionGap)
} else {
values.push(blank)
}
Expand Down
7 changes: 7 additions & 0 deletions packages/remark-stringify/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ Increment ordered list item numbers (`boolean`, default: `true`).

When `false`, all list item numbers will be the same.

###### `options.tightDefinitions`

Separate definitions with a single line feed (`boolean`, default: `false`).

When `false`, definitions will have blank lines between them, similar to other
blocks.

###### `options.rule`

Marker to use for thematic breaks / horizontal rules (`'-'`, `'*'`, or `'_'`,
Expand Down
32 changes: 32 additions & 0 deletions packages/remark-stringify/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ test('remark().stringify(ast, file)', function (t) {
'should throw when `options.incrementListMarker` is not a boolean'
)

t.throws(
function () {
unified()
.use(stringify)
.data('settings', {tightDefinitions: 'blank'})
.stringify(empty())
},
/options\.tightDefinitions/,
'should throw when `options.tightDefinitions` is not a boolean'
)

t.throws(
function () {
unified()
Expand Down Expand Up @@ -1259,6 +1270,27 @@ test('stringify escapes', function (t) {
t.end()
})

test('definition separators', function (t) {
var tree = u('root', [
u('definition', {identifier: 'foo', url: 'first'}),
u('definition', {identifier: 'bar', url: 'second'})
])

t.equal(
toString(tree, {tightDefinitions: false}),
'[foo]: first\n\n[bar]: second\n',
'blank line between definitions'
)

t.equal(
toString(tree, {tightDefinitions: true}),
'[foo]: first\n[bar]: second\n',
'no blank line between definitions'
)

t.end()
})

function toString(value, options) {
var tree = typeof value === 'string' ? u('text', value) : value

Expand Down
1 change: 1 addition & 0 deletions packages/remark-stringify/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare namespace remarkStringify {
bullet: '-' | '*' | '+'
listItemIndent: 'tab' | '1' | 'mixed'
incrementListMarker: boolean
tightDefinitions: boolean
rule: '-' | '_' | '*'
ruleRepetition: number
ruleSpaces: boolean
Expand Down

0 comments on commit 31ac691

Please sign in to comment.