1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- showdown.subParser('headers', function (text, options, globals) {
- 'use strict';
- var prefixHeader = options.prefixHeaderId;
- // Set text-style headers:
- // Header 1
- // ========
- //
- // Header 2
- // --------
- //
- text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm, function (wholeMatch, m1) {
- var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
- hID = (options.noHeaderId) ? '' : 'id="' + headerId(m1) + '"',
- hashBlock = '<h1' + hID + '>' + spanGamut + '</h1>';
- return showdown.subParser('hashBlock')(hashBlock, options, globals);
- });
- text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm, function (matchFound, m1) {
- var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
- hID = (options.noHeaderId) ? '' : 'id="' + headerId(m1) + '"',
- hashBlock = '<h2' + hID + '>' + spanGamut + '</h2>';
- return showdown.subParser('hashBlock')(hashBlock, options, globals);
- });
- // atx-style headers:
- // # Header 1
- // ## Header 2
- // ## Header 2 with closing hashes ##
- // ...
- // ###### Header 6
- //
- /*
- text = text.replace(/
- ^(\#{1,6}) // $1 = string of #'s
- [ \t]*
- (.+?) // $2 = Header text
- [ \t]*
- \#* // optional closing #'s (not counted)
- \n+
- /gm, function() {...});
- */
- text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm, function (wholeMatch, m1, m2) {
- var span = showdown.subParser('spanGamut')(m2, options, globals),
- hID = (options.noHeaderId) ? '' : 'id="' + headerId(m1) + '"',
- header = '<h' + m1.length + hID + '>' + span + '</h' + m1.length + '>';
- return showdown.subParser('hashBlock')(header, options, globals);
- });
- function headerId(m) {
- 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;
- });
|