需求

存在这样的模板:

image-20210206162447916

用途:写GitHub Pages时创建Markdown的时候,在第一部分的内容,有几个是变量,几个是常量。

解决方案

IDEA 社区版的文件模板功能。

设置

第一步,打开设置:

image-20210206162614967

第二步,搜索template,选择File and Code Templates,点右边的+号:

image-20210206162814189

新建模板

我们起名叫GitHubPageMarkdown吧:

image-20210206164501225

下面的一部分是说明:

image-20210206164520131

设置我们的模板内容:

image-20210206171136063

说明

${title}${categories}是我们自定义的变量,其他的是预定义好的变量。

---
layout: post
title:  "${title}"
date:   ${YEAR}-${MONTH}-${DAY} ${HOUR}:${MINUTE}:16 +0800
categories: ${categories}
rname-cmd: mv ${NAME} ${YEAR}-${MONTH}-${DAY}-${title}
typora-root-url: ../
---

点击ApplyOK完成创建:

查看效果

右键,点New,选择我们刚刚创建好的模板GitHubPageMarkdown

image-20210206165301023

弹出框,填写信息:

image-20210206171334578

这儿的文件名随意写一个不存在的就可以,真实的文件名在后面。点击OK创建文件:

image-20210206171511215

其他都符合了,rname-cmd的有点问题,再修改下模板:

---
layout: post
title:  "${title}"
date:   ${YEAR}-${MONTH}-${DAY} ${HOUR}:${MINUTE}:16 +0800
categories: ${categories}
rname-cmd: mv ./_posts/${NAME}.md ./_posts/${YEAR}-${MONTH}-${DAY}-${title}.md
typora-root-url: ../
---

image-20210206171841492

现在重命名文件,复制rname-cmd:后的内容,在命令行中运行:

mv ./_posts/aaa.md ./_posts/2021-02-06-使用mogrify给图片加水印.md

image-20210206172042619

现在就完成啦。

模板的英文说明


Along with static text, code and comments, you can also use predefined variables (listed below) that will then be expanded like macros into the corresponding values. It is also possible to specify an arbitrary number of custom variables in the format ${<VARIABLE_NAME>}. In this case, before the new file is created, you will be prompted with a dialog where you can define particular values for all custom variables. Using the #parse directive, you can include templates from the Includes tab, by specifying the full name of the desired template as a parameter in quotation marks. For example: #parse("File Header.java") 
Predefined variables will take the following values:
${PACKAGE_NAME}
 
name of the package in which the new file is created
${NAME}
 
name of the new file specified by you in the New <TEMPLATE_NAME> dialog
${USER}
 
current user system login name
${DATE}
 
current system date
${TIME}
 
current system time
${YEAR}
 
current year
${MONTH}
 
current month
${MONTH_NAME_SHORT}
 
first 3 letters of the current month name. Example: Jan, Feb, etc.
${MONTH_NAME_FULL}
 
full name of the current month. Example: January, February, etc.
${DAY}
 
current day of the month
${DAY_NAME_SHORT}
 
first 3 letters of the current day name. Example: Mon, Tue, etc.
${DAY_NAME_FULL}
 
full name of the current day. Example: Monday, Tuesday, etc.
${HOUR}
 
current hour
${MINUTE}
 
current minute
${PROJECT_NAME}
 
the name of the current project
 Apache Velocity template language is used