1.0.4 • Published 4 years ago

hexo-unique-post-path v1.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

hexo-unique-post-path

NPM Version LICENSE Build Status Coverage Status dependencies Status devDependencies Status

This plug-in helps Hexo create new posts with unique auto-generated paths.

Just like this.

$ hexo new2

INFO  Created: ./source/_posts/ck20kqmij0001ieyn4es62xh7.md

Installation

npm install --save hexo-unique-post-path

Usages

There are tree ways to use this plug-in to create new posts with unique auto-generated paths.

1. Use hexo new2 command

hexo new2 [layout] [title]

e.g.

$ hexo new2 "Hello World!"

INFO  Created: ./source/_posts/ck2c0zo1y0001gyyn5c2ma96m.md

$ hexo new2

INFO  Created: ./source/_posts/ck20kqmij0001ieyn4es62xh7.md

The title argument can be omitted and its default value is ' '.

You can define the default value of title in the _config.yml file.

#_cofig.yml

+ unique_post_path:
+   title_default: "new post"

You can define the path generating algorithm in the _config.yml file. e.g.

#_cofig.yml

+ unique_post_path:
+   type: nanoid
+   size: 20

or

#_cofig.yml

+ unique_post_path:
+   type: date-seq
+   size: 2
+   prefix: YYYYMMDD

Available type:

typelengthcharacter setoptionsdefaultdescription
cuid (default)25a-z0-9, start with cuse cuid() generated string. e.g. ck2bi7fxf00013ryng5jr1rer
cuid-slug7-10a-z0-9use cuid.slug() generated string. e.g. xh23npi
nanoid21A-Za-z0-9_-size21use nanoid() generated string. e.g. EwUTt2eoka-oEV5kf-o0O
nanoid-simple24a-z0-9size24use nanoid/generate generated string. e.g. pfldm3gg8h9psydphotqe71d
nanoid-lowercase26a-zsize26use nanoid/generate generated string. e.g. jsjxoibprplrdoitjmppotjrnm
seq1~0-9sizestart111, 2, 3,...001, 002, 003,...
prefix-seq1~A-Za-z0-9_-sizestartprefix11<none>items-1, items-2, items-3,...items-001, items-002, items-003,...
date-seq1~A-Za-z0-9_-sizestartprefix (*)21YYYYMMDD2019102901, 2019102902, 2019103001, ...2019-10-29-001, 2019-10-29-002, 2019-10-30-001,...
latin1~A-Za-z0-9_-separatorlowercase-truethe argument title is required. "你好, World!" => ni-hao-world"안녕하세요, 세계" => annyeonghaseyo-segye

Sample of valid prefix option for date-seq:

YYYYMMDD (default)
YYYY-MM-DD-
YYMMDD-
YYYYMM
YYYY

You can add your own path generating algorithm by define Custom functions.

If the layout is page, or if --path, -p or --slug, -s option is provided, hexo new2 works same as hexo new.

2. Use hexo new command with unique_post_path configuration

Add unique_post_path options to the _config.yml file.

#_cofig.yml

+ unique_post_path:
+   auto: true

Then use new command as before.

$ hexo new "My New Post"

INFO  Created: ./source/_posts/ck20kqmij0001ieyn4es62xh7.md

3. Use hexo new command with --type option

$ hexo new "Hello World!" --type=cuid

INFO  Created: ./source/_posts/ck2c0zo1y0001gyyn5c2ma96m.md

It also works on hexo new2 too.

$ hexo new2 "Hello World!" --type=cuid

INFO  Created: ./source/_posts/ck2c0zo1y0001gyyn5c2ma96m.md

$ hexo new2 --type=cuid

INFO  Created: ./source/_posts/ck20kqmij0001ieyn4es62xh7.md

Define Custom Functions

Add a script file into the scripts folder of your hexo base directory. e.g. scripts/my_custom_path.js

Then register a generator function. The generator function should return a function that returns a string. e.g.

const { register } = require('hexo-unique-post-path');

register('my_custom_path', function(option) {
  let size = option.size || 8;
  let prefix = option.prefix || 'items-';
  return function(title) {
    return prefix + title.toLowerCase().replace(/[^\w]/g, '').substring(0, size);
  };
});
#_cofig.yml

+ unique_post_path:
+   type: my_custom_path
+   prefix: articles-
$ hexo new2 "Hello World!"
=> articles-hellowor.md

Related

  • id-generators - API for this module.
  • Awesome Unique ID - A curated list of awesome Unique ID libraries and resources.
  • transliteration - Universal Unicode to Latin transliteration + slugify module. Works on all platforms and with all major languages.
  • hexo-abbrlink - A Hexo plugin to generate static post link based on post titles.
  • hexo-permalink-pinyin - A Hexo plugin which convert Chinese title to transliterate permalink.

License

Copyright (c) 2019 dailyrandomphoto. Licensed under the MIT license.

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

5 years ago