Skip to content

Commit

Permalink
fix: return values from walkTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Jun 4, 2022
1 parent 2002557 commit 67a2343
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 304 deletions.
99 changes: 56 additions & 43 deletions lib/marked.cjs
Expand Up @@ -412,7 +412,7 @@ function outputLink(cap, link, raw, lexer) {
href: href,
title: title,
text: text,
tokens: lexer.inlineTokens(text, [])
tokens: lexer.inlineTokens(text)
};
lexer.state.inLink = false;
return token;
Expand Down Expand Up @@ -520,15 +520,13 @@ var Tokenizer = /*#__PURE__*/function () {
}
}

var token = {
return {
type: 'heading',
raw: cap[0],
depth: cap[1].length,
text: text,
tokens: []
tokens: this.lexer.inline(text)
};
this.lexer.inline(token.text, token.tokens);
return token;
}
};

Expand Down Expand Up @@ -746,10 +744,10 @@ var Tokenizer = /*#__PURE__*/function () {
};

if (this.options.sanitize) {
var text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
token.type = 'paragraph';
token.text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
token.tokens = [];
this.lexer.inline(token.text, token.tokens);
token.text = text;
token.tokens = this.lexer.inline(text);
}

return token;
Expand Down Expand Up @@ -819,8 +817,7 @@ var Tokenizer = /*#__PURE__*/function () {
l = item.header.length;

for (j = 0; j < l; j++) {
item.header[j].tokens = [];
this.lexer.inline(item.header[j].text, item.header[j].tokens);
item.header[j].tokens = this.lexer.inline(item.header[j].text);
} // cell child tokens


Expand All @@ -830,8 +827,7 @@ var Tokenizer = /*#__PURE__*/function () {
row = item.rows[j];

for (k = 0; k < row.length; k++) {
row[k].tokens = [];
this.lexer.inline(row[k].text, row[k].tokens);
row[k].tokens = this.lexer.inline(row[k].text);
}
}

Expand All @@ -844,45 +840,40 @@ var Tokenizer = /*#__PURE__*/function () {
var cap = this.rules.block.lheading.exec(src);

if (cap) {
var token = {
return {
type: 'heading',
raw: cap[0],
depth: cap[2].charAt(0) === '=' ? 1 : 2,
text: cap[1],
tokens: []
tokens: this.lexer.inline(cap[1])
};
this.lexer.inline(token.text, token.tokens);
return token;
}
};

_proto.paragraph = function paragraph(src) {
var cap = this.rules.block.paragraph.exec(src);

if (cap) {
var token = {
var text = cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1];
return {
type: 'paragraph',
raw: cap[0],
text: cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1],
tokens: []
text: text,
tokens: this.lexer.inline(text)
};
this.lexer.inline(token.text, token.tokens);
return token;
}
};

_proto.text = function text(src) {
var cap = this.rules.block.text.exec(src);

if (cap) {
var token = {
return {
type: 'text',
raw: cap[0],
text: cap[0],
tokens: []
tokens: this.lexer.inline(cap[0])
};
this.lexer.inline(token.text, token.tokens);
return token;
}
};

Expand Down Expand Up @@ -1061,7 +1052,7 @@ var Tokenizer = /*#__PURE__*/function () {
type: 'em',
raw: src.slice(0, lLength + match.index + rLength + 1),
text: _text,
tokens: this.lexer.inlineTokens(_text, [])
tokens: this.lexer.inlineTokens(_text)
};
} // Create 'strong' if smallest delimiter has even char count. **a***

Expand All @@ -1071,7 +1062,7 @@ var Tokenizer = /*#__PURE__*/function () {
type: 'strong',
raw: src.slice(0, lLength + match.index + rLength + 1),
text: text,
tokens: this.lexer.inlineTokens(text, [])
tokens: this.lexer.inlineTokens(text)
};
}
}
Expand Down Expand Up @@ -1117,7 +1108,7 @@ var Tokenizer = /*#__PURE__*/function () {
type: 'del',
raw: cap[0],
text: cap[2],
tokens: this.lexer.inlineTokens(cap[2], [])
tokens: this.lexer.inlineTokens(cap[2])
};
}
};
Expand Down Expand Up @@ -1735,10 +1726,15 @@ var Lexer = /*#__PURE__*/function () {
};

