我在Hugo中使用了一个短代码来在.md页面上的链接中包装一个标题,这样我就可以在同一个页面上使用锚链接了。
短码将标题转换为小写,并将空格和其他字符转换为短划线。问题是,如果标题末尾有问号或其他字符,则短代码将在标题上留下尾随的"-“。如果结尾破折号-
存在,我如何剥离它?
短码链接-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>
在为锚点链接使用markdown时
## This is a String with a Trailing?
输出
<h2 id="this-is-a-heading-with-a-trailing">This is a heading with a Trailing?</h5>
问题出在短码输出末尾的-
。如果在上面短码link-heading.html
中使用replaceRE
,我如何去掉最后一个?
发布于 2021-05-31 09:46:09
https://stackoverflow.com/questions/67767057
复制相似问题