Setup Hexo and Typora local images with github actions
Just deployed hexo on github pages. Never used hexo before and one thing annoying me is the images in markdown.
On typora I have following settings:
This works well as typora will copy images to the relative path...and of coz it's not working with hexo.
I'm new to hexo, from my (re)search, the official way is to set post_asset_folder: true
, but the default image path is ./${filename}
not my current ./imgs.md/${filename}
, and, it's dumb to create an empty folder for blog post without images...
Solution
Some quick search shows hexo-asset-image
plugin...well, it's kinda abandoned and has bug with github.io
-- or to be exact, the permanent link url pattern.
Here is the original idea for the .io
domain fix. Mine is slightly different as a quick dirty fix:
DON"T ENABLE
post_asset_folder
, i.e., keep it asfalse
in_config.yml
install the plugin
npm install hexo-asset-image --save
Modify
node_modules/hexo-asset-image/index.js
:
let it run regardlesspost_asset_folder
settings and setvar link = 'pics/imgs.md/'
Another minor bug. Find:
|| /^\s*\/uploads|images\//.test(src))) {
change to
|| /^\s*\/(uploads|images)\//.test(src))) {
This regex is used to keep
/images/
/uploads/
unchanged, however without parentheses it will also match things like/path/to/some-images/images.jpg
behind the scenes...
Now, with hexo new test
, hexo will only create source/_post/test.md
but no additional image folder, and after I insert 1.jpg
, typora puts it in source/_posts/imgs.md/test/1.jpg
, and referred as relative path ./imgs.md/test/1.jpg
.
After hexo generate
with patched hexo-asset-image
, the compiled html will look for/pics/imgs.md/test/1.jpg
, which, in our local path, is public/pics/imgs.md/test/1.jpg
.
With post_asset_folder
disabled, the whole imgs.md
folder is ignored during hexo g
, so we have to copy it to the public
folder afterwards and before final deploy.
Put everything together with github action
old workflow
I use github actions to deploy the blog to github pages. Before the change, it goes like:
1 | cd "${GITHUB_WORKSPACE}" |
new workflow:
In blog folder, mkdir script
, put modified index.js
as hexo-asset-image-index.js
in it and create a fix-hexo-image.sh
as below.
1 |
|
The updated workflow:
patch hexo-asset-image
, followed by hexo clean
, hexo generate
. Copy imgs.md
folder to public/
and finally hexo deploy
.
1 | cd "${GITHUB_WORKSPACE}" |
My full modified hexo-asset-image/index.js
for reference, save it as hexo-asset-image-index.js
and put in script
folder.
1 | ; |