Skip to content

Commit

Permalink
feat(rawHeaderId): Remove only spaces, ' and " from generated header ids
Browse files Browse the repository at this point in the history
This option removes only spaces, ' and " from generated Header IDs,
replacing them with dashes. This might generate malformed IDs.

Closes #409
  • Loading branch information
tivie committed Aug 6, 2017
1 parent fef110c commit 1791cf0
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 11 deletions.
20 changes: 17 additions & 3 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.

6 changes: 3 additions & 3 deletions 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.

5 changes: 5 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ function getDefaultOpts (simple) {
describe: 'Generate header ids compatible with github style (spaces are replaced with dashes, a bunch of non alphanumeric chars are removed)',
type: 'boolean'
},
rawHeaderId: {
defaultValue: false,
describe: 'Remove only spaces, \' and " from generated header ids (including prefixes), replacing them with dashes (-). WARNING: This might result in malformed ids',
type: 'boolean'
},
headerLevelStart: {
defaultValue: false,
describe: 'The header blocks level start',
Expand Down
13 changes: 11 additions & 2 deletions src/subParsers/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ showdown.subParser('headers', function (text, options, globals) {
text = globals.converter._dispatch('headers.before', text, options, globals);

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

// Set text-style headers:
// Header 1
Expand Down Expand Up @@ -76,7 +75,7 @@ showdown.subParser('headers', function (text, options, globals) {
title = m;
}

if (ghHeaderId) {
if (options.ghCompatibleHeaderId) {
title = title
.replace(/ /g, '-')
// replace previously escaped chars (&, ¨ and $)
Expand All @@ -87,6 +86,16 @@ showdown.subParser('headers', function (text, options, globals) {
// borrowed from github's redcarpet (some they should produce similar results)
.replace(/[&+$,\/:;=?@"#{}|^¨~\[\]`\\*)(%.!'<>]/g, '')
.toLowerCase();
} else if (options.rawHeaderId) {
title = title
.replace(/ /g, '-')
// replace previously escaped chars (&, ¨ and $)
.replace(/&amp;/g, '&')
.replace(/¨T/g, '¨')
.replace(/¨D/g, '$')
// replace " and '
.replace(/["']/g, '-')
.toLowerCase();
} else {
title = title
.replace(/[^\w]/g, '')
Expand Down
1 change: 1 addition & 0 deletions test/features/rawHeaderId/simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1 id="123-my#very/-strange-\header*`^ªº-_.,-yeah">123 My#very/ strange \header*`^ªº-_., yeah</h1>
1 change: 1 addition & 0 deletions test/features/rawHeaderId/simple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 123 My#very/ strange \header*`^ªº-_., yeah
2 changes: 2 additions & 0 deletions test/features/rawHeaderId/with-prefixHeaderId.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1 id="/prefix/some-header">some header</h1>
<h1 id="/prefix/another-!-#$%&/()=?»@£§{[]}«--header">another !"#$%&amp;/()=?»@£§{[]}«' header</h1>
3 changes: 3 additions & 0 deletions test/features/rawHeaderId/with-prefixHeaderId.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# some header

# another !"#$%&/()=?»@£§{[]}«' header
17 changes: 16 additions & 1 deletion test/node/testsuite.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var bootstrap = require('../bootstrap.js'),
tableSuite = bootstrap.getTestSuite('test/features/tables/'),
simplifiedAutoLinkSuite = bootstrap.getTestSuite('test/features/simplifiedAutoLink/'),
openLinksInNewWindowSuite = bootstrap.getTestSuite('test/features/openLinksInNewWindow/'),
disableForced4SpacesIndentedSublistsSuite = bootstrap.getTestSuite('test/features/disableForced4SpacesIndentedSublists/');
disableForced4SpacesIndentedSublistsSuite = bootstrap.getTestSuite('test/features/disableForced4SpacesIndentedSublists/'),
html5CompatibleHeaderIdSuite = bootstrap.getTestSuite('test/features/rawHeaderId/');

describe('makeHtml() features testsuite', function () {
'use strict';
Expand Down Expand Up @@ -152,4 +153,18 @@ describe('makeHtml() features testsuite', function () {
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
});

// test rawHeaderId support
describe('rawHeaderId support', function () {
var converter,
suite = html5CompatibleHeaderIdSuite;
for (var i = 0; i < suite.length; ++i) {
if (suite[i].name === 'with-prefixHeaderId') {
converter = new showdown.Converter({rawHeaderId: true, prefixHeaderId: '/prefix/'});
} else {
converter = new showdown.Converter({rawHeaderId: true});
}
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
});
});

0 comments on commit 1791cf0

Please sign in to comment.