Skip to content

Commit

Permalink
🗜️ build [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkedJS bot committed May 14, 2020
1 parent 5ef872b commit 05f2fd6
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 103 deletions.
126 changes: 87 additions & 39 deletions lib/marked.esm.js
Expand Up @@ -32,6 +32,7 @@ function getDefaults() {
smartLists: false,
smartypants: false,
tokenizer: null,
walkTokens: null,
xhtml: false
};
}
Expand Down Expand Up @@ -565,6 +566,7 @@ var Tokenizer_1 = class Tokenizer {
}

list.items.push({
type: 'list_item',
raw,
task: istask,
checked: ischecked,
Expand Down Expand Up @@ -2145,39 +2147,33 @@ function marked(src, opt, callback) {
+ Object.prototype.toString.call(src) + ', string expected');
}

if (callback || typeof opt === 'function') {
if (!callback) {
callback = opt;
opt = null;
}
if (typeof opt === 'function') {
callback = opt;
opt = null;
}

opt = merge$2({}, marked.defaults, opt || {});
checkSanitizeDeprecation$1(opt);
opt = merge$2({}, marked.defaults, opt || {});
checkSanitizeDeprecation$1(opt);

if (callback) {
const highlight = opt.highlight;
let tokens,
pending,
i = 0;
let tokens;

try {
tokens = Lexer_1.lex(src, opt);
} catch (e) {
return callback(e);
}

pending = tokens.length;

const done = function(err) {
if (err) {
opt.highlight = highlight;
return callback(err);
}

let out;

try {
out = Parser_1.parse(tokens, opt);
} catch (e) {
err = e;
if (!err) {
try {
out = Parser_1.parse(tokens, opt);
} catch (e) {
err = e;
}
}

opt.highlight = highlight;
Expand All @@ -2193,34 +2189,45 @@ function marked(src, opt, callback) {

delete opt.highlight;

if (!pending) return done();
if (!tokens.length) return done();

for (; i < tokens.length; i++) {
(function(token) {
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();
let pending = 0;
marked.walkTokens(tokens, function(token) {
if (token.type === 'code') {
pending++;
highlight(token.text, token.lang, function(err, code) {
if (err) {
return done(err);
}
if (code != null && code !== token.text) {
token.text = code;
token.escaped = true;
}

pending--;
if (pending === 0) {
done();
}
token.text = code;
token.escaped = true;
--pending || done();
});
})(tokens[i]);
}
});

if (pending === 0) {
done();
}

return;
}

try {
opt = merge$2({}, marked.defaults, opt || {});
checkSanitizeDeprecation$1(opt);
return Parser_1.parse(Lexer_1.lex(src, opt), opt);
const tokens = Lexer_1.lex(src, opt);
if (opt.walkTokens) {
marked.walkTokens(tokens, opt.walkTokens);
}
return Parser_1.parse(tokens, opt);
} catch (e) {
e.message += '\nPlease report this to https://github.com/markedjs/marked.';
if ((opt || marked.defaults).silent) {
if (opt.silent) {
return '<p>An error occurred:</p><pre>'
+ escape$3(e.message + '', true)
+ '</pre>';
Expand Down Expand Up @@ -2278,9 +2285,50 @@ marked.use = function(extension) {
}
opts.tokenizer = tokenizer;
}
if (extension.walkTokens) {
const walkTokens = marked.defaults.walkTokens;
opts.walkTokens = (token) => {
extension.walkTokens(token);
if (walkTokens) {
walkTokens(token);
}
};
}
marked.setOptions(opts);
};

/**
* Run callback for every token
*/

marked.walkTokens = function(tokens, callback) {
for (const token of tokens) {
callback(token);
switch (token.type) {
case 'table': {
for (const cell of token.tokens.header) {
marked.walkTokens(cell, callback);
}
for (const row of token.tokens.cells) {
for (const cell of row) {
marked.walkTokens(cell, callback);
}
}
break;
}
case 'list': {
marked.walkTokens(token.items, callback);
break;
}
default: {
if (token.tokens) {
marked.walkTokens(token.tokens, callback);
}
}
}
}
};

/**
* Expose
*/
Expand Down

1 comment on commit 05f2fd6

@vercel
Copy link

@vercel vercel bot commented on 05f2fd6 May 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.