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

推荐订阅源

D
DataBreaches.Net
小众软件
小众软件
腾讯CDC
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
美团技术团队
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
罗磊的独立博客
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
C
Check Point Blog
大猫的无限游戏
大猫的无限游戏
Recent Announcements
Recent Announcements

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN Mintty Tips and Configurations
Creating Markdown Front Matter with Ultisnips
2019-12-22 · via jdhao's digital space

Introduction#

Currently, I write my blog posts in Markdown and build the blog using Hugo. Hugo supports front matter for Markdown so that you can attach metadata for a post, such as title, date, tags, categories, etc.

The front matter for a post looks like the following:

---
title: "Creating Markdown Front Matter with Ultisnips"
date: 2019-12-22 13:45:25+0800
tags: [Vim, Markdown]
categories: [Nvim]
---

The date field should be in ISO date format.

Initial snippet#

It would be tedious to write the boilerplate text in front matter for every post I create. Ultisnips is good at automating such tasks. If you are not familiar with Ultisnips, you can check my previous post on setting up Ultisnips.

So the initial snippet I wrote looks like this:

---
snippet meta "Markdown front matter in YAML format" b
title: "${1:TITLE TEXT}"
date: $2
tags: [$3]
categories: [$4]
---

When we type the trigger word meta at the beginning of a line1 and press Tab, it will be expanded. We can jump forward and backward among the tabstops to fill the text.

This snippet works great. However, for the date field, we still need to fill the current date manually, which is not ideal enough.

Ultisnips command interpolation#

Ultisnips also provides a powerful feature called interpolation, which means that you can run shell, Vim or Python command inside a snippet and use the output in snippet. The interpolation code are placed inside two backticks (`CODE`) and opens up many possibilities. For shell command interpolation, you just write the shell command inside the backticks. For Vim and Python code interpolation, you should follow the opening backtick with !v and !p respectively.

For example, to get the current date in ISO format, i.e., 2019-12-22 14:43:43+0800, using the above three interpolations, the code is like:

  • shell interpolation: `date "+%Y-%m-%d %H:%M:%S%z"`
  • Vim interpolation: `!v strftime("%Y-%m-%d %H:%M:%S%z")`
  • Python interpolation:
    `!p from datetime import datetime
    snip.rv=datetime.now().strftime("%Y-%m-%d %H:%M:%S%z")`

Back to the above snippet, if we want the interpolation code to be cross-platform, we should only consider Vim or Python interpolation since most Linux shell commands are not available on Windows.

Vim interpolation#

With Vim interpolation, the original meta snippet becomes:

snippet "meta(data)?" "Markdown metadata front matter" br
---
title: "$1"
date: `!v strftime("%Y-%m-%d %H:%M:%S%z")`
tags: [$2]
categories: [$3]
---

One minor issue with this snippet is that the date value keeps changing while we are inside the snippet region2, which is a little annoying. According to tutorial here, we can use Python interpolation to deal with this issue.

Python interpolation#

Inside the Python interpolation code, Ultisnips provides the snip object. The attribute snip.c will be empty once the interpolation has finished. snip.rv is the result for the snip interpolation.

We can check if snip.c is empty to fix the date once the interpolation code is run.

The new meta snippet now becomes:

snippet "meta" "Markdown metadata front matter" b
---
title: "$1"
date: `!p from datetime import datetime
if not snip.c:
    snip.rv=datetime.now().strftime("%Y-%m-%d %H:%M:%S%z")`
tags: [$2]
categories: [$3]
---

Now the date field will be fixed while we are editing other parts of the snippet.

References#