Prepending header ids with section and adding unique identifier to header ids.

closes #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
This commit is contained in:
nicovalencia 2014-01-08 15:34:54 -07:00
parent 7cd4829f9c
commit 1346a11cb4
14 changed files with 43 additions and 25 deletions

View File

@ -767,6 +767,26 @@ 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
// ========
@ -774,11 +794,13 @@ Showdown.converter = function(converter_options) {
// 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
@ -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;
};

View File

@ -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>

View File

@ -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>

View File

@ -1 +1 @@
<h1 id="thisisanh1">This is an H1</h1>
<h1 id="section-thisisanh1">This is an H1</h1>

View File

@ -1 +1 @@
<h1 id="thisisanh1">This is an H1</h1>
<h1 id="section-thisisanh1">This is an H1</h1>

View File

@ -1 +1 @@
<h1 id="thisisanh1">This is an H1</h1>
<h1 id="section-thisisanh1">This is an H1</h1>

View File

@ -1 +1 @@
<h2 id="thisisanh2">This is an H2</h2>
<h2 id="section-thisisanh2">This is an H2</h2>

View File

@ -1 +1 @@
<h2 id="thisisanh2">This is an H2</h2>
<h2 id="section-thisisanh2">This is an H2</h2>

View File

@ -1 +1 @@
<h2 id="thisisanh2">This is an H2</h2>
<h2 id="section-thisisanh2">This is an H2</h2>

View File

@ -1 +1 @@
<h3 id="thisisanh3">This is an H3</h3>
<h3 id="section-thisisanh3">This is an H3</h3>

View File

@ -1 +1 @@
<h3 id="thisisanh3">This is an H3</h3>
<h3 id="section-thisisanh3">This is an H3</h3>

View File

@ -1 +1 @@
<h4 id="thisisanh4">This is an H4</h4>
<h4 id="section-thisisanh4">This is an H4</h4>

View File

@ -1 +1 @@
<h5 id="thisisanh5">This is an H5</h5>
<h5 id="section-thisisanh5">This is an H5</h5>

View File

@ -1 +1 @@
<h6 id="thisisanh6">This is an H6</h6>
<h6 id="section-thisisanh6">This is an H6</h6>