Module: utils

Source:

Members

(inner, constant) default404 :String

Hikaru's default 404 page content for server.

Type:
  • String
Source:

(inner, constant) hikaruDir

Hikaru's package dir.

Source:

(inner, constant) isBinaryFileSync

You should use isBinaryPath(p) mostly. This is only for compatibility because isbinaryfile dependency is dropped.

Deprecated:
  • Yes
Source:

(inner, constant) resolveHeaderIDs

Use resolveHeadingIDs(node) instead.

Deprecated:
  • Yes
Source:

Methods

(inner) checkType(var, name, …types)

Check whether a variable is one of give types.

Parameters:
Name Type Attributes Description
var *
name String

Variable name.

types String <repeatable>
Source:

(inner) delSite(site, key, file)

Delete file from an array in site which have the same source.

Parameters:
Name Type Description
site Site
key String
file File
Source:

(inner) escapeHTML(str) → {String}

Escape HTML chars.

Parameters:
Name Type Description
str String
Source:
Returns:

Escaped HTML string.

Type
String

(inner) fallbackSort(arr, …fns)

Sort an array with a fallback list, call comparing function from first to last until there is a difference.

Parameters:
Name Type Attributes Description
arr Array
fns compareCallback <repeatable>
Source:

(inner) formatDateTimeFn(locales) → {formatDateTime}

Get a function to format date and time, with locale support if Intl is available.

Parameters:
Name Type Description
locales String
Source:
See:
Returns:
Type
formatDateTime

(inner) formatDateTimeSimple(dtopt) → {String}

Format date and time to YYYY-MM-DD HH:mm:ss.

Parameters:
Name Type Attributes Default Description
dt * <optional>
new Date()
Source:
Returns:
Type
String

(inner) genCategories(posts) → {CategoriesData}

Generate categories from posts.

Parameters:
Name Type Description
posts Array.<File>
Source:
Returns:
Type
CategoriesData

(inner) genTOC(node, optsopt)

Generate TOC from HTML headings.

Parameters:
Name Type Attributes Description
node Object

parse5 Node.

opts Object <optional>
Source:

(inner) genTags(posts) → {TagsData}

Generate tags from posts.

Parameters:
Name Type Description
posts Array.<File>
Source:
Returns:
Type
TagsData

(inner) getContentType(docPath) → {String}

Detect Content-Type via filename.

Parameters:
Name Type Description
docPath String
Source:
Returns:

Content-Type value.

Type
String

(inner) getFrontMatter(str) → {FrontMatter}

Get front-matter from string, front-matter here should be at the beginning of string (so UTF-8 BOM is not supported), and should begin with --- in the first line, and should have valid YAML in lines after that, and should end with --- in the next line.

Parameters:
Name Type Description
str String
Source:
Returns:
Type
FrontMatter

(inner) getFullDocPath(file) → {String}

Get File's full document path.

Parameters:
Name Type Description
file File
Source:
Returns:
Type
String

(inner) getFullSrcPath(file) → {String}

Get File's full src path.

Parameters:
Name Type Description
file File
Source:
Returns:
Type
String

(inner) getNodeAttr(node, attrName) → {String}

Get an attribute value from parse5 Node.

Parameters:
Name Type Description
node Object

parse5 Node.

attrName String
Source:
See:
Returns:

Value of the attribute, null if not available.

Type
String

(inner) getNodeText(node) → {String}

Get text content of a parse5 Node.

Parameters:
Name Type Description
node Object

parse5 Node.

Source:
See:
Returns:
Type
String

(inner) getPathFn(rootDiropt) → {getPath}

Get a function to handle full website path.

Parameters:
Name Type Attributes Description
rootDir String <optional>

Site rootDir.

Source:
Returns:
Type
getPath

(inner) getURLFn(baseURLopt, rootDiropt) → {getURL}

Get a function to handle full website URL.

Parameters:
Name Type Attributes Description
baseURL String <optional>

Site baseURL.

rootDir String <optional>

Site rootDir.

Source:
Returns:
Type
getURL

(inner) getURLProtocol(url) → {String}

Get protocol of a URL.

Parameters:
Name Type Description
url String
Source:
Returns:

If no protocol return null.

Type
String

(inner) getVersion() → {String}

Get Hikaru version.

Source:
Returns:
Type
String

(inner) isArray(o) → {Boolean}

Parameters:
Name Type Description
o *
Source:
Returns:
Type
Boolean

(inner) isBinary(b) → {Boolean}

Check whether a buffer is a binary. You should use isBinaryPath(p) because this is costly and mostly a false positive is harmless so we don't need an exact check. For non-costly exact check, add isbinaryfile dependency for your project.

Parameters:
Name Type Description
b Buffer
Source:
Returns:
Type
Boolean

(inner) isBinaryFile(o) → {Boolean}

You should use isBinaryPath(p) mostly. This is only for compatibility because isbinaryfile dependency is dropped.

Parameters:
Name Type Description
o String | Buffer
Deprecated:
  • Yes
Source:
Returns:
Type
Boolean

