Skip to content

Commit 31ac691

Browse files
authored
stringify: add support for tight definitions
Related-to GH-55. Closes GH-501. Reviewed-by: Titus Wormer <[email protected]>
1 parent 31905d1 commit 31ac691

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

packages/remark-stringify/lib/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
tablePipeAlign: true,
1212
stringLength: stringLength,
1313
incrementListMarker: true,
14+
tightDefinitions: false,
1415
fences: false,
1516
fence: '`',
1617
bullet: '-',

packages/remark-stringify/lib/macro/block.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function block(node) {
1616
var options = self.options
1717
var fences = options.fences
1818
var gap = options.commonmark ? comment : triple
19+
var definitionGap = options.tightDefinitions ? lineFeed : blank
1920
var values = []
2021
var children = node.children
2122
var length = children.length
@@ -42,6 +43,11 @@ function block(node) {
4243
(child.type === 'code' && !child.lang && !fences))
4344
) {
4445
values.push(gap)
46+
} else if (
47+
previous.type === 'definition' &&
48+
child.type === 'definition'
49+
) {
50+
values.push(definitionGap)
4551
} else {
4652
values.push(blank)
4753
}

packages/remark-stringify/readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ Increment ordered list item numbers (`boolean`, default: `true`).
207207

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

210+
###### `options.tightDefinitions`
211+
212+
Separate definitions with a single line feed (`boolean`, default: `false`).
213+
214+
When `false`, definitions will have blank lines between them, similar to other
215+
blocks.
216+
210217
###### `options.rule`
211218

212219
Marker to use for thematic breaks / horizontal rules (`'-'`, `'*'`, or `'_'`,

packages/remark-stringify/test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ test('remark().stringify(ast, file)', function (t) {
187187
'should throw when `options.incrementListMarker` is not a boolean'
188188
)
189189

190+
t.throws(
191+
function () {
192+
unified()
193+
.use(stringify)
194+
.data('settings', {tightDefinitions: 'blank'})
195+
.stringify(empty())
196+
},
197+
/options\.tightDefinitions/,
198+
'should throw when `options.tightDefinitions` is not a boolean'
199+
)
200+
190201
t.throws(
191202
function () {
192203
unified()
@@ -1259,6 +1270,27 @@ test('stringify escapes', function (t) {
12591270
t.end()
12601271
})
12611272

1273+
test('definition separators', function (t) {
1274+
var tree = u('root', [
1275+
u('definition', {identifier: 'foo', url: 'first'}),
1276+
u('definition', {identifier: 'bar', url: 'second'})
1277+
])
1278+
1279+
t.equal(
1280+
toString(tree, {tightDefinitions: false}),
1281+
'[foo]: first\n\n[bar]: second\n',
1282+
'blank line between definitions'
1283+
)
1284+
1285+
t.equal(
1286+
toString(tree, {tightDefinitions: true}),
1287+
'[foo]: first\n[bar]: second\n',
1288+
'no blank line between definitions'
1289+
)
1290+
1291+
t.end()
1292+
})
1293+
12621294
function toString(value, options) {
12631295
var tree = typeof value === 'string' ? u('text', value) : value
12641296

packages/remark-stringify/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ declare namespace remarkStringify {
3232
bullet: '-' | '*' | '+'
3333
listItemIndent: 'tab' | '1' | 'mixed'
3434
incrementListMarker: boolean
35+
tightDefinitions: boolean
3536
rule: '-' | '_' | '*'
3637
ruleRepetition: number
3738
ruleSpaces: boolean

0 commit comments

Comments
 (0)