Giter VIP home page Giter VIP logo

hexo-unique-post-path's Introduction

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.

hexo-unique-post-path's People

Contributors

dailyrandomphoto avatar

Stargazers

 avatar

Watchers

 avatar

hexo-unique-post-path's Issues

NPM install is broken on latest Hexo

When trying to install this package on [email protected]:

PS C:\Growl\FluffyMufflers\CheeseIts> npm i -S [email protected]

> [email protected] preinstall C:\Growl\FluffyMufflers\CheeseIts\node_modules\id-generators

npm WARN [email protected] requires a peer of nanoid-good@^1.3.0 but none is installed. You must install peer dependencies yourself.
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 255
npm ERR! [email protected] preinstall: `unlink-self 2>&1 | echo unlink-self`
npm ERR! Exit status 255
npm ERR!
npm ERR! Failed at the [email protected] preinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output abov

Probably requires upgrade.

feat: use transliteration module to generate path

Use transliteration module to generate path.

transliteration usages:

slugify('你好, world!');
// ni-hao-world

hexo-unique-post-path Usage:

1. use hexo new2

#_cofig.yml

+ unique_post_path:
+   type: latin
$ hexo new2 "你好, world!"
=> ni-hao-world.md

2. use hexo new

#_cofig.yml

+ unique_post_path:
+   type: latin
+   auto: true
$ hexo new "你好, world!"
=> ni-hao-world.md

3. use -- type option

$ hexo new "你好, world!" --type=latin
=> ni-hao-world.md

# or 

$ hexo new2 "你好, world!" --type=latin
=> ni-hao-world.md

feat: set as a custom path in from matter

Like hexo-abbrlink,
set as a custom path in from matter.

e.g.

#source/_posts/hello-world.md

title: "Hello World"
custom_slug: ck20kqmij0001ieyn4es62xh7
...
#_cofig.yml

permalink: posts/:custom_slug/
permalink_defaults:
    custom_slug: :title

feat: support id + title and title + id

For example:

# _cofig.yml

+ unique_post_path:
+   type: date-seq-title
hexo new2 "Hello World"
=> 2019121001-hello-world.md
# _cofig.yml

+ unique_post_path:
+   type: title-cuid-slug
hexo new2 "Hello World"
=> hello-world-xh23npi.md

Use hexo-unique-post-path to transliterate post filename and URL to English

Related Issues:

hexojs/hexo#506
hexojs/hexo#1479
ppoffice/hexo-theme-icarus#315
hexojs/hexo-util#17

Solution:

Install hexo-unique-post-path plugin

npm i hexo-unique-post-path

Usage:

1. use -- type option

$ hexo new "你好, world!" --type=latin
=> ni-hao-world.md

$ hexo new "안녕하세요, 세계!" --type=latin
=> annyeonghaseyo-segye.md

$ hexo new "Привет, мир!" --type=latin
=> privet-mir.md

2. use hexo new, without option

#_cofig.yml

+ unique_post_path:
+   type: latin
+   auto: true
$ hexo new "你好, world!"
=> ni-hao-world.md

$ hexo new "안녕하세요, 세계!"
=> annyeonghaseyo-segye.md

$ hexo new "Привет, мир!"
=> privet-mir.md

3. use hexo new2

#_cofig.yml

+ unique_post_path:
+   type: latin
$ hexo new2 "你好, world!"
=> ni-hao-world.md

$ hexo new2 "안녕하세요, 세계!"
=> annyeonghaseyo-segye.md

$ hexo new2 "Привет, мир!"
=> privet-mir.md

Related: #9

feat: Users can provide a custom function for path generation.

Related Issue: #1

Users can provide a custom function for path generation.
e.g.

const unique_post_path = require('hexo-unique-post-path');

unique_post_path.register('my_path', function(title) {
  return title.toLowerCase().replace(/[^\w]/g, '');
});
#_cofig.yml

unique_post_path:
  type: my_path
$ hexo new2 "Hello World!"
=> helloworld.md

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.