mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
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:
parent
fef110cccb
commit
1791cf0ebf
BIN
dist/showdown.js
vendored
BIN
dist/showdown.js
vendored
Binary file not shown.
BIN
dist/showdown.js.map
vendored
BIN
dist/showdown.js.map
vendored
Binary file not shown.
BIN
dist/showdown.min.js
vendored
BIN
dist/showdown.min.js
vendored
Binary file not shown.
BIN
dist/showdown.min.js.map
vendored
BIN
dist/showdown.min.js.map
vendored
Binary file not shown.
|
@ -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',
|
||||
|
|
|
@ -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(/&/g, '&')
|
||||
.replace(/¨T/g, '¨')
|
||||
.replace(/¨D/g, '$')
|
||||
// replace " and '
|
||||
.replace(/["']/g, '-')
|
||||
.toLowerCase();
|
||||
} else {
|
||||
title = title
|
||||
.replace(/[^\w]/g, '')
|
||||
|
|
1
test/features/rawHeaderId/simple.html
Normal file
1
test/features/rawHeaderId/simple.html
Normal file
|
@ -0,0 +1 @@
|
|||
<h1 id="123-my#very/-strange-\header*`^ªº-_.,-yeah">123 My#very/ strange \header*`^ªº-_., yeah</h1>
|
1
test/features/rawHeaderId/simple.md
Normal file
1
test/features/rawHeaderId/simple.md
Normal file
|
@ -0,0 +1 @@
|
|||
# 123 My#very/ strange \header*`^ªº-_., yeah
|
2
test/features/rawHeaderId/with-prefixHeaderId.html
Normal file
2
test/features/rawHeaderId/with-prefixHeaderId.html
Normal file
|
@ -0,0 +1,2 @@
|
|||
<h1 id="/prefix/some-header">some header</h1>
|
||||
<h1 id="/prefix/another-!-#$%&/()=?»@£§{[]}«--header">another !"#$%&/()=?»@£§{[]}«' header</h1>
|
3
test/features/rawHeaderId/with-prefixHeaderId.md
Normal file
3
test/features/rawHeaderId/with-prefixHeaderId.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# some header
|
||||
|
||||
# another !"#$%&/()=?»@£§{[]}«' header
|
|
@ -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));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user