-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
131 lines (105 loc) · 4.08 KB
/
app.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/// <reference path="./option.ts" />
/// <reference path="./helpers/escape.ts" />
/// <reference path="./helpers/helpers.ts" />
/// <reference path="./parser/block.ts" />
/// <reference path="./parser/inline.ts" />
/// <reference path="./parser/inlineLexer.ts" />
/// <reference path="./parser/lexer.ts" />
/// <reference path="./parser/parser.ts" />
/**
* marked - a markdown parser
* Copyright (c) 2011-2018, Christopher Jeffrey. (MIT Licensed)
* https://github.com/markedjs/marked
*/
const marked: markedjs.Imarked = (function () {
let markedCallback = function (src: string, opt: markedjs.option, callback: markedjs.markedCallback) {
var highlight = opt.highlight,
tokens: markedjs.Itoken[],
pending: number,
i = 0;
try {
tokens = new markedjs.Lexer(opt).lex(src);
} catch (e) {
return callback(e);
}
pending = tokens.length;
var done = function (err: string = null) {
if (err) {
(<markedjs.option>opt).highlight = highlight;
return callback(err);
}
var out: string;
try {
out = new markedjs.parser(<markedjs.option>opt).parse(tokens);
} catch (e) {
err = e;
}
(<markedjs.option>opt).highlight = highlight;
return err
? callback(err)
: callback(null, out);
};
if (!highlight || highlight.length < 3) {
return done();
}
delete opt.highlight;
if (!pending) return done();
for (; i < tokens.length; i++) {
(function (token: markedjs.Itoken) {
if (token.type !== 'code') {
return --pending || done();
}
return highlight(token.text, token.lang, function (err, code) {
if (err) return done(err);
if (code == null || code === token.text) {
return --pending || done();
}
token.text = <string>code;
token.escaped = true;
--pending || done();
});
})(tokens[i]);
}
}
let marked: markedjs.Imarked = <any>function marked(src: string,
opt: markedjs.option | markedjs.markedCallback = markedjs.option.Defaults,
callback: markedjs.markedCallback = null): string {
// throw error in case of non string input
if (typeof src === 'undefined' || src === null) {
throw new Error('marked(): input parameter is undefined or null');
}
if (typeof src !== 'string') {
throw new Error('marked(): input parameter is of type '
+ Object.prototype.toString.call(src) + ', string expected');
}
if (callback || typeof opt === 'function') {
if (!callback) {
callback = <markedjs.markedCallback>opt;
opt = null;
}
opt = <markedjs.option>markedjs.helpers.merge({}, markedjs.option.Defaults, opt || {});
markedCallback(src, opt, callback);
} else {
try {
if (opt) opt = <markedjs.option>markedjs.helpers.merge({}, markedjs.option.Defaults, opt);
let lexer = new markedjs.Lexer(opt)
let tokens = lexer.lex(src);
let mdparser = new markedjs.parser(opt);
let output = mdparser.parse(tokens);
if (opt.debug) {
console.log(output);
}
return output;
} catch (e) {
e.message += '\nPlease report this to https://github.com/markedjs/marked.';
if ((opt || markedjs.option.Defaults).silent) {
return '<p>An error occurred:</p><pre>'
+ markedjs.helpers.escape.doescape(e.message + '', true)
+ '</pre>';
}
throw e;
}
}
}
return marked;
})();