From 1346a11cb40b96b8f3db6bea8a8addaea28b2389 Mon Sep 17 00:00:00 2001 From: nicovalencia Date: Wed, 8 Jan 2014 15:34:54 -0700 Subject: [PATCH] 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 --- src/showdown.js | 42 +++++++++++++++------- test/cases/anchors-by-reference.html | 2 +- test/cases/blockquote-nested-markdown.html | 2 +- test/cases/h1-with-double-hash.html | 2 +- test/cases/h1-with-equals.html | 2 +- test/cases/h1-with-single-hash.html | 2 +- test/cases/h2-with-dashes.html | 2 +- test/cases/h2-with-double-hash.html | 2 +- test/cases/h2-with-single-hash.html | 2 +- test/cases/h3-with-double-hash.html | 2 +- test/cases/h3-with-single-hash.html | 2 +- test/cases/h4-with-single-hash.html | 2 +- test/cases/h5-with-single-hash.html | 2 +- test/cases/h6-with-single-hash.html | 2 +- 14 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/showdown.js b/src/showdown.js index ebbd8a7..24835e0 100644 --- a/src/showdown.js +++ b/src/showdown.js @@ -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('

' + _RunSpanGamut(m1) + "

");}); + text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm, function(wholeMatch,m1){ + return hashBlock('

' + _RunSpanGamut(m1) + "

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

' + _RunSpanGamut(m1) + "

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

' + _RunSpanGamut(m1) + "

"); + }); // 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("' + _RunSpanGamut(m2) + ""); - }); + text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm, function(wholeMatch,m1,m2) { + var h_level = m1.length; + return hashBlock("' + _RunSpanGamut(m2) + ""); + }); - function headerId(m) { - return m.replace(/[^\w]/g, '').toLowerCase(); - } return text; }; diff --git a/test/cases/anchors-by-reference.html b/test/cases/anchors-by-reference.html index 9265274..99c15e3 100644 --- a/test/cases/anchors-by-reference.html +++ b/test/cases/anchors-by-reference.html @@ -2,4 +2,4 @@

This is an example reference-style link. This is another reference-style link. This is a third reference-style link. -This is a fourth reference-style link.

\ No newline at end of file +This is a fourth reference-style link.

diff --git a/test/cases/blockquote-nested-markdown.html b/test/cases/blockquote-nested-markdown.html index 25800d7..afae8f8 100644 --- a/test/cases/blockquote-nested-markdown.html +++ b/test/cases/blockquote-nested-markdown.html @@ -1,5 +1,5 @@
-

This is a header.

+

This is a header.

  1. This is the first list item.
  2. diff --git a/test/cases/h1-with-double-hash.html b/test/cases/h1-with-double-hash.html index 2cecc38..749011b 100644 --- a/test/cases/h1-with-double-hash.html +++ b/test/cases/h1-with-double-hash.html @@ -1 +1 @@ -

    This is an H1

    \ No newline at end of file +

    This is an H1

    diff --git a/test/cases/h1-with-equals.html b/test/cases/h1-with-equals.html index 2cecc38..749011b 100644 --- a/test/cases/h1-with-equals.html +++ b/test/cases/h1-with-equals.html @@ -1 +1 @@ -

    This is an H1

    \ No newline at end of file +

    This is an H1

    diff --git a/test/cases/h1-with-single-hash.html b/test/cases/h1-with-single-hash.html index 2cecc38..749011b 100644 --- a/test/cases/h1-with-single-hash.html +++ b/test/cases/h1-with-single-hash.html @@ -1 +1 @@ -

    This is an H1

    \ No newline at end of file +

    This is an H1

    diff --git a/test/cases/h2-with-dashes.html b/test/cases/h2-with-dashes.html index d040d75..2fc6b0b 100644 --- a/test/cases/h2-with-dashes.html +++ b/test/cases/h2-with-dashes.html @@ -1 +1 @@ -

    This is an H2

    \ No newline at end of file +

    This is an H2

    diff --git a/test/cases/h2-with-double-hash.html b/test/cases/h2-with-double-hash.html index d040d75..2fc6b0b 100644 --- a/test/cases/h2-with-double-hash.html +++ b/test/cases/h2-with-double-hash.html @@ -1 +1 @@ -

    This is an H2

    \ No newline at end of file +

    This is an H2

    diff --git a/test/cases/h2-with-single-hash.html b/test/cases/h2-with-single-hash.html index d040d75..2fc6b0b 100644 --- a/test/cases/h2-with-single-hash.html +++ b/test/cases/h2-with-single-hash.html @@ -1 +1 @@ -

    This is an H2

    \ No newline at end of file +

    This is an H2

    diff --git a/test/cases/h3-with-double-hash.html b/test/cases/h3-with-double-hash.html index 082318e..92a7399 100644 --- a/test/cases/h3-with-double-hash.html +++ b/test/cases/h3-with-double-hash.html @@ -1 +1 @@ -

    This is an H3

    \ No newline at end of file +

    This is an H3

    diff --git a/test/cases/h3-with-single-hash.html b/test/cases/h3-with-single-hash.html index 082318e..92a7399 100644 --- a/test/cases/h3-with-single-hash.html +++ b/test/cases/h3-with-single-hash.html @@ -1 +1 @@ -

    This is an H3

    \ No newline at end of file +

    This is an H3

    diff --git a/test/cases/h4-with-single-hash.html b/test/cases/h4-with-single-hash.html index cc32aa1..605e93a 100644 --- a/test/cases/h4-with-single-hash.html +++ b/test/cases/h4-with-single-hash.html @@ -1 +1 @@ -

    This is an H4

    \ No newline at end of file +

    This is an H4

    diff --git a/test/cases/h5-with-single-hash.html b/test/cases/h5-with-single-hash.html index 96dca78..ae3a75d 100644 --- a/test/cases/h5-with-single-hash.html +++ b/test/cases/h5-with-single-hash.html @@ -1 +1 @@ -
    This is an H5
    \ No newline at end of file +
    This is an H5
    diff --git a/test/cases/h6-with-single-hash.html b/test/cases/h6-with-single-hash.html index 10de690..a93ba51 100644 --- a/test/cases/h6-with-single-hash.html +++ b/test/cases/h6-with-single-hash.html @@ -1 +1 @@ -
    This is an H6
    \ No newline at end of file +
    This is an H6