all files / packages/links-ext/inline-tag-defs/ link.js

30.77% Statements 4/13
0% Branches 0/4
33.33% Functions 1/3
30.77% Lines 4/13
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                                                                          
var encoder = new require('node-html-encoder').Encoder();
 
var INLINE_LINK = /(\S+)(?:\s+([\s\S]+))?/;
 
/**
 * @dgService linkInlineTagDef
 * @description
 * Process inline link tags (of the form {@link some/uri Some Title}), replacing them with HTML anchors
 * @kind function
 * @param  {Object} url   The url to match
 * @param  {Function} docs error message
 * @return {String}  The html link information
 *
 * @property {boolean} relativeLinks Whether we expect the links to be relative to the originating doc
 */
module.exports = function linkInlineTagDef(getLinkInfo, createDocMessage, log) {
  return {
    name: 'link',
    description: 'Process inline link tags (of the form {@link some/uri Some Title}), replacing them with HTML anchors',
    handler: function(doc, tagName, tagDescription) {
 
      // Parse out the uri and title
      return tagDescription.replace(INLINE_LINK, function(match, uri, title) {
 
        var linkInfo = getLinkInfo(uri, title, doc);
 
        if ( !linkInfo.valid ) {
          log.warn(createDocMessage(linkInfo.error, doc));
        }
 
        var link = '<a href="' + linkInfo.url + '"';
        if (linkInfo.external) {
          link += ' _target="blank"';
        }
        link += '>' + encoder.htmlEncode(linkInfo.title) + '</a>';
 
        return  link;
      });
    }
  };
};