Skip to content

Commit 437f5a2

Browse files
committed
Add error on misconfigured fence
1 parent cfdd274 commit 437f5a2

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

lib/handle/code.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ module.exports = code
33
var repeat = require('repeat-string')
44
var streak = require('longest-streak')
55
var formatCodeAsIndented = require('../util/format-code-as-indented')
6+
var checkFence = require('../util/check-fence')
67
var indentLines = require('../util/indent-lines')
78
var safe = require('../util/safe')
89

910
function code(node, _, context) {
10-
var marker = context.options.fence || '`'
11+
var marker = checkFence(context)
1112
var raw = node.value || ''
1213
var suffix = marker === '`' ? 'GraveAccent' : 'Tilde'
1314
var value

lib/util/check-fence.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = checkFence
2+
3+
function checkFence(context) {
4+
var marker = context.options.fence || '`'
5+
6+
if (marker !== '`' && marker !== '~') {
7+
throw new Error(
8+
'Cannot serialize code with `' +
9+
marker +
10+
'` for `options.fence`, expected `` ` `` or `~`'
11+
)
12+
}
13+
14+
return marker
15+
}

test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,14 @@ test('break', function (t) {
509509
test('code (flow)', function (t) {
510510
t.equal(to({type: 'code'}), '```\n```', 'should support empty code')
511511

512+
t.throws(
513+
function () {
514+
to({type: 'code'}, {fence: '+'})
515+
},
516+
/Cannot serialize code with `\+` for `options\.fence`, expected `` ` `` or `~`/,
517+
'should throw on when given an incorrect `fence`'
518+
)
519+
512520
t.equal(
513521
to({type: 'code', value: 'a'}),
514522
' a',

0 commit comments

Comments
 (0)