惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
F
Full Disclosure
B
Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
Lohrmann on Cybersecurity
月光博客
月光博客
I
Intezer
博客园 - 三生石上(FineUI控件)
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园_首页
P
Proofpoint News Feed
C
Check Point Blog
N
News | PayPal Newsroom
H
Heimdal Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
G
GRAHAM CLULEY
WordPress大学
WordPress大学
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
Recorded Future
Recorded Future
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
W
WeLiveSecurity
L
LINUX DO - 热门话题
Microsoft Azure Blog
Microsoft Azure Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
爱范儿
爱范儿
Martin Fowler
Martin Fowler
U
Unit 42
T
Troy Hunt's Blog
S
Securelist
V
V2EX
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
T
Tor Project blog
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News

longlin的个人小站

Conclusion | 2023 年终总结 Confused | 写在19岁的开篇 Rewrite: Start my blog again CommonGit | 个人用常见git命令记录 Summary | 2023 暑期总结 EmbedPython | 一种py打包方式 Cumcmthesis | 基于LaTeX的数模写作模板
Hugo ShortCode | 丰富你的 Markdown
longlin · 2023-08-04 · via longlin的个人小站

Hugo ShortCode

Hugo loves Markdown because of its simple content format, but there are times when Markdown falls short. Often, content authors are forced to add raw HTML (e.g., video <iframe>’s) to Markdown content. We think this contradicts the beautiful simplicity of Markdown’s syntax.

Hugo 官方文档中我们可以得知,为了在丰富 Markdown 文本的同时仍然保持其简约性,Hugo 创造了短代码对繁缛的 Html 进行替代。我们可以简单的将其理解为 Markdown 的一种插件,一种拓展语法,一个 template,它的出现为文本增添了许多新功能。


使用方法

短代码的常见格式为 {{% shortcode parameter %}}{{< shortcode parameter >}}

  • shortcode 为我们所定义的方法,具体代码需要写在 /layouts/shortcodes 文件夹中新建的同名 html 文件里;parameter 则类似 css 中的标签,可以方便我们定位

  • % 所包裹的代码内容会被 markdown 引擎渲染解析后再传入,而 <> 的代码内容不会被渲染

  • 我们可以将其理解为 {{< shortcode >}} 的实质是将同名 shortcode.html 文件内容复制到 markdown 中并执行

Html 一样,shortcode 也有opening 和 closing 两种方式:

  • opening: {{% shortcode %}}
  • closing: {{% shortcode %}} A bunch of code here {{% /shortcode %}}

如果参数过长,则可以考虑用以下形式传递:

1
2
{{<  myshortcode `This is some <b>HTML</b>,
and a new line with a "quoted string".` >}}

下面是一个官方提供的 youtube 教程


简单语法

  • {{ .Inner }} 获取 closing shortcode 中被包裹的所有内容
  • {{ .Get 'foo' }} 获取参数名为 foo 的参数,注意其中 foo 两侧应该为反引号
  • {{ index .Params 0 }} 获取 shortcode 中的第一个参数(和 c 语言中的数组一样,以 0 为起始位)

下面以一个具体例子进行说明:

  1. 先创建 /layouts/shortcodes/abbr.html

  2. 接下来在博客的 markdown 中写入:

1
{{< abbr title="Yahooooooooooooooooooooooooo~" text="Yaho~" >}}
  1. 现在我们便可以在之前所创建的 abbr.html 中进行编辑了,比如使用如下代码:
1
2
3
<abbr title="{{ .Get `title` }}">
  {{ .Get `text` }}
</abbr>
  1. 如此一来在 ./assets/css/_custom.scss 这个自定义 css 文件中(如果你使用的也是 LoveIt 主题的话)我们就可以用 css 对刚才的代码块进行修饰,从而减轻我们在 markdown 中的书写负担(markdown 中只要写 shortcode 就行了,并且这种方法类似函数调用,复用性很高)

例子在这(把鼠标放过来看看吧~) Yaho~

从官方文档我们可以发现,短代码能做的不只这些,它们组合起来可以进行很复杂的操作,条件判断,循环语句都有对应的短代码,它就像一款新的编程语言,只不过是针对博客上的元素进行操作。

并且,Hugo 官方已经帮我们实现好了不少 shortcode ,即取即用,它们被统称为 Custom shortcode,在官网可以进行查看。

另外,在 LoveIt 主题中,诸如 bilibili , music 这些短代码也已经帮我们实现好了,具体可以在 /theme/layouts/shortcodes 里查看。

网上也有不少大佬对短代码进行了整理,现在也贴在下面以供读者查看:


一些插曲

在写这篇博客时,我发现无论如何 {{< shortcode >}} 中的短代码都会比 markdown 语法更快渲染出来,导致网页因为无法识别未定义的 shortcode 而崩溃。为此我在 Google 上查了一个多小时,没有发现任何一篇中文博客提到了这点特性。最终在我将要放弃的时候居然在 Hugo官方的Help界面 发现了问题的答案。只需写成 {{< /*shortcode*/ >}} 就能正常让 shortcode stop rendering 从而被正常渲染。此外,不包含 <> 或 % 的短代码如果加上 /* */ 反而是画蛇添足,正常书写即可。

与此同时,我在不经意间发现了 风月大佬写的将近 5 万字 Hugo 博客详解 , 让我受益匪浅,也大受震撼。

当下的互联网,太多人只是单纯从别人手中照抄照搬,这在中文互联网更是屡见不鲜。
别人的项目,稍微修改一下名字就当作自己的内容发布,这种低质量的水文遍地都是。
高质量的长篇博文却无人问津,甚至连被抄袭的可能性都失去,实在荒唐。

满地六便士,我幻想成为月亮。