0

我在 Hugo 中使用简码将标题包装在 .md 页面上的链接中,这样我就可以在同一页面上使用锚链接。

短代码将标题转换为小写,并将空格和其他字符转换为破折号。问题是,如果标题末尾有问号或其他字符,短代码将在标题上留下尾随的“-”。-如果存在破折号,我怎样才能去掉它?

简码 link-heading.html :

{{ $id := .Get 0 | lower | replaceRE "[^0-9a-z]" "-" | replaceRE "-+" "-" -}}
<a href="#{{ $id }}">
  <h2>{{ .Get 0 }}</h2>
</a>

.md 文件中的简码用法:

{{< link-heading "This is a String with a Trailing?" >}}

输出

<a href="#this-is-a-heading-with-a-trailing-">
<h2>This is a Heading with a Trailing?</h2>
</a>

在对锚链接使用降价时

## This is a String with a Trailing?

输出

<h2 id="this-is-a-heading-with-a-trailing">This is a heading with a Trailing?</h5>

问题是-在短代码输出的末尾。我怎样才能去掉最后一个 - 如果它存在replaceRElink-heading.html上面的简码中?

4

1 回答 1

0

我想我可能在这里遗漏了一些东西,但如果你只想抓住-字符串末尾的 或者如果它前面有 a?你可以这样做:

在此处输入图像描述

于 2021-05-31T01:46:09.313 回答