Skip to content

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

License

Notifications You must be signed in to change notification settings

dailyrandomphoto/hexo-unique-post-path

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

type length character set options default description
cuid (default) 25 a-z0-9, start with c use cuid() generated string.
e.g. ck2bi7fxf00013ryng5jr1rer
cuid-slug 7-10 a-z0-9 use cuid.slug() generated string.
e.g. xh23npi
nanoid 21 A-Za-z0-9_- size 21 use nanoid() generated string.
e.g. EwUTt2eoka-oEV5kf-o0O
nanoid-simple 24 a-z0-9 size 24 use nanoid/generate generated string.
e.g. pfldm3gg8h9psydphotqe71d
nanoid-lowercase 26 a-z size 26 use nanoid/generate generated string.
e.g. jsjxoibprplrdoitjmppotjrnm
seq 1~ 0-9 size
start
1
1
1, 2, 3,...
001, 002, 003,...
prefix-seq 1~ A-Za-z0-9_- size
start
prefix
1
1
<none>
items-1, items-2, items-3,...
items-001, items-002, items-003,...
date-seq 1~ A-Za-z0-9_- size
start
prefix (*)
2
1
YYYYMMDD
2019102901, 2019102902, 2019103001, ...
2019-10-29-001, 2019-10-29-002, 2019-10-30-001,...
latin 1~ A-Za-z0-9_- separator
lowercase
-
true
the argument title is required.
"你好, World!" => ni-hao-world
"안녕하세요, 세계" => annyeonghaseyo-segye
"Привет, мир!" => privet-mir

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published