italicsAndBold.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. showdown.subParser('makehtml.italicsAndBold', function (text, options, globals) {
  2. 'use strict';
  3. text = globals.converter._dispatch('makehtml.italicsAndBold.before', text, options, globals);
  4. // it's faster to have 3 separate regexes for each case than have just one
  5. // because of backtracing, in some cases, it could lead to an exponential effect
  6. // called "catastrophic backtrace". Ominous!
  7. function parseInside (txt, left, right) {
  8. /*
  9. if (options.simplifiedAutoLink) {
  10. txt = showdown.subParser('simplifiedAutoLinks')(txt, options, globals);
  11. }
  12. */
  13. return left + txt + right;
  14. }
  15. // Parse underscores
  16. if (options.literalMidWordUnderscores) {
  17. text = text.replace(/\b___(\S[\s\S]*)___\b/g, function (wm, txt) {
  18. return parseInside (txt, '<strong><em>', '</em></strong>');
  19. });
  20. text = text.replace(/\b__(\S[\s\S]*)__\b/g, function (wm, txt) {
  21. return parseInside (txt, '<strong>', '</strong>');
  22. });
  23. text = text.replace(/\b_(\S[\s\S]*?)_\b/g, function (wm, txt) {
  24. return parseInside (txt, '<em>', '</em>');
  25. });
  26. } else {
  27. text = text.replace(/___(\S[\s\S]*?)___/g, function (wm, m) {
  28. return (/\S$/.test(m)) ? parseInside (m, '<strong><em>', '</em></strong>') : wm;
  29. });
  30. text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) {
  31. return (/\S$/.test(m)) ? parseInside (m, '<strong>', '</strong>') : wm;
  32. });
  33. text = text.replace(/_([^\s_][\s\S]*?)_/g, function (wm, m) {
  34. // !/^_[^_]/.test(m) - test if it doesn't start with __ (since it seems redundant, we removed it)
  35. return (/\S$/.test(m)) ? parseInside (m, '<em>', '</em>') : wm;
  36. });
  37. }
  38. // Now parse asterisks
  39. if (options.literalMidWordAsterisks) {
  40. text = text.replace(/([^*]|^)\B\*\*\*(\S[\s\S]+?)\*\*\*\B(?!\*)/g, function (wm, lead, txt) {
  41. return parseInside (txt, lead + '<strong><em>', '</em></strong>');
  42. });
  43. text = text.replace(/([^*]|^)\B\*\*(\S[\s\S]+?)\*\*\B(?!\*)/g, function (wm, lead, txt) {
  44. return parseInside (txt, lead + '<strong>', '</strong>');
  45. });
  46. text = text.replace(/([^*]|^)\B\*(\S[\s\S]+?)\*\B(?!\*)/g, function (wm, lead, txt) {
  47. return parseInside (txt, lead + '<em>', '</em>');
  48. });
  49. } else {
  50. text = text.replace(/\*\*\*(\S[\s\S]*?)\*\*\*/g, function (wm, m) {
  51. return (/\S$/.test(m)) ? parseInside (m, '<strong><em>', '</em></strong>') : wm;
  52. });
  53. text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, function (wm, m) {
  54. return (/\S$/.test(m)) ? parseInside (m, '<strong>', '</strong>') : wm;
  55. });
  56. text = text.replace(/\*([^\s*][\s\S]*?)\*/g, function (wm, m) {
  57. // !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it)
  58. return (/\S$/.test(m)) ? parseInside (m, '<em>', '</em>') : wm;
  59. });
  60. }
  61. text = globals.converter._dispatch('makehtml.italicsAndBold.after', text, options, globals);
  62. return text;
  63. });