0

我正在使用 Hugo 短代码向链接添加一个类。我们想复制 Wikipedia 的“红色链接”样式,即尚未构建但已计划好的页面。

简码 red-link.html 如下所示:

<a href="{{ .Get "url" }}" class="red-link">{{ with .Get "title" }}{{.}}{{else}}{{.Get "url"}}{{end}}</a>

当我在 markdown 文件中使用它时,它看起来像这样:

Here is an {{< red-link title="example red link" url="#" >}}.

想要的是这个(链接文本和标点符号之间没有空格):

<p>Here is an <a href="#" class="red-link">example red link</a>.</p>

我得到的是:

<p>
    "Here is an "
    <a href="#" class="red-link">example red link</a>
    " ."
</p>

最终看起来像这样:

我无法修复的愚蠢错误的屏幕截图

我需要更改什么来删除链接末尾和句点之间的多余空间?

4

2 回答 2

1

我有同样的问题,但该项目被配置为在所有文件的末尾添加一个新行。我没有重新配置项目以允许最后没有新行的文件,而是找到了一个丑陋但有效的解决方案。

以catertot 为例,您可能有以下shotcode(注意结尾的新行):

<a href="{{ .Get "url" }}" class="red-link">{{ blabla }}</a>

解决方案是在短代码的最后添加一个{{- "" -}}

<a href="{{ .Get "url" }}" class="red-link">{{ blabla }}</a>{{- "" -}}

这告诉 Hugo 去除链接本身后面的任何空格,从而产生一个没有空格的完美内联短代码。

于 2022-01-14T12:29:37.850 回答
0

在您的简码文件中,删除以下所有内容,包括所有空行:

<a href="{{ .Get "url" }}" class="red-link">{{ with .Get "title" }}{{.}}{{else}}{{.Get "url"}}{{end}}</a>

如果这不能消除该空间,请尝试使用适当 的 Go Template 修剪空白分隔符{{- -}})。例如,尝试将上面的内容更改为:

<a href="{{ .Get "url" -}}" class="red-link">{{ with .Get "title" -}}{{. -}}{{else -}}{{.Get "url" -}}{{end -}}</a>

我的简码有这个问题,我在https://www.ii.com/hugo-tips-fragments/#_8_years_sinceyears-since上写过。

于 2021-09-08T16:25:41.820 回答