(inner) isBinaryPath(p) → {Boolean}

Check whether a path is a binary via ext name. For non-costly exact check, add isbinaryfile dependency for your project.

Parameters:
Name Type Description
p String
Source:
Returns:
Type
Boolean

(inner) isBuffer(o) → {Boolean}

Parameters:
Name Type Description
o *
Source:
Returns:
Type
Boolean

(inner) isCurrentHostFn(baseURL, rootDiropt) → {isCurrentHost}

Get a function to handle if parameter is on current host.

Parameters:
Name Type Attributes Description
baseURL String

Site baseURL.

rootDir String <optional>

Site rootDir.

Source:
Returns:
Type
isCurrentHost

(inner) isCurrentPathFn(rootDiropt, currentPathopt) → {isCurrentPath}

Get a function to handle if parameter is current path.

Parameters:
Name Type Attributes Description
rootDir String <optional>

Site rootDir.

currentPath String <optional>

current page's path.

Source:
Returns:
Type
isCurrentPath

(inner) isFunction(o) → {Boolean}

Parameters:
Name Type Description
o *
Source:
Returns:
Type
Boolean

(inner) isNumber(o) → {Boolean}

Parameters:
Name Type Description
o *
Source:
Returns:
Type
Boolean

(inner) isObject(o) → {Boolean}

Parameters:
Name Type Description
o *
Source:
Returns:

Return false when o == null.

Type
Boolean

(inner) isReadableSync(p) → {Boolean}

Node.js marks fs.exists() deprecated and suggest to use fs.access(), but it throws error instead of return a boolean, this is a wrapper for it.

Parameters:
Name Type Description
p String
Source:
See:
Returns:
Type
Boolean

(inner) isString(o) → {Boolean}

Parameters:
Name Type Description
o *
Source:
Returns:
Type
Boolean

(async, inner) loadJSON(path) → {Promise.<Object>}

Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise.<Object>

(inner) loadJSONSync(path) → {Object}

Parameters:
Name Type Description
path String
Source:
Returns:
Type
Object

(async, inner) loadYAML(path) → {Promise.<Object>}

Parameters:
Name Type Description
path String
Source:
Returns:
Type
Promise.<Object>

(inner) loadYAMLSync(path) → {Object}

Parameters:
Name Type Description
path String
Source:
Returns:
Type
Object

(inner) localeCompareFn(locales) → {localCompare}

Get a function to compare strings, with locale support if Intl is available.

Parameters:
Name Type Description
locales String
Source:
See:
Returns:
Type
localCompare

(inner) localeCompareSimple(a, b) → {Number}

Compare strings.

Parameters:
Name Type Description
a String
b String
Source:
Returns:
Type
Number

(inner) matchFiles(pattern, optsopt) → {Promise.<Array.<String>>}

A Promised glob.

Parameters:
Name Type Attributes Description
pattern String
opts Objects <optional>

Optional match opts.

Properties
Name Type Attributes Default Description
workDir String <optional>
.

Working dir for this match.

ignoreDir Boolean <optional>
true

Ignore directories or not.

ignoreHidden Boolean <optional>
true

Ignore hidden files or not.

recursive Boolean <optional>
true

Set to false if you don't care about subdirs to get better performance.

Source:
Returns:
Type
Promise.<Array.<String>>

(inner) nodesEach(node, callback)

Recursively Pre-Order Traversal of parse5 Node.

Parameters:
Name Type Description
node Object

Root parse5 Node of a tree.

callback traversalCallback
Source:
See:

(inner) nodesFilter(node, callback) → {Array.<Object>}

Recursively Pre-Order Traversal of parse5 Node.

Parameters:
Name Type Description
node Object

Root parse5 Node of a tree.

callback filterCallback
Source:
See:
Returns:

An Array of filtered parse5 Nodes.

Type
Array.<Object>

(inner) paginate(p, posts, perPageopt) → {Array.<File>}

Paginate page's posts.

Parameters:
Name Type Attributes Default Description
p File

Original page.

posts Array.<File>

Page related posts.

perPage Number <optional>
10

How many posts per page.

Source:
Returns:

Paginated pages, original page's index is 0.

Type
Array.<File>

(inner) paginateCategoriesPosts(categories, parentPath, docDir, perPageopt) → {Array.<File>}

Generate pages and paginate posts of categories.

Parameters:
Name Type Attributes Default Description
categories Array.<Category>
parentPath String

Parent category's dir.

docDir docDir
perPage Number <optional>
10

How many posts per page.

Source:
Returns:

All category and it's subs pages.

Type
Array.<File>

(inner) parseFrontMatter(file) → {File}

Parse front-matter and set properties to file.

Parameters:
Name Type Description
file File
Source:
Returns:
Type
File

(inner) parseNode(nodeopt, html, optionsopt) → {Object}

Parse HTML string into parse5 Node.

Parameters:
Name Type Attributes Description
node Object <optional>

If specified, given fragment will be parsed as it was set to the context element's innerHTML property.

html String

HTML string to parse.

options Object <optional>

