codeSpans.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. *
  3. * * Backtick quotes are used for <code></code> spans.
  4. *
  5. * * You can use multiple backticks as the delimiters if you want to
  6. * include literal backticks in the code span. So, this input:
  7. *
  8. * Just type ``foo `bar` baz`` at the prompt.
  9. *
  10. * Will translate to:
  11. *
  12. * <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
  13. *
  14. * There's no arbitrary limit to the number of backticks you
  15. * can use as delimters. If you need three consecutive backticks
  16. * in your code, use four for delimiters, etc.
  17. *
  18. * * You can use spaces to get literal backticks at the edges:
  19. *
  20. * ... type `` `bar` `` ...
  21. *
  22. * Turns to:
  23. *
  24. * ... type <code>`bar`</code> ...
  25. */
  26. showdown.subParser('codeSpans', function (text) {
  27. 'use strict';
  28. /*
  29. text = text.replace(/
  30. (^|[^\\]) // Character before opening ` can't be a backslash
  31. (`+) // $2 = Opening run of `
  32. ( // $3 = The code block
  33. [^\r]*?
  34. [^`] // attacklab: work around lack of lookbehind
  35. )
  36. \2 // Matching closer
  37. (?!`)
  38. /gm, function(){...});
  39. */
  40. text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm, function (wholeMatch, m1, m2, m3) {
  41. var c = m3;
  42. c = c.replace(/^([ \t]*)/g, ''); // leading whitespace
  43. c = c.replace(/[ \t]*$/g, ''); // trailing whitespace
  44. c = showdown.subParser('encodeCode')(c);
  45. return m1 + '<code>' + c + '</code>';
  46. });
  47. return text;
  48. });