Skip to content

Commit

Permalink
fix(prefixHeaderId): make prefixHeaderId string be parsed along the…
Browse files Browse the repository at this point in the history
… generated id

If an invalid prefix was passed (a string with spaces, for instance),
the string would be added as is. This ould generate invalid ids.
Also, this makes `prefixHeaderId` option play nicely with `ghCompatibleHeaderId`, since they will
follow the same escaping rules when both options are enabled.
  • Loading branch information
tivie committed Feb 6, 2017
1 parent 41cb3f6 commit f641a7d
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 39 deletions.
35 changes: 17 additions & 18 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

35 changes: 17 additions & 18 deletions src/subParsers/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ showdown.subParser('headers', function (text, options, globals) {

text = globals.converter._dispatch('headers.before', text, options, globals);

var prefixHeader = options.prefixHeaderId,
headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
var headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
ghHeaderId = options.ghCompatibleHeaderId,

// Set text-style headers:
Expand Down Expand Up @@ -53,10 +52,18 @@ showdown.subParser('headers', function (text, options, globals) {
});

function headerId (m) {
var title, escapedId;
var title;
// Prefix id to prevent causing inadvertent pre-existing style matches.
if (showdown.helper.isString(options.prefixHeaderId)) {
title = options.prefixHeaderId + m;
} else if (options.prefixHeaderId === true) {
title = 'section ' + m;
} else {
title = m;
}

if (ghHeaderId) {
escapedId = m
title = title
.replace(/ /g, '-')
// replace previously escaped chars (&, ¨ and $)
.replace(/&/g, '')
Expand All @@ -67,23 +74,15 @@ showdown.subParser('headers', function (text, options, globals) {
.replace(/[&+$,\/:;=?@"#{}|^¨~\[\]`\\*)(%.!'<>]/g, '')
.toLowerCase();
} else {
escapedId = m.replace(/[^\w]/g, '').toLowerCase();
title = title
.replace(/[^\w]/g, '')
.toLowerCase();
}

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

// Prefix id to prevent causing inadvertent pre-existing style matches.
if (prefixHeader === true) {
prefixHeader = 'section';
}

if (showdown.helper.isString(prefixHeader)) {
return prefixHeader + title;
globals.hashLinkCounts[title] = 1;
}
return title;
}
Expand Down
1 change: 1 addition & 0 deletions test/features/prefixHeaderId-simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1 id="sectionfooheader">foo header</h1>
1 change: 1 addition & 0 deletions test/features/prefixHeaderId-simple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# foo header
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1 id="my-prefix-foo-header">foo header</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# foo header
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1 id="my-prefix-foo-header">foo header</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# foo header
1 change: 1 addition & 0 deletions test/features/prefixHeaderId-string.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1 id="myprefixfooheader">foo header</h1>
1 change: 1 addition & 0 deletions test/features/prefixHeaderId-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# foo header
8 changes: 8 additions & 0 deletions test/node/testsuite.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({encodeEmails: false, simplifiedAutoLink: true});
} else if (testsuite[i].name === '#331.allow-escaping-of-tilde') {
converter = new showdown.Converter({strikethrough: true});
} else if (testsuite[i].name === 'prefixHeaderId-simple') {
converter = new showdown.Converter({prefixHeaderId: true});
} else if (testsuite[i].name === 'prefixHeaderId-string') {
converter = new showdown.Converter({prefixHeaderId: 'my-prefix-'});
} else if (testsuite[i].name === 'prefixHeaderId-string-and-ghCompatibleHeaderId') {
converter = new showdown.Converter({prefixHeaderId: 'my-prefix-', ghCompatibleHeaderId: true});
} else if (testsuite[i].name === 'prefixHeaderId-string-and-ghCompatibleHeaderId2') {
converter = new showdown.Converter({prefixHeaderId: 'my prefix ', ghCompatibleHeaderId: true});
} else {
converter = new showdown.Converter();
}
Expand Down

0 comments on commit f641a7d

Please sign in to comment.