shikeying
2023-03-17 8c1a723d62a6aa5d6266ca613ae4eb77c789db06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
 
var escapeHTML = require('../Utils').escapeHTML;
 
/**
 * Create a linkified and HTML escaped entry field description.
 *
 * As a special feature, this description may contain both markdown
 * and plain <a href> links.
 *
 * @param {String} description
 */
module.exports = function entryFieldDescription(description) {
 
  // we tokenize the description to extract text, HTML and markdown links
  // text and links are handled seperately
 
  var escaped = [];
 
  // match markdown [{TEXT}]({URL}) and HTML links <a href="{URL}">{TEXT}</a>
  var pattern = /(?:\[([^\]]+)\]\((https?:\/\/[^"<>\]]+)\))|(?:<a href="(https?:\/\/[^"<>]+)">([^<]*)<\/a>)/gi;
 
  var index = 0;
  var match;
  var link, text;
 
  while ((match = pattern.exec(description))) {
 
    // escape + insert text before match
    if (match.index > index) {
      escaped.push(escapeHTML(description.substring(index, match.index)));
    }
 
    link = match[2] || match[3];
    text = match[1] || match[4];
 
    // insert safe link
    escaped.push('<a href="' + link + '" target="_blank">' + escapeHTML(text) + '</a>');
 
    index = match.index + match[0].length;
  }
 
  // escape and insert text after last match
  if (index < description.length) {
    escaped.push(escapeHTML(description.substring(index)));
  }
 
  return '<div class="bpp-field-description">' + escaped.join('') + '</div>';
};