hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
38.83k stars 4.75k forks source link

How to put post Markdown file into asset folder? #3245

Open AlynxZhou opened 5 years ago

AlynxZhou commented 5 years ago

Environment Info

Node version(node -v): v10.9.0 This doesn't matter.

Your site _config.yml (Optional): I will describe it later.

Your theme _config.yml (Optional): This doesn't matter.

Hexo and Plugin version(npm ls --depth 0):

hexo-site@0.0.0 /home/alynx/stackharbor.alynx.xyz
├── hexo@3.7.1
├── hexo-blog-encrypt@2.0.7
├── hexo-deployer-git@0.3.1
├── hexo-deployer-heroku@0.1.2
├── hexo-deployer-rsync@0.1.3
├── hexo-generator-archive@0.1.5
├── hexo-generator-category@0.1.3
├── hexo-generator-feed@1.2.2
├── hexo-generator-index@0.2.1
├── hexo-generator-search@2.2.5
├── hexo-generator-sitemap@1.2.0
├── hexo-generator-tag@0.2.0
├── hexo-neat@1.0.4
├── hexo-renderer-ejs@0.3.1
├── hexo-renderer-marked@0.3.2
├── hexo-renderer-njucks@1.0.0
├── hexo-renderer-stylus@0.3.3
└── hexo-server@0.2.2

For question

We all know that by default when Hexo create a post with asset folder, it creates a :title.md file and a :title/ folder in _posts/, and in generating your :title.md file will be moved to :title/index.html.

However, this is REALLY REALLY anti-human and anti-editor. Because Hexo changes route while generating (your post .md source path is NOT public path), all editor cannot find the image link or other asset link, and none of them can read Hexo's {% asset_img %} tag.

Normal config:

new_post_name: :title.md
post_asset_folder: true

Current Hexo post dir structure:

_posts/
    |
    - Some-Title.md
    |
    - Some-Title/
        |
        - Assets of Some-Title

If we can make it the same, I mean, the path in source dir will be the same in public dir, like your Markdown file will be put as :title/index.md and your asset folder is just the :title/, it will be better in all conditions. I think other staticf generator except Hexo uses this better way.

Better structure:

_posts/
      |
      - Some-Title/
          |
          - index.md (Markdown file of this post)
          |
          - Assets of Some-Title

I have tried change config to do this. First I set my config like this:

new_post_name: :title/index.md
post_asset_folder: true

But it causes this:

_posts/
      |
      - Some-Title/
          |
          - index.md (Markdown file of this post)
          |
          - index/ (Hexo make another silly asset folder!)
              |
              - Assets of Some-Title

And then I changed this:

post_asset_folder: false

It ignored asset file like this:

_posts/
      |
      - Some-Title/
          |
          - index.md (Markdown file of this post)
          |
          - Assets of Some-Title was IGNORED!!!

For feature request

I think there is a historical reason for Hexo to use such a bad way to deal it, and we should make it better. In face I think if we keep behavior right now but generate not only post Markdown files but also other files together with this Markdown file, we can make it better by just set existing config like:

new_post_name: :title/index.md
post_asset_folder: false

And get a structure like:

_posts/
      |
      - Some-Title/
          |
          - index.md (Markdown file of this post)
          |
          - DO NOT ignore Assets of Some-Title here

It will be better.

PS: I can use English to express my thought in most cases of coding, but a complex issue like this is hard to describe, so if there are developers speaking Chinese well we can communicate in Chinese and then translate into English to make it clearly. Thank you.

stevenjoezhang commented 12 months ago

You can try this plugin: hexo-img-locator

In _config.yml:

new_post_name: :title/index.md
post_asset_folder: false

For file structure like this:

_posts/
      |
      - Some-Title/
          |
          - index.md (Markdown file of this post)
          |
          - Assets of Some-Title, e.g. example.png

You can simply use ![](example.png) in index.md and it will be replaced by an absolute path automatically.

In fact, I believe this is simpler and more straightforward than the current asset management approach. Perhaps we should consider modifying the default behavior of asset management. What do you think? cc @hexojs/core

stevenjoezhang commented 12 months ago

We could make it the new default behavior, and also support the old post asset folder structure (for backward compatibility)

fuyufjh commented 11 months ago

You can try this plugin: hexo-img-locator

@stevenjoezhang Thanks for this plugin, but it doesn't work for me. In my case, the image's path was successfully changed but the target path didn't exist:

File structure:
_source
+ post-title
  + index.md
  + my-image.png

In Markdown:
[](my-image.png)

Generated path in HTML:
http://localhost:4000/post-title/my-image.png <-- 404 Not Found

Another troublesome issue is that the permanent link of the article becomes

http://localhost:4000/post-title/index/

While the original permanent link (without 'index/') become 404 page, which I is unacceptable to me.

stevenjoezhang commented 11 months ago

@fuyufjh Yes, the situation is more complicated than I thought. The PostAssets in Hexo need to be updated because currently these images are not recognized by Hexo and copied to the new location.

uiolee commented 2 months ago

I think this is a good approach. It can solve the conflict relative paths between editing and deployment.

bl-sun commented 1 month ago
_posts/
    |
    - Some-Title.md
    |
    - Some-Title/
        |
        - Assets of Some-Title

我认为上面这种结构不是问题。问题是在Some-Title.md中引用图片的方式。当前,![](my-image.png)这种引用方式对源文件编辑器是不友好的。我想应该解析![](Some-Title/my-image.png)这种图片或者别的静态内容的引用。因为这符合源代码的分布逻辑。总之感觉这触及到了hexo的生成过程和渲染过程。

_posts/
      |
      - Some-Title/
          |
          - index.md (Markdown file of this post)
          |
          - Assets of Some-Title

总之我觉得上面这两种文件的组织方式,可能各有所爱。我还是觉得上面一种文件的组织方式要好一点(可能更方便一些有图床的同学)。


我想到了一个简单解决办法可以将第一种文件组织方式。简单的渲染或生成为下面这种结构应该就可以解决问题了

Some-Title/
    |
    - index.html
    |
    - Some-Title/
        |
        - Assets of Some-Title