After modifying the url to XXX.1.html, record how to autofill increment id when new posts are created.
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.
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.
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.
The solutin is above.
The main file is index.js, the register event code is:
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:
const fs = require('fs-extra')
Attention Needs to new a file
themes/icarus/scripts/id, and set the content to your current system’s latest id value.
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.