简单装修2.0

上次 还说不会有装修 2.0 了吧,结果啪啪打脸。近期的一些变动。

Umami

写了简单的 设置流程

Umami 很简洁但有两个问题,一是似乎 Umami 不会显示来自中国的访问流量,GA 里排名第一的地区是中国,可 Umami 里完全没出现过。二是 Umami 没办法有效排除我自己的访问数据,但这是我的原因,因为走代理并且用了 WebRTC Leak Prevent 插件浏览器无法获取真实 IP,Umami 的设置中自然也无法过滤。但算了就这样吧。

Homepage

我的博客主题首页默认显示所有博文,数量不多时还好,但现在累积了数百篇首页就变得相当累赘。

index.html 中原先的模块删掉,利用 Hugo 的 range 语句生成最近 5 篇文章的列表,首页就清爽多啦。

      {{ range first 5 (sort site.RegularPages "Date" "desc") }}
      <div style="position: relative;">
        <ol style="margin-top: 15; margin-bottom: 15;">
          <li class=" no-underline rounded-md hover:bg-white transition duration-300 ease-in-out"
            style="margin-top: 5; margin-bottom: 5;">
            <a href="{{ .RelPermalink }}" class="tracking-tight no-underline text-blue-600  mb-0">
              {{ .Title }}
            </a>
          </li>
        </ol>
      </div>
      {{ end }}

不想写中文了直接复制和 ChatGPT 的对话……

Use custom render hook to convert plain text links (e.g., [title](plaintext)) and excludes real URLs (e.g., [link](https://xxxxx)).

Create a file at layouts/_default/_markup/render-link.html.

{{ $destination := .Destination }}
{{ if not (or (strings.HasPrefix $destination "http") (strings.HasPrefix $destination "https") (strings.HasPrefix $destination "//")) }}
  {{ $destination = (print "/posts/" $destination | urlize) }}
{{ end }}
<a href="{{ $destination }}">{{ .Text }}</a>

Then we can use the simple [title](plaintext) syntax for plain text links and normal syntax for real URLs in markdown files.

在 Obsidian 中用 markdown 的 [title](filename) 替代原本双链 [[filename]] 格式,这样既能保证我在 Obsidian 里跳转的是本地文件,用 Hugo 渲染完成后博客又能正常访问对应博文链接。

更改链接打开方式,将默认的当前页面跳转改为新标签页打开。

baseof.html 中加入以下内容。

  <script>
    document.querySelectorAll('a').forEach(function (link) {
      link.setAttribute('target', '_blank');
      link.setAttribute('rel', 'noopener noreferrer');
    });
  </script>

2024.08.27 更新:

发现上面的方式会把本站链接也默认在新标签页打开,这样会显得有些烦人,所以更改了一下逻辑,删除了 script,把上一步的 layouts/_default/_markup/render-link.html 改为如下内容。这样仅在新标签页打开外站链接,本站还是基于当前页面跳转。

{{ $destination := .Destination }}
{{ if not (or (strings.HasPrefix $destination "http") (strings.HasPrefix $destination "https") (strings.HasPrefix $destination "//")) }}
  {{ $destination = (print "/posts/" $destination | urlize) }}
{{ end }}
<a href="{{ $destination }}" {{ with .Title }}title="{{ . }}"{{ end }}{{ if strings.HasPrefix $destination "http" }}target="_blank" rel="noreferrer noopener"{{ end }}>{{ .Text | safeHTML }}</a>

Comments