How to use the hexo-util.Pattern function in hexo-util

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 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 / template_locals / i18n.js View on Github external
function i18nLocalsFilter(locals) {
  const { i18n } = this.theme;
  const { config } = this;
  const i18nDir = config.i18n_dir;
  const { page } = locals;
  let lang = page.lang || page.language;
  const i18nLanguages = i18n.list();
  const i18nConfigLanguages = i18n.languages;

  if (!lang) {
    const pattern = new Pattern(`${i18nDir}/*path`);
    const data = pattern.match(locals.path);

    if (data && data.lang && i18nLanguages.includes(data.lang)) {
      lang = data.lang;
      page.canonical_path = data.path;
    } else {
      // i18n.languages is always an array with at least one argument ('default')
      lang = i18nConfigLanguages[0];
    }

    page.lang = lang;
  }

  page.canonical_path = page.canonical_path || locals.path;

  const languages = [...new Set([].concat(lang, i18nConfigLanguages, i18nLanguages).filter(Boolean))];
github hexojs / hexo / lib / plugins / processor / common.js View on Github external
const last = path[path.length - 1];
  return last === '%' || last === '~';
}

function isHiddenFile(path) {
  return /(^|\/)[_.]/.test(path);
}

function isExcludedFile(path, config) {
  if (isTmpFile(path)) return true;
  if (isMatch(path, config.exclude)) return true;
  if (isHiddenFile(path) && !isMatch(path, config.include)) return true;
  return false;
}

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

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

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 / theme / processors / config.js View on Github external
file.box.config = {};
    return;
  }

  const self = this;

  return file.render().then(result => {
    file.box.config = result;
    self.log.debug('Theme config loaded.');
  }).catch(err => {
    self.log.error('Theme config load failed.');
    throw err;
  });
};

exports.pattern = new Pattern(/^_config\.\w+$/);
github ikeq / hexo-theme-inside / lib / theme-processor / localized-script.js View on Github external
module.exports = function (hexo) {
  const lang = localeId(hexo.config.language);

  return {
    process: function (file) {
      const Asset = this.model('Asset');
      const id = file.source.substring(this.base_dir.length).replace(/\\/g, '/')
      const doc = Asset.findById(id);
      if (doc && (!localeScripts[lang] || !file.path.match(localeScripts[lang]))) doc.remove();
    },
    pattern: new Pattern(/source\/main.*\.js$/)
  }
}
github hexojs / hexo / lib / plugins / processor / asset.js View on Github external
return doc.remove();
      }

      return;
    }

    return Asset.save({
      _id: id,
      path: file.path,
      modified: file.type !== 'skip',
      renderable: file.params.renderable
    });
  }

  return {
    pattern: new Pattern(path => {
      if (isExcludedFile(path, ctx.config)) return;

      return {
        renderable: ctx.render.isRenderable(path) && !isMatch(path, ctx.config.skip_render)
      };
    }),

    process: function assetProcessor(file) {
      if (file.params.renderable) {
        return processPage(file);
      }

      return processAsset(file);
    }
  };
};
github hexojs / hexo / lib / box / index.js View on Github external
'use strict';

const { join, sep } = require('path');
const Promise = require('bluebird');
const File = require('./file');
const { Pattern, HashStream } = require('hexo-util');
const { createReadStream, readdir, stat, watch } = require('hexo-fs');
const { magenta } = require('chalk');
const { EventEmitter } = require('events');
const { inherits } = require('util');
const { isMatch, makeRe } = require('micromatch');
const ignore = ['**/themes/*/node_modules/**', '**/themes/*/.git/**'];

const defaultPattern = new Pattern(() => ({}));

function Box(ctx, base, options) {
  Reflect.apply(EventEmitter, this, []);

  this.options = Object.assign({
    persistent: true
  }, options);

  if (!base.endsWith(sep)) {
    base += sep;
  }

  this.context = ctx;
  this.base = base;
  this.processors = [];
  this._processingFiles = {};
github hexojs / hexo / lib / box / index.js View on Github external
Box.prototype.addProcessor = function(pattern, fn) {
  if (!fn && typeof pattern === 'function') {
    fn = pattern;
    pattern = defaultPattern;
  }

  if (typeof fn !== 'function') throw new TypeError('fn must be a function');
  if (!(pattern instanceof Pattern)) pattern = new Pattern(pattern);

  this.processors.push({
    pattern,
    process: fn
  });
};
github hexojs / hexo / lib / theme / processors / view.js View on Github external
const { Pattern } = require('hexo-util');

exports.process = file => {
  const { path } = file.params;

  if (file.type === 'delete') {
    file.box.removeView(path);
    return;
  }

  return file.read().then(result => {
    file.box.setView(path, result);
  });
};

exports.pattern = new Pattern('layout/*path');
github hexojs / hexo / lib / plugins / processor / post.js View on Github external
return PostAsset.save({
        _id: id,
        slug: file.source.substring(post.asset_dir.length),
        post: post._id,
        modified: file.type !== 'skip',
        renderable: file.params.renderable
      });
    }

    if (doc) {
      return doc.remove();
    }
  }

  return {
    pattern: new Pattern(path => {
      if (isTmpFile(path)) return;

      let result;

      if (path.startsWith(postDir)) {
        result = {
          published: true,
          path: path.substring(postDir.length)
        };
      } else if (path.startsWith(draftDir)) {
        result = {
          published: false,
          path: path.substring(draftDir.length)
        };
      }