[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/Litedown/Parser/Passes/ -> Links.js (source)

   1  function parse()
   2  {
   3      if (text.indexOf('](') !== -1)
   4      {
   5          parseInlineLinks();
   6      }
   7      if (text.indexOf('<') !== -1)
   8      {
   9          parseAutomaticLinks();
  10      }
  11      if (hasReferences)
  12      {
  13          parseReferenceLinks();
  14      }
  15  }
  16  
  17  /**
  18  * Add an image tag for given text span
  19  *
  20  * @param {number} startPos Start tag position
  21  * @param {number} endPos   End tag position
  22  * @param {number} endLen   End tag length
  23  * @param {string} linkInfo URL optionally followed by space and a title
  24  */
  25  function addLinkTag(startPos, endPos, endLen, linkInfo)
  26  {
  27      // Give the link a slightly worse priority if this is a implicit reference and a slightly
  28      // better priority if it's an explicit reference or an inline link or to give it precedence
  29      // over possible BBCodes such as [b](https://en.wikipedia.org/wiki/B)
  30      var priority = (endLen === 1) ? 1 : -1;
  31  
  32      var tag = addTagPair('URL', startPos, 1, endPos, endLen, priority);
  33      setLinkAttributes(tag, linkInfo, 'url');
  34  
  35      // Overwrite the markup without touching the link's text
  36      overwrite(startPos, 1);
  37      overwrite(endPos,   endLen);
  38  }
  39  
  40  /**
  41  * Capture and return labels used in current text
  42  *
  43  * @return {!Object} Labels' text position as keys, lowercased text content as values
  44  */
  45  function getLabels()
  46  {
  47      var labels = {}, m, regexp = /\[((?:[^\x17[\]]|\[[^\x17[\]]*\])*)\]/g;
  48      while (m = regexp.exec(text))
  49      {
  50          labels[m.index] = m[1].toLowerCase();
  51      }
  52  
  53      return labels;
  54  }
  55  
  56  /**
  57  * Parse automatic links markup
  58  */
  59  function parseAutomaticLinks()
  60  {
  61      var m, regexp = /<[-+.\w]+([:@])[^\x17\s>]+?(?:>|\x1B7)/g;
  62      while (m = regexp.exec(text))
  63      {
  64          // Re-escape escape sequences in automatic links
  65          var content  = decode(m[0].replace(/\x1B/g, "\\\x1B")).replace(/^<(.+)>$/, '$1'),
  66              startPos = m.index,
  67              endPos   = startPos + m[0].length - 1,
  68  
  69              tagName  = (m[1] === ':') ? 'URL' : 'EMAIL',
  70              attrName = tagName.toLowerCase();
  71  
  72          addTagPair(tagName, startPos, 1, endPos, 1).setAttribute(attrName, content);
  73      }
  74  }
  75  
  76  /**
  77  * Parse inline links markup
  78  */
  79  function parseInlineLinks()
  80  {
  81      var m, regexp = /\[(?:[^\x17[\]]|\[[^\x17[\]]*\])*\]\(( *(?:\([^\x17\s()]*\)|[^\x17\s)])*(?=[ )]) *(?:"[^\x17]*?"|'[^\x17]*?'|\([^\x17)]*\))? *)\)/g;
  82      while (m = regexp.exec(text))
  83      {
  84          var linkInfo = m[1],
  85              startPos = m.index,
  86              endLen   = 3 + linkInfo.length,
  87              endPos   = startPos + m[0].length - endLen;
  88  
  89          addLinkTag(startPos, endPos, endLen, linkInfo);
  90      }
  91  }
  92  
  93  /**
  94  * Parse reference links markup
  95  */
  96  function parseReferenceLinks()
  97  {
  98      var labels = getLabels(), startPos;
  99      for (startPos in labels)
 100      {
 101          var id       = labels[startPos],
 102              labelPos = +startPos + 2 + id.length,
 103              endPos   = labelPos - 1,
 104              endLen   = 1;
 105  
 106          if (text[labelPos] === ' ')
 107          {
 108              ++labelPos;
 109          }
 110          if (labels[labelPos] > '' && linkReferences[labels[labelPos]])
 111          {
 112              id     = labels[labelPos];
 113              endLen = labelPos + 2 + id.length - endPos;
 114          }
 115          if (linkReferences[id])
 116          {
 117              addLinkTag(+startPos, endPos, endLen, linkReferences[id]);
 118          }
 119      }
 120  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1