How to use hexo-util - 10 common examples

To help you get started, we’ve selected a few hexo-util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github greg-js / hexo-cli-extras / lib / rename.js View on Github external
function renameFile(art, renamed) {

      var src = art.full_source;
      var newSrc = path.join(src.substr(0, src.lastIndexOf(path.sep)), slugize(renamed, {transform: 1}));

      // first the markdown file
      return fsRename(src, newSrc + '.md').then(function doRenameMd() {
        var fldr;

        console.log(chalk.red(src) + ' renamed to ' + chalk.green(newSrc) + '.md');

        fldr = src.substr(0, src.lastIndexOf('.'));

        // then the folder if it exists
        return fsStat(fldr).then(function doCheckDir(stats) {
          if (stats.isDirectory()) {
            return fsRename(fldr, newSrc).then(function doRenameDir() {
              console.log(chalk.underline('Asset folder renamed as well.'));
            });
          } else {
github hexojs / hexo / lib / hexo / post.js View on Github external
Post.prototype.publish = function(data, replace, callback) {
  if (!callback && typeof replace === 'function') {
    callback = replace;
    replace = false;
  }

  if (data.layout === 'draft') data.layout = 'post';

  const ctx = this.context;
  const { config } = ctx;
  const draftDir = join(ctx.source_dir, '_drafts');
  const slug = slugize(data.slug.toString(), {transform: config.filename_case});
  data.slug = slug;
  const regex = new RegExp(`^${escapeRegExp(slug)}(?:[^\\/\\\\]+)`);
  let src = '';
  const result = {};

  data.layout = (data.layout || config.default_layout).toLowerCase();

  // Find the draft
  return fs.listDir(draftDir).then(list => {
    return list.find(item => regex.test(item));
  }).then(item => {
    if (!item) throw new Error(`Draft "${slug}" does not exist.`);

    // Read the content
    src = join(draftDir, item);
    return fs.readFile(src);
github zthxxx / hexo-directory-category / lib / common.js View on Github external
const moment = require('moment-timezone');
const minimatch = require('minimatch');
const _ = require('lodash');

const DURATION_MINUTE = 1000 * 60;

function isTmpFile(path) {
  const last = path[path.length - 1];
  return last === '%' || last === '~';
}

function isHiddenFile(path) {
  return /(^|\/)[_\.]/.test(path); // eslint-disable-line no-useless-escape
}

exports.ignoreTmpAndHiddenFile = new Pattern(path => {
  if (isTmpFile(path) || isHiddenFile(path)) return false;
  return true;
});

exports.isTmpFile = isTmpFile;
exports.isHiddenFile = isHiddenFile;

exports.toDate = date => {
  if (!date || moment.isMoment(date)) return date;

  if (!(date instanceof Date)) {
    date = new Date(date);
  }

  if (isNaN(date.getTime())) return;
github hexojs / hexo / lib / plugins / filter / before_post_render / backtick_code_block.js View on Github external
if (match[3]) {
            options.caption += `<a href="${match[3]}">${match[4] ? match[4] : 'link'}</a>`;
          }
        }
      }
    }

    // PR #3765
    if (start.includes('&gt;')) {
      const depth = start.split('&gt;').length - 1;
      const regexp = new RegExp(`^([^\\S\\r\\n]*&gt;){0,${depth}}([^\\S\\r\\n]|$)`, 'mg');
      const paddingOnEnd = ' '; // complement uncaptured whitespaces at last line
      content = (content + paddingOnEnd).replace(regexp, '').replace(/\n$/, '');
    }

    content = highlight(stripIndent(content), options)
      .replace(/{/g, '{')
      .replace(/}/g, '}');

    return `${start}${content}${end}`;
  });
}
github hexojs / hexo / lib / plugins / tag / img.js View on Github external
return function imgTag(args, content) {
    const classes = [];
    let src, width, height, title, alt;

    // Find image URL and class name
    while (args.length > 0) {
      const item = args.shift();
      if (rUrl.test(item) || item[0] === '/') {
        src = url_for.call(ctx, item);
        break;
      } else {
        classes.push(item);
      }
    }

    // Find image width and height
    if (args && args.length) {
      if (!/\D+/.test(args[0])) {
        width = args.shift();

        if (args.length && !/\D+/.test(args[0])) {
          height = args.shift();
        }
      }
github hexojs / hexo / lib / plugins / helper / tagcloud.js View on Github external
options = options || {};

  const min = options.min_font || 10;
  const max = options.max_font || 20;
  const orderby = options.orderby || 'name';
  const order = options.order || 1;
  const unit = options.unit || 'px';
  let color = options.color;
  const { transform } = options;
  const separator = options.separator || ' ';
  const result = [];
  let startColor, endColor;

  if (color) {
    startColor = new Color(options.start_color);
    endColor = new Color(options.end_color);

    if (!startColor || !endColor) color = false;
  }

  // Sort the tags
  if (orderby === 'random' || orderby === 'rand') {
    tags = tags.random();
  } else {
    tags = tags.sort(orderby, order);
  }

  // Limit the number of tags
  if (options.amount) {
    tags = tags.limit(options.amount);
  }
github hexojs / hexo / lib / plugins / helper / tagcloud.js View on Github external
if (!tags || !tags.length) return '';
  options = options || {};

  const min = options.min_font || 10;
  const max = options.max_font || 20;
  const orderby = options.orderby || 'name';
  const order = options.order || 1;
  const unit = options.unit || 'px';
  let color = options.color;
  const { transform } = options;
  const separator = options.separator || ' ';
  const result = [];
  let startColor, endColor;

  if (color) {
    startColor = new Color(options.start_color);
    endColor = new Color(options.end_color);

    if (!startColor || !endColor) color = false;
  }

  // Sort the tags
  if (orderby === 'random' || orderby === 'rand') {
    tags = tags.random();
  } else {
    tags = tags.sort(orderby, order);
  }

  // Limit the number of tags
  if (options.amount) {
    tags = tags.limit(options.amount);
  }
github minamo173 / hexo-tag-link-preview / linkPreview.js View on Github external
}

        descriptions += util.htmlTag('div', { class: 'og-title' }, escapeHTML(ogp.ogTitle))

        // Description
        if (config.description && ogp.hasOwnProperty('ogDescription')) {
          let description = ogp.ogDescription

          if (description && description.length > config.descriptionLength) {
            description = description.slice(0, config.descriptionLength) + '…'
          }

          descriptions += util.htmlTag('div', { class: 'og-description' }, escapeHTML(description))
        }

        descriptions = util.htmlTag('div', { class: 'descriptions' }, descriptions)

        const tag = util.htmlTag('div', { class: 'link-area' }, image + descriptions)

        return util.htmlTag('a', { href: url, class: config.className, target: config.target, rel: config.rel }, tag)
      })
      .catch(function (error) {
github cypress-io / cypress / docs / scripts / tags.js View on Github external
hexo.extend.tag.register('issue', function (args) {
  // {% issue 74 'not currently supported' %}

  const num = args[0]

  const attrs = {
    href: `https://github.com/cypress-io/cypress/issues/${num}`,
    target: '_blank',
  }

  const text = args[1] || `issue #${num}`

  return util.htmlTag('a', attrs, text)
})
github minamo173 / hexo-tag-link-preview / linkPreview.js View on Github external
// Description
        if (config.description && ogp.hasOwnProperty('ogDescription')) {
          let description = ogp.ogDescription

          if (description && description.length > config.descriptionLength) {
            description = description.slice(0, config.descriptionLength) + '…'
          }

          descriptions += util.htmlTag('div', { class: 'og-description' }, escapeHTML(description))
        }

        descriptions = util.htmlTag('div', { class: 'descriptions' }, descriptions)

        const tag = util.htmlTag('div', { class: 'link-area' }, image + descriptions)

        return util.htmlTag('a', { href: url, class: config.className, target: config.target, rel: config.rel }, tag)
      })
      .catch(function (error) {