Skip to content

Commit d10c763

Browse files
committed
Fix html (text) from being seen as html (flow)
Closes GH-15.
1 parent 9274f41 commit d10c763

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

lib/handle/html.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
module.exports = html
2+
html.peek = htmlPeek
23

34
function html(node) {
45
return node.value || ''
56
}
7+
8+
function htmlPeek() {
9+
return '<'
10+
}

lib/util/container-phrasing.js

+15
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,27 @@ function phrasing(parent, context, safeOptions) {
2525
after = safeOptions.after
2626
}
2727

28+
// In some cases, html (text) can be found in phrasing right after an eol.
29+
// When we’d serialize that, in most cases that would be seen as html
30+
// (flow).
31+
// As we can’t escape or so to prevent it from happening, we take a somewhat
32+
// reasonable approach: replace that eol with a space.
33+
// See: <https://github.com/syntax-tree/mdast-util-to-markdown/issues/15>
34+
if ((before === '\r' || before === '\n') && child.type === 'html') {
35+
results[results.length - 1] = results[results.length - 1].replace(
36+
/(\r?\n|\r)$/,
37+
' '
38+
)
39+
before = ' '
40+
}
41+
2842
results.push(
2943
context.handle(child, parent, context, {
3044
before: before,
3145
after: after
3246
})
3347
)
48+
3449
before = results[results.length - 1].slice(-1)
3550
}
3651

test.js

+37
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,43 @@ test('html', function (t) {
10671067
t.equal(to({type: 'html'}), '', 'should support a void html')
10681068
t.equal(to({type: 'html', value: ''}), '', 'should support an empty html')
10691069
t.equal(to({type: 'html', value: 'a\nb'}), 'a\nb\n', 'should support html')
1070+
1071+
t.equal(
1072+
to({
1073+
type: 'paragraph',
1074+
children: [
1075+
{type: 'text', value: 'a\n'},
1076+
{type: 'html', value: '<div>'}
1077+
]
1078+
}),
1079+
'a <div>\n',
1080+
'should prevent html (text) from becoming html (flow) (1)'
1081+
)
1082+
1083+
t.equal(
1084+
to({
1085+
type: 'paragraph',
1086+
children: [
1087+
{type: 'text', value: 'a\r'},
1088+
{type: 'html', value: '<div>'}
1089+
]
1090+
}),
1091+
'a <div>\n',
1092+
'should prevent html (text) from becoming html (flow) (2)'
1093+
)
1094+
1095+
t.equal(
1096+
to({
1097+
type: 'paragraph',
1098+
children: [
1099+
{type: 'text', value: 'a\r\n'},
1100+
{type: 'html', value: '<div>'}
1101+
]
1102+
}),
1103+
'a <div>\n',
1104+
'should prevent html (text) from becoming html (flow) (3)'
1105+
)
1106+
10701107
t.end()
10711108
})
10721109

0 commit comments

Comments
 (0)