Extend remy's addition of heading id attributes to apply to Setext-style headings as well.

This commit is contained in:
Corey Innis 2010-10-31 11:10:22 -07:00
parent 053b830e88
commit 55d2f65350

View File

@ -661,10 +661,10 @@ var _DoHeaders = function(text) {
// --------
//
text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm,
function(wholeMatch,m1){return hashBlock("<h1>" + _RunSpanGamut(m1) + "</h1>");});
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>" + _RunSpanGamut(m1) + "</h2>");});
function(matchFound,m1){return hashBlock('<h2 id="' + headerId(m1) + '">' + _RunSpanGamut(m1) + "</h2>");});
// atx-style headers:
// # Header 1
@ -688,10 +688,12 @@ var _DoHeaders = function(text) {
text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm,
function(wholeMatch,m1,m2) {
var h_level = m1.length;
var id = m2.replace(/[^\w]/g, '').toLowerCase();
return hashBlock("<h" + h_level + ' id="' + id + '">' + _RunSpanGamut(m2) + "</h" + h_level + ">");
return hashBlock("<h" + h_level + ' id="' + headerId(m2) + '">' + _RunSpanGamut(m2) + "</h" + h_level + ">");
});
function headerId(m) {
return m.replace(/[^\w]/g, '').toLowerCase();
}
return text;
}