headers.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. showdown.subParser('headers', function (text, options, globals) {
  2. 'use strict';
  3. var prefixHeader = options.prefixHeaderId,
  4. headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
  5. // Set text-style headers:
  6. // Header 1
  7. // ========
  8. //
  9. // Header 2
  10. // --------
  11. //
  12. setextRegexH1 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n={2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n=+[ \t]*\n+/gm,
  13. setextRegexH2 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n-{2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n-+[ \t]*\n+/gm;
  14. text = text.replace(setextRegexH1, function (wholeMatch, m1) {
  15. var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
  16. hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
  17. hLevel = headerLevelStart,
  18. hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
  19. return showdown.subParser('hashBlock')(hashBlock, options, globals);
  20. });
  21. text = text.replace(setextRegexH2, function (matchFound, m1) {
  22. var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
  23. hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
  24. hLevel = headerLevelStart + 1,
  25. hashBlock = '<h' + hLevel + hID + '>' + spanGamut + '</h' + hLevel + '>';
  26. return showdown.subParser('hashBlock')(hashBlock, options, globals);
  27. });
  28. // atx-style headers:
  29. // # Header 1
  30. // ## Header 2
  31. // ## Header 2 with closing hashes ##
  32. // ...
  33. // ###### Header 6
  34. //
  35. text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, function (wholeMatch, m1, m2) {
  36. var span = showdown.subParser('spanGamut')(m2, options, globals),
  37. hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
  38. hLevel = headerLevelStart - 1 + m1.length,
  39. header = '<h' + hLevel + hID + '>' + span + '</h' + hLevel + '>';
  40. return showdown.subParser('hashBlock')(header, options, globals);
  41. });
  42. function headerId(m) {
  43. var title, escapedId = m.replace(/[^\w]/g, '').toLowerCase();
  44. if (globals.hashLinkCounts[escapedId]) {
  45. title = escapedId + '-' + (globals.hashLinkCounts[escapedId]++);
  46. } else {
  47. title = escapedId;
  48. globals.hashLinkCounts[escapedId] = 1;
  49. }
  50. // Prefix id to prevent causing inadvertent pre-existing style matches.
  51. if (prefixHeader === true) {
  52. prefixHeader = 'section';
  53. }
  54. if (showdown.helper.isString(prefixHeader)) {
  55. return prefixHeader + title;
  56. }
  57. return title;
  58. }
  59. return text;
  60. });