Utilities for Hexo.
Clone or download
Latest commit 2b5e282 Apr 25, 2018
Failed to load latest commit information.
lib fix lint error: node/no-deprecated-api Apr 8, 2018
scripts Use cross-spawn instead. Build highlight alias right after npm install Feb 11, 2016
.eslintignore Replace JSHint with ESLint and JSCS Nov 4, 2015
.eslintrc.json rename .eslintrc to .eslintrc.json Apr 8, 2018
.gitignore Change the path of highlight_alias.json Feb 12, 2016
.npmignore Remove postinstall step (#28) Nov 7, 2017
.travis.yml remove jscs Apr 8, 2018
LICENSE First commit Dec 20, 2014
README.md fix linebreak Apr 8, 2018
package.json upgrade mocha Apr 16, 2018



Build Status NPM version Coverage Status dependencies Status devDependencies Status

Utilities for Hexo.


$ npm install hexo-util --save


var util = require('hexo-util');


Caches contents piped to the stream.

var stream = new CacheStream();


stream.on('finish', function(){
  // Read cache piped to the stream

  // Destroy cache

camelCaseKeys(obj, options)

Convert object keys to camelCase. Original keys will be converted to getter/setter and sync to the camelCase keys.

  foo_bar: 'test'
// { fooBar: 'test', foo_bar: 'test' }


Escapes special characters in a regular expression.


Escapes diacritic characters in a string.


Escapes HTML entities in a string.


Generates SHA1 hash.

// <Buffer 7c 4a 8d 09 ca 37 62 af 61 e5 95 20 94 3d c2 64 94 f8 94 1b>


Generates SHA1 hash with a transform stream.

var stream = new HashStream();

  .on('finish', function(){

highlight(str, [options])

Syntax highlighting for a code block.

Option Description Default
gutter Whether to show line numbers true
wrap Whether to wrap the code block true
firstLine First line number 1
hljs Whether to use the hljs-* prefix for CSS classes false
lang Language
caption Caption
tab Replace tabs
autoDetect Detect language automatically false

htmlTag(tag, attrs, text)

Creates a html tag.

htmlTag('img', {src: 'example.png'})
// <img src="example.png">

htmlTag('a', {href: 'http://hexo.io/'}, 'Hexo')
// <a href="http://hexo.io/">Hexo</a>


Parses the string and tests if the string matches the rule. rule can be a string, a regular expression or a function.

var pattern = new Pattern('posts/:id');

// {0: 'posts/89', 1: '89', id: '89'}
var pattern = new Pattern('posts/*path');

// {0: 'posts/2013/hello-world', 1: '2013/hello-world', path: '2013/hello-world'}

Permalink(rule, [options])

Parses a permalink.

Option Description
segments Customize the rule of a segment in the permalink
var permalink = new Permalink(':year/:month/:day/:title', {
  segments: {
    year: /(\d{4})/,
    month: /(\d{2})/,
    day: /(\d{2})/

// {year: '2014', month: '01', day: '31', title: 'test'}

// true

permalink.stringify({year: '2014', month: '01', day: '31', title: 'test'})
// 2014/01/31/test

slugize(str, [options])

Transforms a string into a clean URL-friendly string.

Option Description Default
separator Separator -
transform Transform the string into lower case (1) or upper case (2)
slugize('Hello World') = 'Hello-World'
slugize('Hellô Wòrld') = 'Hello-World'
slugize('Hello World', {separator: '_'}) = 'Hello_World'
slugize('Hello World', {transform: 1}) = 'hello-world'
slugize('Hello World', {transform: 2}) = 'HELLO-WORLD'

spawn(command, [args], [options])

Launches a new process with the given command. This method returns a promise.

Option Description Default
cwd Current working directory of the child process
env Environment key-value pairs
stdio Child's stdio configuration
detached The child will be a process group leader
uid Sets the user identity of the process
gid Sets the group identity of the process
verbose Display messages on the console false
encoding Sets the encoding of the output string utf8
spawn('cat', 'test.txt').then(function(content){


Removes HTML tags in a string.

wordWrap(str, [options])

Wraps the string no longer than line width. This method breaks on the first whitespace character that does not exceed line width.

Option Description Default
width Line width 80
wordWrap('Once upon a time')
// Once upon a time

wordWrap('Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding a successor to the throne turned out to be more trouble than anyone could have imagined...')
// Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding\na successor to the throne turned out to be more trouble than anyone could have\nimagined...

wordWrap('Once upon a time', {width: 8})
// Once\nupon a\ntime

wordWrap('Once upon a time', {width: 1})
// Once\nupon\na\ntime

truncate(str, [options])

Truncates a given text after a given length if text is longer than length. The last characters will be replaced with the omission option for a total length not exceeding length.

Option Description Default
length Max length of the string 30
omission Omission text ...
separator truncate text at a natural break
truncate('Once upon a time in a world far far away')
// "Once upon a time in a world..."

truncate('Once upon a time in a world far far away', {length: 17})
// "Once upon a ti..."

truncate('Once upon a time in a world far far away', {length: 17, separator: ' '})
// "Once upon a..."

truncate('And they found that many people were sleeping better.', {length: 25, omission: '... (continued)'})
// "And they f... (continued)"