Beispiel #1
0
function linkToHelper(path, text, options) {
  if (typeof options === 'boolean') options = {external: options};
  options = options || {};

  if (!text) text = path.replace(/^https?:\/\/|\/$/g, '');

  var attrs = {
    href: this.url_for(path),
    title: text
  };

  var keys = Object.keys(options);
  var key = '';

  for (var i = 0, len = keys.length; i < len; i++) {
    key = keys[i];
    attrs[key] = options[key];
  }

  if (attrs.external) {
    attrs.target = '_blank';
    attrs.rel = 'noopener';
    attrs.external = null;
  }

  if (attrs.class && Array.isArray(attrs.class)) {
    attrs.class = attrs.class.join(' ');
  }

  return htmlTag('a', attrs, text);
}
Beispiel #2
0
hexo.extend.tag.register('tag_cfg', function(args, content){
    var fieldName = args[0],
        tagName = args[1] || 'p',
        cls = args[2] || 'cfg-val',
        fields = fieldName.split('.'),
        cfg = hexo.config,
        cfg1 = hexo.theme.config,
        getPro = function(obj, fds){
            var len = fds.length,
                val = "";
            for (var i = 0; i < len; i++) {
                val = obj[fds[i]];
                if (typeof val === 'object') {
                    if (fds.shift()){
                        return getPro(val, fds);
                    }
                    return JSON.stringify(val);
                }
                return val;
            }
        };
    
    return util.htmlTag(tagName, {
        "class": cls
    }, getPro(cfg, fields) || getPro(cfg1, fields));

});
Beispiel #3
0
  it('google_plus - options', function() {
    var result = openGraph.call({
      page: {},
      config: hexo.config,
      is_post: isPost
    }, {google_plus: '+123456789'});

    result.should.contain(tag('link', {rel: 'publisher', href: '+123456789'}));
  });
Beispiel #4
0
hexo.extend.tag.register('pimg', function(args,content){
    var imageName = args[0],
        altText = "",
        imgAttr = "{}",
        themeConfig = hexo.theme.config;

    switch(args.length){
        case 1:
            break;
        case 2:
            altText = args[1]
            if(altText.length > 1 && altText[0] === '{' && altText[altText.length-1] === '}'){
                imgAttr = altText;
            }
            break;
        case 3:
            altText = args[1];
            imgAttr = args[2];
            break;       
    }
    try {
        imgAttr = JSON.parse(imgAttr);
    }catch(e){
        console.log('scripts.helpers.pimg', e);
        imgAttr = {};
    }
    imgAttr.src   = hexo.config.root + (themeConfig.post.img_dir||postImgDir) + imageName;
    imgAttr.alt = imgAttr.alt || altText;

    // spaces proccess
    for(var p in imgAttr){
        if(typeof imgAttr[p] !== 'string') continue;
        imgAttr[p] = imgAttr[p].replace(/\%20/g, ' ');
    }

    return ('<p class="user_img">' + util.htmlTag('img', imgAttr) + '</p>');
});
Beispiel #5
0
hexo.extend.filter.register('after_post_render', function(data) {
	data.content =
		util.htmlTag('script', {src: '/' + scriptDir + aplayerScript}, " ") +
		data.content;
	return data;
});
Beispiel #6
0
 it('return original image with html tag', function() {
   var tag = tagUtil.convertAttr('9528576237 o'.split(' '));
   var $ = cheerio.load(hexoUtil.htmlTag('img', tagUtil.imgFormat(tag, jsonData)));
   $('img').attr('src').should.eql('https://farm6.staticflickr.com/5445/9528576237_2bf761518c_o.jpg');
 });
Beispiel #7
0
hexo.extend.helper.register('next_url', function(path, text, options) {
  var htmlTag = require('hexo-util').htmlTag;
  var config = this.config;
  var url = require('url');
  var data = url.parse(path);
  var siteHost = url.parse(config.url).hostname || config.url;

  var theme = hexo.theme.config;
  var exturl = '';
  var tag = 'a';
  var attrs = { href: this.url_for(path) };

  // If `exturl` enabled, set spanned links only on external links.
  if (theme.exturl && data.protocol && data.hostname !== siteHost) {
    tag = 'span';
    exturl = 'exturl';
    var encoded = new Buffer(path).toString('base64');
    attrs = {
      class     : exturl,
      'data-url': encoded
    };
  }

  options = options || {};

  var keys = Object.keys(options);
  var key = '';

  for (var i = 0, len = keys.length; i < len; i++) {
    key = keys[i];

    /**
     * If option have `class` attribute, add it to
     * 'exturl' class if `exturl` option enabled.
     */
    if (exturl !== '' && key === 'class') {
      attrs[key] += ' ' + options[key];
    } else {
      attrs[key] = options[key];
    }
  }

  if (attrs.class && Array.isArray(attrs.class)) {
    attrs.class = attrs.class.join(' ');
  }

  // If it's external link, rewrite attributes.
  if (data.protocol && data.hostname !== siteHost) {
    attrs.external = null;

    if (!theme.exturl) {
      // Only for simple link need to rewrite/add attributes.
      attrs.rel = 'noopener';
      attrs.target = '_blank';
    } else {
      // Remove rel attributes for `exturl` in main menu.
      attrs.rel = null;
    }
  }

  return htmlTag(tag, attrs, text);
});
Beispiel #8
0
 function meta(options) {
   return tag('meta', options);
 }