.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-stringify / lib / visitors / code.js
1 'use strict';
2
3 var streak = require('longest-streak');
4 var repeat = require('repeat-string');
5 var pad = require('../util/pad');
6
7 module.exports = code;
8
9 var FENCE = /([`~])\1{2}/;
10
11 /* Stringify code.
12  * Creates indented code when:
13  *
14  * - No language tag exists;
15  * - Not in `fences: true` mode;
16  * - A non-empty value exists.
17  *
18  * Otherwise, GFM fenced code is created:
19  *
20  *     ```js
21  *     foo();
22  *     ```
23  *
24  * When in ``fence: `~` `` mode, uses tildes as fences:
25  *
26  *     ~~~js
27  *     foo();
28  *     ~~~
29  *
30  * Knows about internal fences (Note: GitHub/Kramdown does
31  * not support this):
32  *
33  *     ````javascript
34  *     ```markdown
35  *     foo
36  *     ```
37  *     ````
38  */
39 function code(node, parent) {
40   var self = this;
41   var value = node.value;
42   var options = self.options;
43   var marker = options.fence;
44   var language = self.encode(node.lang || '', node);
45   var fence;
46
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. */
51     if (
52       parent &&
53       parent.type === 'listItem' &&
54       options.listItemIndent !== 'tab' &&
55       options.pedantic
56     ) {
57       self.file.fail('Cannot indent code properly. See http://git.io/vgFvT', node.position);
58     }
59
60     return pad(value, 1);
61   }
62
63   fence = streak(value, marker) + 1;
64
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);
72   }
73
74   fence = repeat(marker, Math.max(fence, 3));
75
76   return fence + language + '\n' + value + '\n' + fence;
77 }