stripLinkDefinitions.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Strips link definitions from text, stores the URLs and titles in
  3. * hash references.
  4. * Link defs are in the form: ^[id]: url "optional title"
  5. *
  6. * ^[ ]{0,3}\[(.+)\]: // id = $1 attacklab: g_tab_width - 1
  7. * [ \t]*
  8. * \n? // maybe *one* newline
  9. * [ \t]*
  10. * <?(\S+?)>? // url = $2
  11. * [ \t]*
  12. * \n? // maybe one newline
  13. * [ \t]*
  14. * (?:
  15. * (\n*) // any lines skipped = $3 attacklab: lookbehind removed
  16. * ["(]
  17. * (.+?) // title = $4
  18. * [")]
  19. * [ \t]*
  20. * )? // title is optional
  21. * (?:\n+|$)
  22. * /gm,
  23. * function(){...});
  24. *
  25. */
  26. showdown.subParser('stripLinkDefinitions', function (text, options, globals) {
  27. 'use strict';
  28. var regex = /^[ ]{0,3}\[(.+)]:[ \t]*\n?[ \t]*<?(\S+?)>?[ \t]*\n?[ \t]*(?:(\n*)["(](.+?)[")][ \t]*)?(?:\n+|(?=~0))/gm;
  29. // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
  30. text += '~0';
  31. text = text.replace(regex, function (wholeMatch, m1, m2, m3, m4) {
  32. m1 = m1.toLowerCase();
  33. globals.gUrls[m1] = showdown.subParser('encodeAmpsAndAngles')(m2); // Link IDs are case-insensitive
  34. if (m3) {
  35. // Oops, found blank lines, so it's not a title.
  36. // Put back the parenthetical statement we stole.
  37. return m3 + m4;
  38. } else if (m4) {
  39. globals.gTitles[m1] = m4.replace(/"/g, '&quot;');
  40. }
  41. // Completely remove the definition from the text
  42. return '';
  43. });
  44. // attacklab: strip sentinel
  45. text = text.replace(/~0/, '');
  46. return text;
  47. });