parse5 options.

Source:
See:
Returns:
Type
Object

(inner) putSite(site, key, file)

Put file into an array in site, will replace file with the same destination.

Parameters:
Name Type Description
site Site
key String
file File
Source:

(inner) removeControlChars(str) → {String}

Remove XML control chars.

Parameters:
Name Type Description
str String
Source:
Returns:

XML string.

Type
String

(inner) removeHTMLTags(str) → {String}

Remove all HTML tags.

Parameters:
Name Type Description
str String
Source:
Returns:

String without HTML tags.

Type
String

(inner) replaceNode(node, html)

Quick and not so dirty way to replace a Node with given HTML string.

Parameters:
Name Type Description
node Object

parse5 Node to replace.

html String
Source:

(inner) resolveAnchors(node, baseURL, rootDir, docPath, optsopt)

Update site's internal link to absolute path, and add attributes for external link.

Parameters:
Name Type Attributes Description
node Object

parse5 Node.

baseURL String

Site baseURL.

rootDir String

Site rootDir.

docPath String
opts Object <optional>
Source:

(inner) resolveCodeBlocks(node, optsopt)

Update code blocks.

Parameters:
Name Type Attributes Description
node Object

parse5 Node.

opts Object <optional>
Properties
Name Type Attributes Default Description
lineNumbers Boolean <optional>
false

Generate line numbers.

Source:

(inner) resolveHeadingIDs(node, optsopt)

Update headings' IDs for bootstrap scrollspy.

Parameters:
Name Type Attributes Description
node Object

parse5 Node.

opts Object <optional>
Properties
Name Type Attributes Default Description
safeChar String <optional>
"-"

Char used to replace unsupported chars.

Source:

(inner) resolveImages(node, rootDir, docPath, optsopt)

Update site's internal image src to absolute path.

Parameters:
Name Type Attributes Description
node Object

parse5 Node.

rootDir String

Site rootDir.

docPath String
opts Object <optional>
Source:

(inner) serializeNode(node, optionsopt) → {String}

Serialize parse5 Node into HTML string.

Parameters:
Name Type Attributes Description
node Object

parse5 Node to serialize.

options Object <optional>

parse5 options.

Source:
See:
Returns:
Type
String

(inner) setNodeAttr(node, attrName, attrValue)

Set an attribute value to parse5 Node.

Parameters:
Name Type Description
node Object

parse5 Node.

attrName String
attrValue String
Source:

(inner) setNodeText(node, html)

Set text content (or innerHTML) of a parse5 Node.

Parameters:
Name Type Description
node Object

parse5 Node.

html String
Source:

Type Definitions

CategoriesData

Type:
  • Object
Properties:
Name Type Description
categories Array.<Category>
categoriesLength Number
Source:

FrontMatter

Type:
  • Object
Properties:
Name Type Description
attributes Object

Parsed front-matter properties.

body String

String after front-matter.

frontMatter String

Front-matter string.

Source:

TagsData

Type:
  • Object
Properties:
Name Type Description
tags Array.<Tag>
tagsLength Number
Source:

compareCallback(a, b) → {Number}

Comparing function accepted by Array.sort().

Parameters:
Name Type Description
a *
b *
Source:
Returns:
Type
Number

filterCallback(node) → {Boolean}

Parameters:
Name Type Description
node Object

parse5 Node.

Source:
Returns:

True to collect a node into an Array.

Type
Boolean

formatDateTime(dtopt) → {String}

Format date and time, with locale support if Intl is available.

Parameters:
Name Type Attributes Default Description
dt * <optional>
new Date()
Source:
Returns:
Type
String

getPath(docPathopt, skipEncodeopt) → {String}

Get full website path starts from / after domain. This function can only handle site internal path.

Parameters:
Name Type Attributes Default Description
docPath String <optional>
skipEncode Boolean <optional>
false

If true, skip encodeURI().

Source:
Returns:

Full path that starts with site rootDir.

Type
String

getURL(docPathopt) → {URL}

Get full website URL including the domain. This function can only handle site internal path.

Parameters:
Name Type Attributes Description
docPath String <optional>
Source:
Returns:

Full website URL.

Type
URL

isCurrentHost(testURLopt) → {Boolean}

Test if given URL is on current host. This function does not care about query string and hash.

Parameters:
Name Type Attributes Description
testURL String <optional>

URL needed to test.

Source:
Returns:
Type
Boolean

isCurrentPath(testPathopt, strictopt) → {Boolean}

Test if given path is current path. This function does not care about query string and hash. This function can only handle site internal path.

Parameters:
Name Type Attributes Default Description
testPath String <optional>

Path needed to test.

strict Boolean <optional>
false

If not strict, true is also returned if given path is parent path of current path.

Source:
Returns:
Type
Boolean

localeCompare(a, b) → {Number}

Compare strings, with locale support if Intl is available.

Parameters:
Name Type Description
a String
b String
Source:
Returns:
Type
Number

traversalCallback(node)

Parameters:
Name Type Description
node Object

parse5 Node.

Source: