Skip to content

Commit 51db1f9

Browse files
committed
Fix breaks from occurring where they cannot
Closes GH-17.
1 parent 6b68e18 commit 51db1f9

File tree

6 files changed

+72
-33
lines changed

6 files changed

+72
-33
lines changed

lib/handle/break.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
module.exports = hardBreak
22

3-
function hardBreak() {
3+
var patternInScope = require('../util/pattern-in-scope')
4+
5+
function hardBreak(node, _, context, safe) {
6+
var index = -1
7+
8+
while (++index < context.unsafe.length) {
9+
// If we can’t put eols in this construct (setext headings, tables), use a
10+
// space instead.
11+
if (
12+
context.unsafe[index].character === '\n' &&
13+
patternInScope(context.stack, context.unsafe[index])
14+
) {
15+
return /[ \t]/.test(safe.before) ? '' : ' '
16+
}
17+
}
18+
419
return '\\\n'
520
}

lib/handle/inline-code.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = inlineCode
22
inlineCode.peek = inlineCodePeek
33

4-
var compilePattern = require('../util/compile-pattern')
4+
var patternCompile = require('../util/pattern-compile')
55

66
function inlineCode(node, parent, context) {
77
var value = node.value || ''
@@ -44,7 +44,7 @@ function inlineCode(node, parent, context) {
4444
// CR.
4545
if (!pattern.atBreak) continue
4646

47-
expression = compilePattern(pattern)
47+
expression = patternCompile(pattern)
4848

4949
while ((match = expression.exec(value))) {
5050
position = match.index

lib/util/compile-pattern.js renamed to lib/util/pattern-compile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
module.exports = compilePattern
1+
module.exports = patternCompile
22

3-
function compilePattern(pattern) {
3+
function patternCompile(pattern) {
44
var before
55
var after
66

lib/util/pattern-in-scope.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = patternInScope
2+
3+
function patternInScope(stack, pattern) {
4+
return (
5+
listInScope(stack, pattern.inConstruct, true) &&
6+
!listInScope(stack, pattern.notInConstruct)
7+
)
8+
}
9+
10+
function listInScope(stack, list, none) {
11+
var index
12+
13+
if (!list) {
14+
return none
15+
}
16+
17+
if (typeof list === 'string') {
18+
list = [list]
19+
}
20+
21+
index = -1
22+
23+
while (++index < list.length) {
24+
if (stack.indexOf(list[index]) !== -1) {
25+
return true
26+
}
27+
}
28+
29+
return false
30+
}

lib/util/safe.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = safe
22

3-
var compilePattern = require('./compile-pattern')
3+
var patternCompile = require('./pattern-compile')
4+
var patternInScope = require('./pattern-in-scope')
45

56
function safe(context, input, config) {
67
var value = (config.before || '') + (input || '') + (config.after || '')
@@ -20,14 +21,11 @@ function safe(context, input, config) {
2021
while (++index < context.unsafe.length) {
2122
pattern = context.unsafe[index]
2223

23-
if (
24-
!inScope(context.stack, pattern.inConstruct, true) ||
25-
inScope(context.stack, pattern.notInConstruct)
26-
) {
24+
if (!patternInScope(context.stack, pattern)) {
2725
continue
2826
}
2927

30-
expression = compilePattern(pattern)
28+
expression = patternCompile(pattern)
3129

3230
while ((match = expression.exec(value))) {
3331
// Often, patterns which depend on a character before or after it, such
@@ -118,28 +116,6 @@ function safe(context, input, config) {
118116
return result.join('')
119117
}
120118

121-
function inScope(stack, list, none) {
122-
var index
123-
124-
if (!list) {
125-
return none
126-
}
127-
128-
if (typeof list === 'string') {
129-
list = [list]
130-
}
131-
132-
index = -1
133-
134-
while (++index < list.length) {
135-
if (stack.indexOf(list[index]) !== -1) {
136-
return true
137-
}
138-
}
139-
140-
return false
141-
}
142-
143119
function numerical(a, b) {
144120
return a - b
145121
}

test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,24 @@ test('blockquote', function (t) {
571571
test('break', function (t) {
572572
t.equal(to({type: 'break'}), '\\\n', 'should support a break')
573573

574+
t.equal(
575+
to(from('a \nb\n=\n')),
576+
'# a b\n',
577+
'should serialize breaks in heading (atx) as a space'
578+
)
579+
580+
t.equal(
581+
to(from('a \\\nb\n=\n')),
582+
'# a b\n',
583+
'should serialize breaks in heading (atx) as nothing when preceded by a space'
584+
)
585+
586+
t.equal(
587+
to(from('a \nb\n=\n'), {setext: true}),
588+
'a\\\nb\n=\n',
589+
'should serialize breaks in heading (setext)'
590+
)
591+
574592
t.end()
575593
})
576594

0 commit comments

Comments
 (0)