-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
75 lines (61 loc) · 1.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*!
* prettify-markdown <https://github.com/jonschlinkert/prettify-markdown>
*
* Copyright (c) 2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var gfm = require('gfm-code-blocks');
module.exports = function prettify(str) {
if (typeof str !== 'string') {
throw new TypeError('prettify-markdown expects a string.');
}
var obj = removeBlocks(str);
str = obj.str;
str = str.split(/(?:\r\n|\n){3,}/).join('\n\n');
str = headings(str);
str = bolded(str);
str = addBlocks(str, obj);
return str.trim();
};
function headings(str) {
var re = /^(#{1,6})\s+([^\n]+)\s+/gm;
return str.replace(re, function (match, $1, $2) {
return $1 + ' ' + $2 + '\n\n';
});
}
function bolded(str) {
var re = /^\s*\*\*([^\n]+)\*\*(?=\n)\s+/gm;
return str.replace(re, function (match, $1) {
return '\n**' + $1 + '**\n\n';
});
}
function removeBlocks(str) {
var codeBlocks = gfm(str);
var len = codeBlocks.length;
var stash = {}, i = 0;
if (len > 0) {
while (len--) {
var key = '__BLOCK' + (i) + '__';
var cur = codeBlocks[i++];
stash[key] = cur;
str = str.split(cur.block).join(key);
}
}
return {str: str, stash: stash};
}
function addBlocks(str, obj) {
var stash = obj.stash;
for (var key in stash) {
var val = stash[key];
if (stash.hasOwnProperty(key)) {
var re = new RegExp('\\s+' + key + '\\s+');
var block = '\n\n';
block += '```' + val.lang + '\n';
block += val.code + '\n';
block += '```\n\n';
str = str.split(re).join(block);
}
}
return str;
}