_proto.inline = function inline(src, tokens) {
if (tokens === void 0) {
tokens = [];
}

this.inlineQueue.push({
src: src,
tokens: tokens
});
return tokens;
}
/**
* Lexing/Compiling
Expand Down Expand Up @@ -2711,22 +2707,32 @@ function marked(src, opt, callback) {
return;
}

function onError(e) {
e.message += '\nPlease report this to https://github.com/markedjs/marked.';

if (opt.silent) {
return '<p>An error occurred:</p><pre>' + escape(e.message + '', true) + '</pre>';
}

throw e;
}

try {
var _tokens = Lexer.lex(src, opt);

if (opt.walkTokens) {
if (opt.async) {
return Promise.all(marked.walkTokens(_tokens, opt.walkTokens)).then(function () {
return Parser.parse(_tokens, opt);
})["catch"](onError);
}

marked.walkTokens(_tokens, opt.walkTokens);
}

return Parser.parse(_tokens, opt);
} catch (e) {
e.message += '\nPlease report this to https://github.com/markedjs/marked.';

if (opt.silent) {
return '<p>An error occurred:</p><pre>' + escape(e.message + '', true) + '</pre>';
}

throw e;
onError(e);
}
}
/**
Expand Down Expand Up @@ -2892,11 +2898,14 @@ marked.use = function () {
var _walkTokens = marked.defaults.walkTokens;

opts.walkTokens = function (token) {
pack.walkTokens.call(this, token);
var values = [];
values.concat(pack.walkTokens.call(this, token));

if (_walkTokens) {
_walkTokens.call(this, token);
values.concat(_walkTokens.call(this, token));
}

return values;
};
}

Expand All @@ -2913,24 +2922,26 @@ marked.use = function () {


marked.walkTokens = function (tokens, callback) {
var values = [];

var _loop3 = function _loop3() {
var token = _step.value;
callback.call(marked, token);
values.concat(callback.call(marked, token));

switch (token.type) {
case 'table':
{
for (var _iterator2 = _createForOfIteratorHelperLoose(token.header), _step2; !(_step2 = _iterator2()).done;) {
var cell = _step2.value;
marked.walkTokens(cell.tokens, callback);
values.concat(marked.walkTokens(cell.tokens, callback));
}

for (var _iterator3 = _createForOfIteratorHelperLoose(token.rows), _step3; !(_step3 = _iterator3()).done;) {
var row = _step3.value;

for (var _iterator4 = _createForOfIteratorHelperLoose(row), _step4; !(_step4 = _iterator4()).done;) {
var _cell = _step4.value;
marked.walkTokens(_cell.tokens, callback);
values.concat(marked.walkTokens(_cell.tokens, callback));
}
}

Expand All @@ -2939,7 +2950,7 @@ marked.walkTokens = function (tokens, callback) {

case 'list':
{
marked.walkTokens(token.items, callback);
values.concat(marked.walkTokens(token.items, callback));
break;
}

Expand All @@ -2948,10 +2959,10 @@ marked.walkTokens = function (tokens, callback) {
if (marked.defaults.extensions && marked.defaults.extensions.childTokens && marked.defaults.extensions.childTokens[token.type]) {
// Walk any extensions
marked.defaults.extensions.childTokens[token.type].forEach(function (childTokens) {
marked.walkTokens(token[childTokens], callback);
values.concat(marked.walkTokens(token[childTokens], callback));
});
} else if (token.tokens) {
marked.walkTokens(token.tokens, callback);
values.concat(marked.walkTokens(token.tokens, callback));
}
}
}
Expand All @@ -2960,6 +2971,8 @@ marked.walkTokens = function (tokens, callback) {
for (var _iterator = _createForOfIteratorHelperLoose(tokens), _step; !(_step = _iterator()).done;) {
_loop3();
}

return values;
};
/**
* Parse Inline
Expand Down

0 comments on commit 67a2343

Please sign in to comment.