theme-shoka-x / hexo-theme-shokaX

a hexo theme based on shoka
https://docs-hexo.shokax.top
GNU Affero General Public License v3.0
288 stars 35 forks source link

links链接块中使用相对URL? #27

Closed symlPigeon closed 1 year ago

symlPigeon commented 1 year ago

问题发生位置

Hexo渲染报错

问题描述

使用links链接块时如果url参数包含了相对url,会出现以下报错

INFO  Validating config
INFO  Start processing
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Template render error: (unknown path)
  TypeError: Invalid URL
    at Object._prettifyError (/home/syml/Temp/website/node_modules/nunjucks/src/lib.js:36:11)
    at /home/syml/Temp/website/node_modules/nunjucks/src/environment.js:563:19
    at Template.root [as rootRenderFunc] (eval at _compile (/home/syml/Temp/website/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:44:3)
    at Template.render (/home/syml/Temp/website/node_modules/nunjucks/src/environment.js:552:10)
    at Environment.renderString (/home/syml/Temp/website/node_modules/nunjucks/src/environment.js:380:17)
    at /home/syml/Temp/website/node_modules/hexo/lib/extend/tag.js:238:16
    at tryCatcher (/home/syml/Temp/website/node_modules/bluebird/js/release/util.js:16:23)
    at Function.Promise.fromNode.Promise.fromCallback (/home/syml/Temp/website/node_modules/bluebird/js/release/promise.js:209:30)
    at Tag.render (/home/syml/Temp/website/node_modules/hexo/lib/extend/tag.js:237:20)
    at Object.onRenderEnd (/home/syml/Temp/website/node_modules/hexo/lib/hexo/post.js:426:22)
    at /home/syml/Temp/website/node_modules/hexo/lib/hexo/render.js:85:21
    at tryCatcher (/home/syml/Temp/website/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/syml/Temp/website/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/home/syml/Temp/website/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/home/syml/Temp/website/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/home/syml/Temp/website/node_modules/bluebird/js/release/promise.js:729:18)
    at _drainQueueStep (/home/syml/Temp/website/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/home/syml/Temp/website/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/home/syml/Temp/website/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.Async.drainQueues (/home/syml/Temp/website/node_modules/bluebird/js/release/async.js:15:14)
    at processImmediate (node:internal/timers:466:21)

应该是/scripts/tags/links.js第53行的urlparam = new URL(item.url)这边判断相对URL不合法导致的。用urlparam = new URL(item.url, hexo.config.url)替代能解决,不过我不确定会不会引入其他问题……

复现步骤

搭建站点,创建新post:

# test

> it's ok

{% links %}
- site: test
  owner: someone
  url: https://114514.com
  desc: some description
{% endlinks %}

> it causes error

{% links %}
- site: test title
  url: /test
  desc: test link
{% endlinks %}

> it causes error, too

{% links %}
- site: test title 2
  url: test2
  desc: test link
{% endlinks %}

运行hexo g出现上述报错。

相关配置

$ node --version
v16.19.0

$ hexo --version
INFO  Validating config
hexo: 6.3.0
hexo-cli: 4.3.0
os: linux 6.1.8-arch1-1 Arch Linux
node: 16.19.0
v8: 9.4.146.26-node.24
uv: 1.43.0
zlib: 1.2.11
brotli: 1.0.9
ares: 1.18.1
modules: 93
nghttp2: 1.47.0
napi: 8
llhttp: 6.0.10
openssl: 1.1.1s+quic
cldr: 41.0
icu: 71.1
tz: 2022f
unicode: 14.0
ngtcp2: 0.8.1
nghttp3: 0.7.0

_config.yml在hexo init生成的配置上增加了需要的条目,_config.shokax.yml使用了仓库中原本的配置。

注意事项

zkz098 commented 1 year ago

urlparam = new URL(item.url, hexo.config.url) 这种替代应该不会引发新问题,因为下文中有针对siteHost和urlparam.hostname相同的特判

symlPigeon commented 1 year ago

那后面更新会考虑修复吗?还是我提一个PR?

zkz098 commented 1 year ago

0.1.3已修复