Skip to content

Commit

Permalink
Prepending header ids with section and adding unique identifier to …
Browse files Browse the repository at this point in the history
…header ids.

closes showdownjs#82
- Modified header id function to prepend `section` to each id
- Modified header id function to track duplicate count and append a unique numerical-id
  • Loading branch information
nicovalencia committed Jan 8, 2014
1 parent 7cd4829 commit 1346a11
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 25 deletions.
42 changes: 30 additions & 12 deletions src/showdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,18 +767,40 @@ Showdown.converter = function(converter_options) {

var _DoHeaders = function(text) {

// Keep track of ids used for headers for hash-linking. If any duplicates
// are used, append a unique string based on the count of identical ids.
// This prevents the result of having duplicate ids if a user creates
// similar headers.
var hashLinkCounts = {};
function headerId(m) {
var title,
escapedId = m.replace(/[^\w]/g, '').toLowerCase();

if (hashLinkCounts[escapedId]) {
title = escapedId + "-" + (hashLinkCounts[escapedId]++);
} else {
title = escapedId;
hashLinkCounts[escapedId] = 1;
}

// Prefix id to prevent causing inadverntent pre-existing style matches.
return "section-" + title;
}

// Setext-style headers:
// Header 1
// ========
//
// Header 2
// --------
//
text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm,
function(wholeMatch,m1){return hashBlock('<h1 id="' + headerId(m1) + '">' + _RunSpanGamut(m1) + "</h1>");});
text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm, function(wholeMatch,m1){
return hashBlock('<h1 id="' + headerId(m1) + '">' + _RunSpanGamut(m1) + "</h1>");
});

text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm,
function(matchFound,m1){return hashBlock('<h2 id="' + headerId(m1) + '">' + _RunSpanGamut(m1) + "</h2>");});
text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm, function(matchFound,m1){
return hashBlock('<h2 id="' + headerId(m1) + '">' + _RunSpanGamut(m1) + "</h2>");
});

// atx-style headers:
// # Header 1
Expand All @@ -799,15 +821,11 @@ Showdown.converter = function(converter_options) {
/gm, function() {...});
*/

text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm,
function(wholeMatch,m1,m2) {
var h_level = m1.length;
return hashBlock("<h" + h_level + ' id="' + headerId(m2) + '">' + _RunSpanGamut(m2) + "</h" + h_level + ">");
});
text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm, function(wholeMatch,m1,m2) {
var h_level = m1.length;
return hashBlock("<h" + h_level + ' id="' + headerId(m2) + '">' + _RunSpanGamut(m2) + "</h" + h_level + ">");
});

function headerId(m) {
return m.replace(/[^\w]/g, '').toLowerCase();
}
return text;
};

Expand Down
2 changes: 1 addition & 1 deletion test/cases/anchors-by-reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
<p>This is <a href="http://example.com/" title="Optional Title Here">an example</a> reference-style link.
This is <a href="http://example.com/" title="Optional Title Here">another</a> reference-style link.
This is <a href="http://example.com/" title="Optional Title Here">a third</a> reference-style link.
This is <a href="http://example.com/" title="Optional Title Here">a fourth</a> reference-style link.</p>
This is <a href="http://example.com/" title="Optional Title Here">a fourth</a> reference-style link.</p>
2 changes: 1 addition & 1 deletion test/cases/blockquote-nested-markdown.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<blockquote>
<h2 id="thisisaheader">This is a header.</h2>
<h2 id="section-thisisaheader">This is a header.</h2>

<ol>
<li>This is the first list item.</li>
Expand Down
2 changes: 1 addition & 1 deletion test/cases/h1-with-double-hash.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h1 id="thisisanh1">This is an H1</h1>
<h1 id="section-thisisanh1">This is an H1</h1>
2 changes: 1 addition & 1 deletion test/cases/h1-with-equals.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h1 id="thisisanh1">This is an H1</h1>
<h1 id="section-thisisanh1">This is an H1</h1>
2 changes: 1 addition & 1 deletion test/cases/h1-with-single-hash.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h1 id="thisisanh1">This is an H1</h1>
<h1 id="section-thisisanh1">This is an H1</h1>
2 changes: 1 addition & 1 deletion test/cases/h2-with-dashes.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h2 id="thisisanh2">This is an H2</h2>
<h2 id="section-thisisanh2">This is an H2</h2>
2 changes: 1 addition & 1 deletion test/cases/h2-with-double-hash.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h2 id="thisisanh2">This is an H2</h2>
<h2 id="section-thisisanh2">This is an H2</h2>
2 changes: 1 addition & 1 deletion test/cases/h2-with-single-hash.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h2 id="thisisanh2">This is an H2</h2>
<h2 id="section-thisisanh2">This is an H2</h2>
2 changes: 1 addition & 1 deletion test/cases/h3-with-double-hash.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h3 id="thisisanh3">This is an H3</h3>
<h3 id="section-thisisanh3">This is an H3</h3>
2 changes: 1 addition & 1 deletion test/cases/h3-with-single-hash.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h3 id="thisisanh3">This is an H3</h3>
<h3 id="section-thisisanh3">This is an H3</h3>
2 changes: 1 addition & 1 deletion test/cases/h4-with-single-hash.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h4 id="thisisanh4">This is an H4</h4>
<h4 id="section-thisisanh4">This is an H4</h4>
2 changes: 1 addition & 1 deletion test/cases/h5-with-single-hash.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h5 id="thisisanh5">This is an H5</h5>
<h5 id="section-thisisanh5">This is an H5</h5>
2 changes: 1 addition & 1 deletion test/cases/h6-with-single-hash.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h6 id="thisisanh6">This is an H6</h6>
<h6 id="section-thisisanh6">This is an H6</h6>

0 comments on commit 1346a11

Please sign in to comment.