Hexo Autofill Increment Id

After modifying the url to XXX.1.html, record how to autofill increment id when new posts are created.

CN doc

1、Overview

When I was build my blog, I modified the URL to XXX.1.html for beauty, the detailed operation is change posts view url. So I need type the newest id for every new post.

It’s difficult for me to remind the latest id, I always open the front page and see the latest post to see the latest id, and set the new post’s id to the latest id + 1.

It’s very cumbersome and useless. I was thinking about how to autofill increment id when the new post is created.

2、Solution

The solution is very simple, new a file to store the latest id.

Execute a new script when a post is created.

The script is simple too, first of all, to get the latest id from the file, and change the new post’s id to latest +1, last write the latest id +1 in the id file.

And there is a problem is I was new at js, I have no idea about how to trigger a script when the new post is created, and how to read & format the new post.

Fortunately, I found a plugin from Google that can increment id. I can know how to register for an event and how to format a post.

3、Test plugin

The plugin named hexo-incremental-id, the Github page is here).

Is a complete plugin from the npm repo. The plugin will scan all your markdown files under the sources folder every time you create a new post, and add an incremental id to all pages.

I have accured some problems when I tested, there is a performance problem when the number of pages is large which the document said. And some old page files were modified because the MD params are invalid.

There is a little trouble for me, I will re-install this every time I want to change the writing environment because this is a plugin.

I have some re-coding for this plugin because of these problems. Thank you KagamiChan for giving me ideas like registering the event and formatting the post.

4、Re-coding

The solutin is above.

The main file is index.js, the register event code is:

1
hexo.on('new', setId)

When the post is created and trigged setId method, use plugin hexo-front-matter to format the post.

We only need to re-coding the setId method, the code is very to understand:

The file path is: themes/icarus/scripts/incremental.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const fs = require('fs-extra')
const path = require('path')
const fm = require('hexo-front-matter')

const setId = (post) => {
// change here to your id file path
let idFile = path.join(process.cwd(), 'themes/icarus/scripts/id')
// current latest id is file content + 1
let maxId = parseInt(fs.readFileSync(idFile, "utf8")) + 1
// write back the current latest id
fs.writeFileSync(idFile, maxId)
// read new post's content, use hexo-front-matter to format post's params
let content = fm.parse(fs.readFileSync(post.path, "utf8"))
// set latest id to new post
content.id = maxId
// write back the new post content
fs.writeFileSync(post.path, fm.stringify(content))
}

// register event
hexo.on('new', setId)

Attention Needs to new a file themes/icarus/scripts/id, and set the content to your current system’s latest id value.

5、Testing & Summer

When exec hexo n “test”, a new post will be created with the latest id, and the id file value will +1, its success. We don’t need to remind the latest id anymore.


# Hexo

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×