Permalink
Browse files

Move header generation into markdown-stream-utils, add TOC generation…

… to the Chinese character integration test
  • Loading branch information...
1 parent c3c31ae commit 2306c9940006afa31988ced8b8e0211582fa5ed6 @mixu committed Jan 5, 2017
Showing with 21 additions and 21 deletions.
  1. +9 −21 lib/convert-md.js
  2. +12 −0 test/integration.test.js
View
@@ -11,20 +11,14 @@ module.exports = function(argv) {
argv = { 'header-links': true };
}
- // header ids already seen in the current render
- var idCount = {};
// custom rendered for headings
var renderer = new md.marked.Renderer();
- renderer.heading = function(text, level, raw) {
- var id = this.options.headerPrefix + raw.trim().toLowerCase().replace(/\s+/g, '-');
- // do nothing the first time a heading is seen
- if (!idCount.hasOwnProperty(id)) {
- idCount[id] = 0;
- } else {
- // when duplicate headings are seen, append a dash-number starting with 1
- idCount[id]++;
- id += '-' + idCount[id];
- }
+ // Note: the fourth argument is NOT part of the regular Marked interface - I'm adding
+ // it (in markdown-stream-utils) so that id generation can live in one place.
+ // I wish marked's API would give me direct access to the token in the renderer itself :'(
+ renderer.heading = function(text, level, raw, token) {
+ // the 4th parameter is an addition so check whether we got that parameter before accessing it
+ var id = this.options.headerPrefix + (token ? token.id : raw);
return '<h' +
level +
' id="' +
@@ -77,13 +71,7 @@ module.exports = function(argv) {
return out;
};
- // reset the header counts for each file, so that idCount is not shared across the whole render
- return pi.pipeline([
- pi.forEach(function() {
- idCount = {};
- }),
- md.convertMd({
- renderer: renderer
- })
- ]);
+ return md.convertMd({
+ renderer: renderer
+ });
};
@@ -60,13 +60,25 @@ describe('integration tests', function() {
var out = fixture.dirname();
+ var layoutDir = fixture.dir({
+ 'page.html': '"{{title}}" by {{author}}\n{{> toc}}{{> content}}'
+ });
+
mds.render({
input: dir,
output: out,
layout: layoutDir
}, function() {
assert.equal(fs.readFileSync(out + '/foo.html', 'utf8'), [
'"你好 世界" by Anonymous',
+ '<ul class="nav nav-list">',
+ ' <li class="sidebar-header-1"><a href="#世界因我而不同">世界因我而不同</a></li>',
+ ' <li class="sidebar-header-2"><a href="#世界因我而不同-1">世界因我而不同</a></li>',
+ ' <li class="sidebar-header-3"><a href="#世界因我而不同-2">世界因我而不同</a></li>',
+ ' <li class="sidebar-header-1"><a href="#一个世界,一个梦想">一个世界,一个梦想</a></li>',
+ ' <li class="sidebar-header-2"><a href="#一个世界,一个梦想-1">一个世界,一个梦想</a></li>',
+ ' <li class="sidebar-header-3"><a href="#世界因我而不同-3">世界因我而不同</a></li>',
+ '</ul>',
'<h1 id="世界因我而不同">世界因我而不同</h1>',
'<h2 id="世界因我而不同-1">世界因我而不同</h2>',
'<h3 id="世界因我而不同-2">世界因我而不同</h3>',

0 comments on commit 2306c99

Please sign in to comment.