txt.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. showdown.subParser('makeMarkdown.txt', function (node) {
  2. 'use strict';
  3. var txt = node.nodeValue;
  4. // multiple spaces are collapsed
  5. txt = txt.replace(/ +/g, ' ');
  6. // replace the custom ¨NBSP; with a space
  7. txt = txt.replace(/¨NBSP;/g, ' ');
  8. // ", <, > and & should replace escaped html entities
  9. txt = showdown.helper.unescapeHTMLEntities(txt);
  10. // escape markdown magic characters
  11. // emphasis, strong and strikethrough - can appear everywhere
  12. // we also escape pipe (|) because of tables
  13. // and escape ` because of code blocks and spans
  14. txt = txt.replace(/([*_~|`])/g, '\\$1');
  15. // escape > because of blockquotes
  16. txt = txt.replace(/^(\s*)>/g, '\\$1>');
  17. // hash character, only troublesome at the beginning of a line because of headers
  18. txt = txt.replace(/^#/gm, '\\#');
  19. // horizontal rules
  20. txt = txt.replace(/^(\s*)([-=]{3,})(\s*)$/, '$1\\$2$3');
  21. // dot, because of ordered lists, only troublesome at the beginning of a line when preceded by an integer
  22. txt = txt.replace(/^( {0,3}\d+)\./gm, '$1\\.');
  23. // +, * and -, at the beginning of a line becomes a list, so we need to escape them also (asterisk was already escaped)
  24. txt = txt.replace(/^( {0,3})([+-])/gm, '$1\\$2');
  25. // images and links, ] followed by ( is problematic, so we escape it
  26. txt = txt.replace(/]([\s]*)\(/g, '\\]$1\\(');
  27. // reference URIs must also be escaped
  28. txt = txt.replace(/^ {0,3}\[([\S \t]*?)]:/gm, '\\[$1]:');
  29. return txt;
  30. });