Published on

调整了下 Hugo 永久链接生成配置

Authors
  • avatar
    Name
    ttyS3
    Twitter

之前一直用的是 Hugo 默认的 permalink 配置.

content/post/linux/commandlinefu/how-to-start-tmux-as-systemd-user-service/index.md 将会生成
/post/linux/commandlinefu/how-to-start-tmux-as-systemd-user-service/ 的 URL path.

最近我越来越发现这样的链接过度复杂了,干脆直接调整成 /post/:filename/

于是 /post/linux/commandlinefu/how-to-start-tmux-as-systemd-user-service/ 变成了 /post/how-to-start-tmux-as-systemd-user-service/

这意味着,目录结构可能不单是我组织文件的方式,同时也被用来分类了 (父类/子类/文章slug)。

这给我带来了负担,导致我很久都没写博客。因为有时候想写点东西,又纠结,应该放在哪个分类下面。

因为有些东西是很难划分界线的,可能属于这个分类,但是你放在另一个分类里,也不是不可以。

从 URL 里去掉可以肉眼看出是分类的部分 path, 同时,彻底去掉分类这个东西。用 tags 来分类。 一个文章可以有很多 tags , 手动写几个 tags 来给文章分类即可。

想想以前用 WordPress 的时候,我甚至用过4级分类,现在想来,那时候真是疯了。

调整配置之后,page bunlde 的图片无法显示了。于是修复了下,就不细说了,直接贴一下我的 commit log.

commit 4ddd32088bba8486b2aa72a29c0ac0f540de9bd4
Author: 荒野無燈
Date:   Wed Jun 2 23:26:08 2021 +0800

    fix(url): fixup rel page bundle image resource url for permalinks config set to /post/:filename/
    
    for example:
    for url `http://localhost:1313/post/tmux-24bit-true-color-support/`
    
    the real page bundle path is `content/post/linux/apps/tmux-24bit-true-color-support`
    
    since our permalink config is set to `:filename`, so the real image for the page bundle
    
    will copied to `public/post/tmux-24bit-true-color-support`
    
    so the `RelPermalink` (directory created according to permalinks config) decided the image's directory.
    
    even if we changed the permalinks config to /post/:slug or other format, the code will works fine

diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html
index 26c4bc3..0c5036b 100644
--- a/layouts/_default/_markup/render-image.html
+++ b/layouts/_default/_markup/render-image.html
@@ -5,7 +5,7 @@
 {{/* process relative PATH URL in page bundle: no Scheme and not start with `/` */}}
 {{ if and (eq $theURL.Scheme "") (not (strings.HasPrefix .Destination "/")) }}
     {{ if eq "index.md" .Page.File.LogicalName }}
-        {{ $imgSrc = printf "%s/%s" .Page.File.Dir .Destination | absURL | safeURL }}
+        {{ $imgSrc = printf "%s/%s" .Page.RelPermalink .Destination | absURL | safeURL }}
     {{ end }}
 {{ end }}
 
diff --git a/layouts/_default/_markup/render-image.rss.xml b/layouts/_default/_markup/render-image.rss.xml
index 48846b9..3cffcd4 100644
--- a/layouts/_default/_markup/render-image.rss.xml
+++ b/layouts/_default/_markup/render-image.rss.xml
@@ -5,7 +5,7 @@
 {{/* process relative PATH URL in page bundle: no Scheme and not start with `/` */}}
 {{ if and (eq $theURL.Scheme "") (not (strings.HasPrefix .Destination "/")) }}
     {{ if eq "index.md" .Page.File.LogicalName }}
-        {{ $imgSrc = printf "%s/%s" .Page.File.Dir .Destination | absURL | safeURL }}
+        {{ $imgSrc = printf "%s/%s" .Page.RelPermalink .Destination | absURL | safeURL }}
     {{ end }}
 {{ end }}
 
diff --git a/layouts/_default/_markup/render-link.html b/layouts/_default/_markup/render-link.html
index a101673..ae92493 100644
--- a/layouts/_default/_markup/render-link.html
+++ b/layouts/_default/_markup/render-link.html
@@ -7,7 +7,7 @@
 {{/* process relative PATH URL in page bundle: no Scheme and not start with `/` */}}
 {{ if and (eq $theURL.Scheme "") (not (strings.HasPrefix .Destination "/")) }}
     {{ if eq "index.md" .Page.File.LogicalName }}
-        {{ $linkHref = printf "%s/%s" .Page.File.Dir .Destination | absURL | safeURL }}
+        {{ $linkHref = printf "%s/%s" .Page.RelPermalink .Destination | absURL | safeURL }}
     {{ end }}
 {{ end }}
 
diff --git a/layouts/_default/list.html b/layouts/_default/list.html
index cceef21..f2b89c0 100644
--- a/layouts/_default/list.html
+++ b/layouts/_default/list.html
@@ -41,7 +41,7 @@
 
     {{ if .Params.cover }}
       {{ if eq "index.md" .File.LogicalName }}
-        <img src="{{ printf "%s/%s" .File.Dir .Params.cover | absURL }}" class="post-cover" />
+        <img src="{{ printf "%s/%s" .RelPermalink .Params.cover | absURL }}" class="post-cover" />
       {{ else }}
         <img src="{{ .Params.cover | absURL }}" class="post-cover" />
       {{ end }}
diff --git a/layouts/_default/single.html b/layouts/_default/single.html
index a2ae357..72354bc 100644
--- a/layouts/_default/single.html
+++ b/layouts/_default/single.html
@@ -35,7 +35,7 @@
 
   {{ if .Params.cover }}
     {{ if eq "index.md" .File.LogicalName }}
-      <img src="{{ printf "%s/%s" .File.Dir .Params.cover | absURL }}" class="post-cover" />
+      <img src="{{ printf "%s/%s" .RelPermalink .Params.cover | absURL }}" class="post-cover" />
     {{ else }}
       <img src="{{ .Params.cover | absURL }}" class="post-cover" />
     {{ end }}