mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
feat(uniqueHeaderId): add unique id prefix and suffix to headers
If two headers have similar texts, the generated id could be equal. In order to prevent id clash: - A unique suffix is added if a header id already exists - Option to add a prefix to header id - Update of correspondent tests - (credits to nicovalencia) Closes #81, closes #82
This commit is contained in:
parent
c9de4b6b1f
commit
c367a4b9a1
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.
|
@ -6,7 +6,8 @@
|
||||||
var showdown = {},
|
var showdown = {},
|
||||||
parsers = {},
|
parsers = {},
|
||||||
globalOptions = {
|
globalOptions = {
|
||||||
omitExtraWLInCodeBlocks: false
|
omitExtraWLInCodeBlocks: false,
|
||||||
|
prefixHeaderId: false
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -29,10 +30,17 @@ showdown.extensions = {};
|
||||||
//Public methods
|
//Public methods
|
||||||
showdown.setOption = function (key, value) {
|
showdown.setOption = function (key, value) {
|
||||||
globalOptions[key] = value;
|
globalOptions[key] = value;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
showdown.getOption = function (key) {
|
||||||
|
return globalOptions[key];
|
||||||
|
};
|
||||||
|
|
||||||
|
showdown.getOptions = function () {
|
||||||
|
return globalOptions;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Static Method
|
* Static Method
|
||||||
*
|
*
|
||||||
|
@ -97,7 +105,8 @@ showdown.Converter = function (converterOptions) {
|
||||||
gHtmlBlocks: [],
|
gHtmlBlocks: [],
|
||||||
gUrls: {},
|
gUrls: {},
|
||||||
gTitles: {},
|
gTitles: {},
|
||||||
gListLevel: 0
|
gListLevel: 0,
|
||||||
|
hashLinkCounts: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// attacklab: Replace ~ with ~T
|
// attacklab: Replace ~ with ~T
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
showdown.subParser('headers', function (text, options, globals) {
|
showdown.subParser('headers', function (text, options, globals) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var prefixHeader = options.prefixHeaderId;
|
||||||
|
|
||||||
// Set text-style headers:
|
// Set text-style headers:
|
||||||
// Header 1
|
// Header 1
|
||||||
// ========
|
// ========
|
||||||
|
@ -52,7 +54,25 @@ showdown.subParser('headers', function (text, options, globals) {
|
||||||
});
|
});
|
||||||
|
|
||||||
function headerId(m) {
|
function headerId(m) {
|
||||||
return m.replace(/[^\w]/g, '').toLowerCase();
|
var title,
|
||||||
|
escapedId = m.replace(/[^\w]/g, '').toLowerCase();
|
||||||
|
|
||||||
|
if (globals.hashLinkCounts[escapedId]) {
|
||||||
|
title = escapedId + '-' + (globals.hashLinkCounts[escapedId]++);
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
|
|
5
test/cases/repeated-headers.html
Normal file
5
test/cases/repeated-headers.html
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<h1 id="sametitle">Same Title</h1>
|
||||||
|
|
||||||
|
<p>some text</p>
|
||||||
|
|
||||||
|
<h1 id="sametitle-1">Same Title</h1>
|
5
test/cases/repeated-headers.md
Normal file
5
test/cases/repeated-headers.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# Same Title
|
||||||
|
|
||||||
|
some text
|
||||||
|
|
||||||
|
# Same Title
|
Loading…
Reference in New Issue
Block a user