UNPKG

1.35 kBJavaScriptView Raw
1/*!
2 * gulp-format-md <https://github.com/jonschlinkert/gulp-format-md>
3 *
4 * Copyright (c) 2015-present, Jon Schlinkert.
5 * Licensed under the MIT License.
6 */
7
8'use strict';
9
10const sections = require('sections');
11const Remarkable = require('remarkable');
12const prettify = require('pretty-remarkable');
13const utils = require('./utils');
14
15const 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
22format.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 * Fix params bug
36 */
37
38function fixParam(str) {
39 return str.split('__{_}_*').join('**{any}**');
40}
41
42/**
43 * Instantiate `Remarkable` and use the `prettify` plugin
44 * on the given `str`.
45 *
46 * @param {String} `str`
47 * @param {Object} `options`
48 * @return {String}
49 */
50
51function prettyRemarkable(str, options) {
52 return new Remarkable(options).use(prettify).render(str);
53}
54
55/**
56 * Expose `format`
57 */
58
59module.exports = format;