Skip to content

Commit

Permalink
Merge pull request #1638 from UziTech/fix-demo
Browse files Browse the repository at this point in the history
fix demo table tokens
  • Loading branch information
UziTech committed Apr 10, 2020
2 parents 029492a + ab033f2 commit 0ad3a9e
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions docs/demo/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function parse(e) {
case 'parse':
var startTime = new Date();
var lexed = marked.lexer(e.data.markdown, e.data.options);
var lexedList = getLexedList(lexed);
var lexedList = jsonString(lexed);
var parsed = marked.parser(lexed, e.data.options);
var endTime = new Date();
postMessage({
Expand All @@ -62,23 +62,6 @@ function parse(e) {
}
}

function getLexedList(lexed, level) {
level = level || 0;
var lexedList = [];
for (var i = 0; i < lexed.length; i++) {
var lexedLine = [];
for (var j in lexed[i]) {
if (j === 'tokens' || j === 'items') {
lexedLine.push(j + ': [\n' + getLexedList(lexed[i][j], level + 1) + '\n]');
} else {
lexedLine.push(j + ':' + jsonString(lexed[i][j]));
}
}
lexedList.push(stringRepeat(' ', 2 * level) + '{' + lexedLine.join(', ') + '}');
}
return lexedList.join('\n');
}

function stringRepeat(char, times) {
var s = '';
for (var i = 0; i < times; i++) {
Expand All @@ -87,15 +70,33 @@ function stringRepeat(char, times) {
return s;
}

function jsonString(input) {
var output = (input + '')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t')
.replace(/\f/g, '\\f')
.replace(/[\\"']/g, '\\$&')
.replace(/\u0000/g, '\\0');
return '"' + output + '"';
function jsonString(input, level) {
level = level || 0;
if (Array.isArray(input)) {
if (input.length === 0) {
return '[]';
}
var items = [],
i;
if (!Array.isArray(input[0]) && typeof input[0] === 'object' && input[0] !== null) {
for (i = 0; i < input.length; i++) {
items.push(stringRepeat(' ', 2 * level) + jsonString(input[i], level + 1));
}
return '[\n' + items.join('\n') + '\n]';
}
for (i = 0; i < input.length; i++) {
items.push(jsonString(input[i], level));
}
return '[' + items.join(', ') + ']';
} else if (typeof input === 'object' && input !== null) {
var props = [];
for (var prop in input) {
props.push(prop + ':' + jsonString(input[prop], level));
}
return '{' + props.join(', ') + '}';
} else {
return JSON.stringify(input);
}
}

function loadVersion(ver) {
Expand Down

1 comment on commit 0ad3a9e

@vercel
Copy link

@vercel vercel bot commented on 0ad3a9e Apr 10, 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.