hexojs / hexo

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

Tag Plugins post_link generate a Post not found error #5042

Closed DNACore closed 1 year ago

DNACore commented 1 year ago

Check List

Please check followings before submitting a new issue.

Expected behavior

{% post_link somemarkdownfile %} should generate a corrent link to the exsit markdown file.

Actual behavior

it genrated a post not found xxx link.

How to reproduce?

just write the post_link such as {% post_link somemarkdownfile %}

what's more:

I created a new hexo folder, and created a new post named test.md, and wrote {% post_link test %} in hello-world.md. run hexo g, it can genrate the correnct link. and I move test.md to subfolder/test.md, then run hexo clean && hexo g. it can't genrate the correct link, just said: post not found. I moved the test.md just below the _post folder, then it can genrate the correct link again. BUT for my original hexo folder, wherever i put the somemarkdownfile.md, it always said post not found.

Is the problem still there under "Safe mode"?

YES

Environment & Settings

Node.js & npm version(node -v && npm -v)

v18.7.0
8.17.0

Your site _config.yml (Optional)

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

hexo-site@0.0.0
├── hexo-cli@4.3.0
├── hexo-deployer-git@3.0.0
├── hexo-generator-archive@1.0.0
├── hexo-generator-category@1.0.0
├── hexo-generator-index@2.0.0
├── hexo-generator-sitemap@3.0.1
├── hexo-generator-tag@1.0.0
├── hexo-renderer-ejs@2.0.0
├── hexo-renderer-marked@5.0.0
├── hexo-renderer-stylus@2.1.0
├── hexo-server@3.0.0
└── hexo@6.2.0

Your package.json package.json

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "hexo generate",
    "clean": "hexo clean",
    "deploy": "hexo deploy",
    "server": "hexo server"
  },
  "hexo": {
    "version": "6.2.0"
  },
  "dependencies": {
    "hexo": "^6.0.0",
    "hexo-cli": "^4.3.0",
    "hexo-deployer-git": "^3.0.0",
    "hexo-generator-archive": "^1.0.0",
    "hexo-generator-category": "^1.0.0",
    "hexo-generator-index": "^2.0.0",
    "hexo-generator-sitemap": "^3.0.1",
    "hexo-generator-tag": "^1.0.0",
    "hexo-renderer-ejs": "^2.0.0",
    "hexo-renderer-marked": "^5.0.0",
    "hexo-renderer-stylus": "^2.0.1",
    "hexo-server": "^3.0.0"
  }
}

Others

I replace the post_link.js in the node_moduls/hexo/lib bla bla with the current post_link.js on the master.

it throws the error below, hope it helps

Template render error: (unknown path)
  Error: Post not found: post_link somemarkdownfile.
    at Object._prettifyError (TheHexoFolder/node_modules/nunjucks/src/lib.js:36:11)
    at TheHexoFolder/node_modules/nunjucks/src/environment.js:563:19
    at Template.root [as rootRenderFunc] (eval at _compile (TheHexoFolder/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:19:3)
    at Template.render (TheHexoFolder/node_modules/nunjucks/src/environment.js:552:10)
    at Environment.renderString (TheHexoFolder/node_modules/nunjucks/src/environment.js:380:17)
    at TheHexoFolder/node_modules/hexo/lib/extend/tag.js:236:16
    at tryCatcher (TheHexoFolder/node_modules/bluebird/js/release/util.js:16:23)
    at Promise.fromNode.Promise.fromCallback (TheHexoFolder/node_modules/bluebird/js/release/promise.js:209:30)
    at Tag.render (TheHexoFolder/node_modules/hexo/lib/extend/tag.js:235:20)
    at Object.onRenderEnd (TheHexoFolder/node_modules/hexo/lib/hexo/post.js:426:22)
    at TheHexoFolder/node_modules/hexo/lib/hexo/render.js:85:21
    at tryCatcher (TheHexoFolder/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (TheHexoFolder/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (TheHexoFolder/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (TheHexoFolder/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (TheHexoFolder/node_modules/bluebird/js/release/promise.js:729:18)
lorezyra commented 1 year ago

I have this issue too... Seems this only works for posts that are in the /_post/ folder. If you place the post in any subdirectory, it will fail.

Obviously, need to see why this is failing at: https://github.com/hexojs/hexo/blob/740a0d6a94a8f3a096a5315ec3e8c45328994e93/lib/plugins/tag/post_link.js#L27-L30

https://github.com/hexojs/hexo/blob/740a0d6a94a8f3a096a5315ec3e8c45328994e93/lib/plugins/tag/index.js#L53-L70

D-Sketon commented 1 year ago

It seems that if you want to link to articles in _post/subfolder, you need to write like this {% post_link subfolder-test %}

stevenjoezhang commented 1 year ago

Yes, you need to specify the full path, for example

{% post_link '2017/10/Hello World' %}
stevenjoezhang commented 1 year ago

Posts with the same name may exist under different folders. If only the filename is provided without the full directory, then hexo cannot deduce which post to link to.

lorezyra commented 1 year ago

Posts with the same name may exist under different folders. If only the filename is provided without the full directory, then hexo cannot deduce which post to link to.

The Hexo db has the slug defined for every post. It should be a unique identifier in the front-matter. Requiring the full path makes this function completely pointless. Far easier to use the [Link text](url) markdown.

Furthermore, it adds overhead to the blog management when you move posts across folders. If you only need to provide the slug (without path) - as defined in documentation - then it’s trivial to reorganize your posts.

As for post names being duplicated in different folders, you can specify a slightly different slug in the front matter. This function doesn’t need to deduce which folder’s post to display, just use the first matching slug.

stevenjoezhang commented 1 year ago

You can't define the slug in front-matter. It will always be overwritten here: https://github.com/hexojs/hexo/blob/860f1bfb77ef8306b7d3a39a4ff8481544a15360/lib/plugins/processor/post.js#L99

(Maybe you can try modifying the relevant code)

This function calls url_for and is therefore able to handle cases where the blog root directory is changed.

Furthermore, slug contains the directory name by default: https://github.com/hexojs/hexo/issues/4837

lvhejin commented 1 year ago

相对地址、相对链接、内部链接、站内链接 {% post_link 'VsCode/使用vsCode配置Go开发环境' '使用vsCode配置Go开发环境' %}

DNACore commented 1 year ago

I have two articles.

_post/a/poo.md:
---
permalink: post/poo.html
---

_post/b/foo.md
---
permalink: post/foo.html
---

How can I link poo.md in foo.md ?

I have both tried {% post_link a/poo %} {% post_link post/poo %}

@stevenjoezhang @lorezyra

stevenjoezhang commented 1 year ago

{% post_link a/poo %} is working. Note that you will get an empty link if you don't set the title in front-matter.

DNACore commented 1 year ago

@stevenjoezhang

sorry for the late reply, I've tried that, but it wasnt ok.

here is my sample hexo project. It can reproduce this problem.

https://github.com/DNACore/HexoTest

stevenjoezhang commented 1 year ago

@DNACore First, you need to update to Hexo 7.0.0-rc1:

npm i hexo@7.0.0-rc1

Second, your post path is not a/poo, but a/poo_2023-05-31_19-12-35.

截屏2023-06-05 14 01 09

So, using {% post_link a/poo %} won't work. You can use title instead:

{% post_link poo %}
DNACore commented 1 year ago

@stevenjoezhang

After updating to hexo@7.0.0-rc1 and using title instead it worked.

While usding post path isn't work even I use the correct path.

Thank you!