Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stringify: add option to not put blank lines between definitions #501

Merged
merged 4 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
wooorm marked this conversation as resolved.
Show resolved Hide resolved
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