Skip to content

cowsay-blog/hexo-nanoid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hexo-nanoid

Use ai/nanoid to generate abbreviated post links.

Build Status npm npm license Gitmoji

Features

  • Compatible with hexo new to generate ID for new posts.
  • ID validatation on existing posts to ensure the uniqueness.
  • Ability to autofix invalid and conflicted post IDs.
  • Event nanoid:generate is emitted after each ID generation.

Usage

Use the variable :nanoid in your permalink configuration. For example,

# _config.yml
permalink: post/:nanoid/

Configuration

The following list shows the descending precedence of config files. (The first one presented is used.)

  • Key nanoid in <hexo_root>/_config.yml
  • Key theme_config.nanoid in <hexo_root>/_config.yml
  • Key nanoid in <hexo_root>/themes/<theme_name>/_config.yml
  • Inline default config
interface HexoNanoIdOptions {
  /**
   * Autofix invalid or conflicted IDs.
   * @default false
   */
  autofix: boolean

  /**
   * Max try to find an unique ID.
   * @default 10
   */
  maxtry: number | "Infinity"

  /**
   * Ensure the ID uniqueness when creating a new post,
   * which somewhat slow down the new post generation
   * since all posts will be loaded and validated.
   * 
   * The uniqueness of the new post ID is not guaranteed if this config is set to `false`.
   * Check https://zelark.github.io/nano-id-cc/ for more info about collision probability.
   * @default true
   */
  check_on_new: boolean

  /***********************************/
  /*** Configuring nanoid library ***/
  /***********************************/

  /**
   * @default 21
   */
  length: number

  /**
   * @default "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-"
   */
  characters?: string
}

API

Event nanoid:generate is emitted to the Hexo instance with a parameter postId. You can listen to it as the following snippet.

hexo.on('nanoid:generate', (postId) => {
    console.log('New post ID is generated: %s.', postId)
})

About

Use ai/nanoid to generate abbreviated post links.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published