1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | const sections = require('sections');
|
11 | const Remarkable = require('remarkable');
|
12 | const prettify = require('pretty-remarkable');
|
13 | const utils = require('./utils');
|
14 |
|
15 | const format = (file, options) => {
|
16 | let opts = Object.assign({ author: {} }, options, file.options, file.data);
|
17 | let str = format.prettify(file.contents.toString(), opts);
|
18 | file.contents = Buffer.from(str);
|
19 | return file;
|
20 | };
|
21 |
|
22 | format.prettify = (str, options = {}) => {
|
23 | if (options.stripEmpty !== false && /^\n#/gm.test(str) && /^\n[^#]/gm.test(str)) {
|
24 | str = sections.format(str);
|
25 | }
|
26 | let res = fixParam(prettyRemarkable(str, options));
|
27 | let output = utils.escapePipesInTables(res).trim();
|
28 | if (options.newline === true) {
|
29 | output += '\n';
|
30 | }
|
31 | return output;
|
32 | };
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function fixParam(str) {
|
39 | return str.split('__{_}_*').join('**{any}**');
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function prettyRemarkable(str, options) {
|
52 | return new Remarkable(options).use(prettify).render(str);
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | module.exports = format;
|