☕️ Classic, reliable, trusted test framework for Node.js and the browser
generate better markdown TOC
TJ Holowaychuke185caa4033f
diff --git a/lib/reporters/markdown.js b/lib/reporters/markdown.jsindex 1e23f8f7..7d7842b2 100644--- a/lib/reporters/markdown.js+++ b/lib/reporters/markdown.js@@ -26,7 +26,6 @@ function Markdown(runner) {, stats = this.stats, total = runner.total, level = 0- , toc = '', buf = '';function title(str) {@@ -37,10 +36,38 @@ function Markdown(runner) {return Array(level).join(' ');}+ // if (!suite.root) toc += indent() + '- [' + suite.title + '](#' + slug + ')\n';++ function mapTOC(suite, obj) {+ var ret = obj;+ obj = obj[suite.title] = obj[suite.title] || {};+ suite.suites.forEach(function(suite){+ mapTOC(suite, obj);+ });+ return ret;+ }++ function stringifyTOC(obj, level) {+ ++level;+ var buf = '';+ for (var key in obj) {+ if (key) buf += Array(level).join(' ') + ' - ' + key + '\n';+ buf += stringifyTOC(obj[key], level);+ }+ --level;+ return buf;+ }++ function generateTOC(suite) {+ var obj = mapTOC(suite, {});+ return stringifyTOC(obj, 0);+ }++ generateTOC(runner.suite);+runner.on('suite', function(suite){++level;var slug = utils.slug(suite.fullTitle());- if (!suite.root) toc += indent() + '- [' + suite.title + '](#' + slug + ')\n';buf += '<a name="' + slug + '" />' + '\n';buf += title(suite.title) + '\n';});@@ -59,7 +86,7 @@ function Markdown(runner) {runner.on('end', function(){process.stdout.write('# TOC\n');- process.stdout.write(toc);+ process.stdout.write(generateTOC(runner.suite));process.stdout.write(buf);});}