2015-01-16 05:21:33 +08:00
|
|
|
showdown.subParser('headers', function (text, options, globals) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('headers.before', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-07-13 12:09:03 +08:00
|
|
|
var prefixHeader = options.prefixHeaderId,
|
2015-07-15 00:10:52 +08:00
|
|
|
headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
|
2016-12-31 03:01:44 +08:00
|
|
|
ghHeaderId = options.ghCompatibleHeaderId,
|
2015-01-18 10:12:32 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
// Set text-style headers:
|
|
|
|
// Header 1
|
|
|
|
// ========
|
|
|
|
//
|
|
|
|
// Header 2
|
|
|
|
// --------
|
|
|
|
//
|
2015-07-15 00:10:52 +08:00
|
|
|
setextRegexH1 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n={2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n=+[ \t]*\n+/gm,
|
|
|
|
setextRegexH2 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n-{2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n-+[ \t]*\n+/gm;
|
|
|
|
|
|
|
|
text = text.replace(setextRegexH1, function (wholeMatch, m1) {
|
2015-06-08 10:41:14 +08:00
|
|
|
|
2015-01-19 20:04:22 +08:00
|
|
|
var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
|
2015-06-08 10:51:43 +08:00
|
|
|
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
|
2015-07-13 12:09:03 +08:00
|
|
|
hLevel = headerLevelStart,
|
2015-06-17 09:19:44 +08:00
|
|
|
hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
|
2015-01-19 20:04:22 +08:00
|
|
|
return showdown.subParser('hashBlock')(hashBlock, options, globals);
|
2015-01-19 19:37:21 +08:00
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-07-15 00:10:52 +08:00
|
|
|
text = text.replace(setextRegexH2, function (matchFound, m1) {
|
2015-01-19 20:04:22 +08:00
|
|
|
var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
|
2015-06-08 10:51:43 +08:00
|
|
|
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
|
2015-07-13 12:09:03 +08:00
|
|
|
hLevel = headerLevelStart + 1,
|
2015-06-17 09:19:44 +08:00
|
|
|
hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
|
2015-01-19 20:04:22 +08:00
|
|
|
return showdown.subParser('hashBlock')(hashBlock, options, globals);
|
2015-01-19 19:37:21 +08:00
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
// atx-style headers:
|
|
|
|
// # Header 1
|
|
|
|
// ## Header 2
|
|
|
|
// ## Header 2 with closing hashes ##
|
|
|
|
// ...
|
|
|
|
// ###### Header 6
|
|
|
|
//
|
2016-12-17 14:01:15 +08:00
|
|
|
var atxStyle = (options.requireSpaceBeforeHeadingText) ? /^(#{1,6})[ \t]+(.+?)[ \t]*#*\n+/gm : /^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm;
|
|
|
|
|
|
|
|
text = text.replace(atxStyle, function (wholeMatch, m1, m2) {
|
2015-01-19 20:04:22 +08:00
|
|
|
var span = showdown.subParser('spanGamut')(m2, options, globals),
|
2015-06-08 10:51:43 +08:00
|
|
|
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
|
2015-07-13 12:09:03 +08:00
|
|
|
hLevel = headerLevelStart - 1 + m1.length,
|
2015-06-17 09:19:44 +08:00
|
|
|
header = '<h' + hLevel + hID + '>' + span + '</h' + hLevel + '>';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
return showdown.subParser('hashBlock')(header, options, globals);
|
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
function headerId(m) {
|
2016-12-31 03:01:44 +08:00
|
|
|
var title, escapedId;
|
|
|
|
|
|
|
|
if (ghHeaderId) {
|
|
|
|
escapedId = m
|
|
|
|
.replace(/ /g, '-')
|
|
|
|
//replace previously escaped chars (&, ~ and $)
|
|
|
|
.replace(/&/g, '')
|
|
|
|
.replace(/~T/g, '')
|
|
|
|
.replace(/~D/g, '')
|
|
|
|
//replace rest of the chars (&~$ are repeated as they might have been escaped)
|
2016-12-31 03:21:03 +08:00
|
|
|
.replace(/[&~$!@#*()=:/,;?+'.%\\]/g, '')
|
2016-12-31 03:01:44 +08:00
|
|
|
.toLowerCase();
|
|
|
|
} else {
|
|
|
|
escapedId = m.replace(/[^\w]/g, '').toLowerCase();
|
|
|
|
}
|
2015-01-18 10:12:32 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
if (globals.hashLinkCounts[escapedId]) {
|
|
|
|
title = escapedId + '-' + (globals.hashLinkCounts[escapedId]++);
|
|
|
|
} else {
|
|
|
|
title = escapedId;
|
|
|
|
globals.hashLinkCounts[escapedId] = 1;
|
|
|
|
}
|
2015-01-18 10:12:32 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
// Prefix id to prevent causing inadvertent pre-existing style matches.
|
|
|
|
if (prefixHeader === true) {
|
|
|
|
prefixHeader = 'section';
|
|
|
|
}
|
2015-01-18 10:12:32 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
if (showdown.helper.isString(prefixHeader)) {
|
|
|
|
return prefixHeader + title;
|
2015-01-16 05:21:33 +08:00
|
|
|
}
|
2015-01-19 19:37:21 +08:00
|
|
|
return title;
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('headers.after', text, options, globals);
|
2015-01-19 19:37:21 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|