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

推荐订阅源

小众软件
小众软件
B
Blog RSS Feed
美团技术团队
博客园 - 【当耐特】
C
Check Point Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
aimingoo的专栏
aimingoo的专栏
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
Vercel News
Vercel News
P
Proofpoint News Feed
IT之家
IT之家
I
InfoQ
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More

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#