File tree Expand file tree Collapse file tree 4 files changed +72
-30
lines changed Expand file tree Collapse file tree 4 files changed +72
-30
lines changed Original file line number Diff line number Diff line change 1+ var md = require ( 'markdown-stream-utils' ) ;
2+
3+ // A wrapper around md.convertMd that includes the custom heading generation and id
4+ // generation code.
5+ module . exports = function ( argv ) {
6+ if ( ! argv ) {
7+ // default to true to match bin/generate-md
8+ argv = { 'header-links' : true } ;
9+ }
10+
11+ // header ids already seen in the current render
12+ var idCount = { } ;
13+ // custom rendered for headings
14+ var renderer = new md . marked . Renderer ( ) ;
15+ renderer . heading = function ( text , level , raw ) {
16+ var id = this . options . headerPrefix + raw . trim ( ) . toLowerCase ( ) . replace ( / [ ^ \w ] + / g, '-' ) ;
17+ // do nothing the first time a heading is seen
18+ if ( ! idCount . hasOwnProperty ( id ) ) {
19+ idCount [ id ] = 0 ;
20+ } else {
21+ // when duplicate headings are seen, append a dash-number starting with 1
22+ idCount [ id ] ++ ;
23+ id += '-' + idCount [ id ] ;
24+ }
25+ return '<h' +
26+ level +
27+ ' id="' +
28+ id +
29+ '">' +
30+ // if we want to generate header links, add the <a> link
31+ ( argv [ 'header-links' ] ?
32+ '<a class="header-link" href="#' + id + '"></a>' : '' ) +
33+ text +
34+ '</h' +
35+ level +
36+ '>\n' ;
37+ } ;
38+
39+ return md . convertMd ( {
40+ renderer : renderer
41+ } ) ;
42+ } ;
Original file line number Diff line number Diff line change 11module . exports = {
22 resolveArgs : require ( './resolve-args' ) ,
33 render : require ( './render' ) ,
4- pipeline : require ( './pipeline' )
4+ pipeline : require ( './pipeline' ) ,
5+ convertMd : require ( './convert-md' )
56} ;
Original file line number Diff line number Diff line change @@ -3,33 +3,10 @@ var fs = require('fs'),
33 md = require ( 'markdown-stream-utils' ) ,
44 setOutputPath = require ( './set-output-path' ) ,
55 applyTemplate = require ( './apply-template' ) ,
6- mergeMeta = require ( './merge-meta' ) ;
7-
8- var idCount = { } ;
6+ mergeMeta = require ( './merge-meta' ) ,
7+ convertMd = require ( './convert-md' ) ;
98
109module . exports = function ( argv ) {
11- // custom rendered for headings
12- var renderer = new md . marked . Renderer ( ) ;
13- // if we want to generate header links, override the default header renderer
14- if ( argv [ 'header-links' ] ) {
15- renderer . heading = function ( text , level , raw ) {
16- var id = this . options . headerPrefix + raw . trim ( ) . toLowerCase ( ) . replace ( / [ ^ \w ] + / g, '-' ) ;
17- idCount [ id ] = idCount . hasOwnProperty ( id ) && idCount [ id ] + 1 || 0 ;
18- id += idCount [ id ] > 0 && '-' + idCount [ id ] || '' ;
19- return '<h' +
20- level +
21- ' id="' +
22- id +
23- '">' +
24- '<a class="header-link" href="#' +
25- id +
26- '"></a>' +
27- text +
28- '</h' +
29- level +
30- '>\n' ;
31- } ;
32- }
3310 return pi . pipeline ( [
3411 md . parseHeader ( ) ,
3512 md . parseMd ( ) ,
@@ -40,8 +17,8 @@ module.exports = function(argv) {
4017 }
4118 return false ;
4219 } ) ,
43- md . convertMd ( {
44- renderer : renderer
20+ convertMd ( {
21+ 'header-links' : argv [ 'header-links' ]
4522 } ) ,
4623
4724 // map paths
Original file line number Diff line number Diff line change @@ -118,7 +118,7 @@ describe('integration tests', function() {
118118 md . parseMd ( ) ,
119119 md . annotateMdHeadings ( ) ,
120120 md . highlight ( ) ,
121- md . convertMd ( ) ,
121+ mds . convertMd ( ) ,
122122
123123 setOutputPath ( {
124124 input : '/fake/input' ,
@@ -229,12 +229,34 @@ describe('integration tests', function() {
229229 } , function ( html ) {
230230 assert . equal ( html , [
231231 'a<p>a</p>' ,
232- '<h1 id="foo">foo</h1>' ,
232+ '<h1 id="foo"><a class="header-link" href="#foo"></a> foo</h1>' ,
233233 '<pre class="hljs"><code>' +
234234 '<span class="hljs-keyword">var</span> foo = bar;</code></pre>b'
235235 ] . join ( '\n' ) ) ;
236236 done ( ) ;
237237 } ) ;
238238 } ) ;
239+
240+ it ( 'when the same header text is repeated, it produces ids with a number appended to them' , function ( done ) {
241+ render ( { contents : [
242+ '# some heading' ,
243+ 'hello' ,
244+ '# some heading' ,
245+ 'world' ,
246+ '# some heading' ,
247+ ] . join ( '\n' ) } , {
248+ template : '{{> content}}'
249+ } , function ( html ) {
250+ assert . equal ( html , [
251+ '<h1 id="some-heading"><a class="header-link" href="#some-heading"></a>some heading</h1>' ,
252+ '<p>hello</p>' ,
253+ '<h1 id="some-heading-1"><a class="header-link" href="#some-heading-1"></a>some heading</h1>' ,
254+ '<p>world</p>' ,
255+ '<h1 id="some-heading-2"><a class="header-link" href="#some-heading-2"></a>some heading</h1>' ,
256+ ''
257+ ] . join ( '\n' ) ) ;
258+ done ( ) ;
259+ } ) ;
260+ } ) ;
239261 } ) ;
240262} ) ;
You can’t perform that action at this time.
0 commit comments