mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
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:
parent
7cd4829f9c
commit
1346a11cb4
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h1 id="thisisanh1">This is an H1</h1>
|
||||
<h1 id="section-thisisanh1">This is an H1</h1>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h1 id="thisisanh1">This is an H1</h1>
|
||||
<h1 id="section-thisisanh1">This is an H1</h1>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h1 id="thisisanh1">This is an H1</h1>
|
||||
<h1 id="section-thisisanh1">This is an H1</h1>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h2 id="thisisanh2">This is an H2</h2>
|
||||
<h2 id="section-thisisanh2">This is an H2</h2>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h2 id="thisisanh2">This is an H2</h2>
|
||||
<h2 id="section-thisisanh2">This is an H2</h2>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h2 id="thisisanh2">This is an H2</h2>
|
||||
<h2 id="section-thisisanh2">This is an H2</h2>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h3 id="thisisanh3">This is an H3</h3>
|
||||
<h3 id="section-thisisanh3">This is an H3</h3>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h3 id="thisisanh3">This is an H3</h3>
|
||||
<h3 id="section-thisisanh3">This is an H3</h3>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h4 id="thisisanh4">This is an H4</h4>
|
||||
<h4 id="section-thisisanh4">This is an H4</h4>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h5 id="thisisanh5">This is an H5</h5>
|
||||
<h5 id="section-thisisanh5">This is an H5</h5>
|
||||
|
|
|
@ -1 +1 @@
|
|||
<h6 id="thisisanh6">This is an H6</h6>
|
||||
<h6 id="section-thisisanh6">This is an H6</h6>
|
||||
|
|
Loading…
Reference in New Issue
Block a user