3 var streak = require('longest-streak');
4 var repeat = require('repeat-string');
5 var pad = require('../util/pad');
9 var FENCE = /([`~])\1{2}/;
12 * Creates indented code when:
14 * - No language tag exists;
15 * - Not in `fences: true` mode;
16 * - A non-empty value exists.
18 * Otherwise, GFM fenced code is created:
24 * When in ``fence: `~` `` mode, uses tildes as fences:
30 * Knows about internal fences (Note: GitHub/Kramdown does
39 function code(node, parent) {
41 var value = node.value;
42 var options = self.options;
43 var marker = options.fence;
44 var language = self.encode(node.lang || '', node);
47 /* Without (needed) fences. */
48 if (!language && !options.fences && value) {
49 /* Throw when pedantic, in a list item which
50 * isn’t compiled using a tab. */
53 parent.type === 'listItem' &&
54 options.listItemIndent !== 'tab' &&
57 self.file.fail('Cannot indent code properly. See http://git.io/vgFvT', node.position);
63 fence = streak(value, marker) + 1;
65 /* Fix GFM / RedCarpet bug, where fence-like characters
66 * inside fenced code can exit a code-block.
67 * Yes, even when the outer fence uses different
68 * characters, or is longer.
69 * Thus, we can only pad the code to make it work. */
70 if (FENCE.test(value)) {
71 value = pad(value, 1);
74 fence = repeat(marker, Math.max(fence, 3));
76 return fence + language + '\n' + value + '\n' + fence;