hexo-generator-json-content
Hexo (https://hexo.io/) plugin to generate a JSON file for generic use or consumption with the contents of posts and pages.
It's useful to serve compact and agile content data for microservices like AJAX site search, Twitter typeahead or public API.
News
To know new features and bugfixes, please visit releases index.
Installation
npm i -S hexo-generator-json-content
Usage
Hexo will run the generator automagically when you run hexo serve
or hexo generate
. 😏
Using the default settings, the content.json
file looks like the following structure:
meta: title: hexoconfigtitle subtitle: hexoconfigsubtitle description: hexoconfigdescription author: hexoconfigauthor url: hexoconfigurlpages: //-> all pages title: pagetitle slug: pageslug date: pagedate updated: pageupdated comments: pagecomments permalink: pagepermalink path: pagepath excerpt: pageexcerpt //-> only text ;) keywords: null //-> it needs settings text: pagecontent //-> only text minified ;) raw: pageraw //-> original MD content content: pagecontent //-> final HTML content author: pageauthor categories: name: categoryname slug: categoryslug permalink: categorypermalink tags: name: tagname slug: tagslug permalink: tagpermalink posts: //-> only published posts title: posttitle slug: postslug date: postdate updated: postupdated comments: postcomments permalink: postpermalink path: postpath excerpt: postexcerpt //-> only text minified ;) description: postdescription //-> only text minified ;) keywords: null //-> it needs settings text: postcontent //-> only text minified ;) raw: postraw //-> original MD content content: postcontent //-> final HTML content author: postauthor categories: name: categoryname slug: categoryslug permalink: categorypermalink tags: name: tagname slug: tagslug permalink: tagpermalink categories: //-> Posts categories index ;) name: categoryname slug: categoryslug permalink: categorypermalinktags: //-> Posts tags index ;) name: tagname slug: tagslug permalink: tagpermalink
hexo.util.stripHTML
is used to get only clean text for excerpt
and text
fields.
Settings
You can customize settings in _config.yml
.
Defaults
Default settings are:
jsonContent: meta: true dafts: false file: content.json keywords: undefined dateFormat: undefined pages: title: true slug: true date: true updated: true comments: true path: true link: true permalink: true excerpt: true keywords: false text: true raw: false content: false author: true posts: title: true slug: true date: true updated: true comments: true path: true link: true permalink: true excerpt: true keywords: false text: true raw: false content: false author: true categories: true tags: true
Keywords
keywords
options extracts keywords from excerpt.
Setting the root keywords
option will automatically reflect to pages.keywords
and posts.keywords
. But you can ignore one by setting it to false
explicitly.
It is powered by michaeldelorenzo/keyword-extractor, NPM package to create a keywords array from a string by removing stopwords.
So, the setting value should be a valid language from its options parameters.
# exemple jsonContent: meta: true keywords: french
If it don't support your language, no worry! It's disabled by default.
Date formats
dateFormat
option sets an output format for datetime objects date
and updated
.
It is powered by moment, so any string accepted by format method can be used.
# exemple jsonContent: meta: true dateFormat: DD/MM/YYYY
If not defined, default format is the JSON.stringify
result for Date
objects.
Output
Sections
By default, the JSON output includes meta
, pages
and posts
sections. If only one of these sections is enabled by config, the json output will consist of a single array.
For example, the following config enables only posts
, showing title, date, path, and text fields:
# exemple jsonContent: meta: false pages: false posts: title: true date: true path: true text: true raw: false content: false slug: false updated: false comments: false link: false permalink: false excerpt: false categories: false tags: false author: false
The result JSON will look like this:
//-> only published posts title: posttitle date: postdate text: postcontent //-> only text minified ;) path: postpath
Excluding fields
You can exclude meta
, pages
or posts
contents from output JSON by setting meta
, pages
, or posts
to false
.
To exclude individual fields from pages
or posts
output, set its config values to false
.
Drafts
By default, drafts are automatically skipped from indexing.
If you want to include drafts, set drafts: true
:
# exemple jsonContent: drafts: true
Skip indexing
Any post
or page
protected with password will be automatically skipped from indexing.
You can also exclude a specific post
or page
by setting hidden: true
on front-matter.
To exclude specific paths or tags, use ignore
lists. Any path or tag which contains at least one of the listed substrings will be skipped from indexing. For example:
# exemple jsonContent: ignore: paths: - path/to/a/page - url/to/one/post - an-entire-category - specific.file - .ext # a file extension tags: - tag1 - tag2
Also, you can set hidden: false
to override all the rules mentioned above.
Custom file name
By default, the output file is content.json
, but is possible to customize the file name:
# exemple jsonContent: file: custom-file-name.json