feat(rawHeaderId): Remove only spaces, ' and " from generated header ids

This option removes only spaces, ' and " from generated Header IDs,
replacing them with dashes. This might generate malformed IDs.

Closes #409
This commit is contained in:
Estevao Soares dos Santos 2017-08-06 17:45:04 +01:00
parent fef110cccb
commit 1791cf0ebf
11 changed files with 39 additions and 3 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -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',

View File

@ -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
@ -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 $)
@ -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, '')

View File

@ -0,0 +1 @@
<h1 id="123-my#very/-strange-\header*`^ªº-_.,-yeah">123 My#very/ strange \header*`^ªº-_., yeah</h1>

View File

@ -0,0 +1 @@
# 123 My#very/ strange \header*`^ªº-_., yeah

View File

@ -0,0 +1,2 @@
<h1 id="/prefix/some-header">some header</h1>
<h1 id="/prefix/another-!-#$%&/()=?»@£§{[]}«--header">another !"#$%&amp;/()=?»@£§{[]}«' header</h1>

View File

@ -0,0 +1,3 @@
# some header
# another !"#$%&/()=?»@£§{[]}«' header

View File

@ -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';
@ -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));
}
});
});