[Tools] Batch Create Markdown Files from a Template with Node.js and Mustache
Creating Markdown files from a template is a straightforward process with Node.js and Mustache. You can define a template, load it into your script, then push whatever data you have into your template, then write the files back out. Node.js built-in filesystem tools allow you to read and write the files while Mustache helps you to push the data into the template.
Install:
npm i --save mustache
index.js:
let fs = require("fs")
let { render } = require("mustache")
let template = fs.readFileSync("./template.md").toString() people.forEach(person => {
let output = render(template, person)
fs.writeFileSync(`./people/${person.name}.md`, output)
})
let fs = require("fs")
let { render } = require("mustache") let people = [
{
name: "Luke Skywalker",
height: "",
mass: "",
hair_color: "blond",
skin_color: "fair",
eye_color: "blue",
birth_year: "19BBY",
gender: "male",
homeworld: "https://swapi.co/api/planets/1/",
films: [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/",
"https://swapi.co/api/films/7/"
],
species: ["https://swapi.co/api/species/1/"],
vehicles: [
"https://swapi.co/api/vehicles/14/",
"https://swapi.co/api/vehicles/30/"
],
starships: [
"https://swapi.co/api/starships/12/",
"https://swapi.co/api/starships/22/"
],
created: "2014-12-09T13:50:51.644000Z",
edited: "2014-12-20T21:17:56.891000Z",
url: "https://swapi.co/api/people/1/"
},
{
name: "C-3PO",
height: "",
mass: "",
hair_color: "n/a",
skin_color: "gold",
eye_color: "yellow",
birth_year: "112BBY",
gender: "n/a",
homeworld: "https://swapi.co/api/planets/1/",
films: [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/5/",
"https://swapi.co/api/films/4/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/"
],
species: ["https://swapi.co/api/species/2/"],
vehicles: [],
starships: [],
created: "2014-12-10T15:10:51.357000Z",
edited: "2014-12-20T21:17:50.309000Z",
url: "https://swapi.co/api/people/2/"
},
{
name: "R2-D2",
height: "",
mass: "",
hair_color: "n/a",
skin_color: "white, blue",
eye_color: "red",
birth_year: "33BBY",
gender: "n/a",
homeworld: "https://swapi.co/api/planets/8/",
films: [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/5/",
"https://swapi.co/api/films/4/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/",
"https://swapi.co/api/films/7/"
],
species: ["https://swapi.co/api/species/2/"],
vehicles: [],
starships: [],
created: "2014-12-10T15:11:50.376000Z",
edited: "2014-12-20T21:17:50.311000Z",
url: "https://swapi.co/api/people/3/"
},
{
name: "Darth Vader",
height: "",
mass: "",
hair_color: "none",
skin_color: "white",
eye_color: "yellow",
birth_year: "41.9BBY",
gender: "male",
homeworld: "https://swapi.co/api/planets/1/",
films: [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/"
],
species: ["https://swapi.co/api/species/1/"],
vehicles: [],
starships: ["https://swapi.co/api/starships/13/"],
created: "2014-12-10T15:18:20.704000Z",
edited: "2014-12-20T21:17:50.313000Z",
url: "https://swapi.co/api/people/4/"
},
{
name: "Leia Organa",
height: "",
mass: "",
hair_color: "brown",
skin_color: "light",
eye_color: "brown",
birth_year: "19BBY",
gender: "female",
homeworld: "https://swapi.co/api/planets/2/",
films: [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/",
"https://swapi.co/api/films/7/"
],
species: ["https://swapi.co/api/species/1/"],
vehicles: ["https://swapi.co/api/vehicles/30/"],
starships: [],
created: "2014-12-10T15:20:09.791000Z",
edited: "2014-12-20T21:17:50.315000Z",
url: "https://swapi.co/api/people/5/"
},
{
name: "Owen Lars",
height: "",
mass: "",
hair_color: "brown, grey",
skin_color: "light",
eye_color: "blue",
birth_year: "52BBY",
gender: "male",
homeworld: "https://swapi.co/api/planets/1/",
films: [
"https://swapi.co/api/films/5/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/1/"
],
species: ["https://swapi.co/api/species/1/"],
vehicles: [],
starships: [],
created: "2014-12-10T15:52:14.024000Z",
edited: "2014-12-20T21:17:50.317000Z",
url: "https://swapi.co/api/people/6/"
},
{
name: "Beru Whitesun lars",
height: "",
mass: "",
hair_color: "brown",
skin_color: "light",
eye_color: "blue",
birth_year: "47BBY",
gender: "female",
homeworld: "https://swapi.co/api/planets/1/",
films: [
"https://swapi.co/api/films/5/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/1/"
],
species: ["https://swapi.co/api/species/1/"],
vehicles: [],
starships: [],
created: "2014-12-10T15:53:41.121000Z",
edited: "2014-12-20T21:17:50.319000Z",
url: "https://swapi.co/api/people/7/"
},
{
name: "R5-D4",
height: "",
mass: "",
hair_color: "n/a",
skin_color: "white, red",
eye_color: "red",
birth_year: "unknown",
gender: "n/a",
homeworld: "https://swapi.co/api/planets/1/",
films: ["https://swapi.co/api/films/1/"],
species: ["https://swapi.co/api/species/2/"],
vehicles: [],
starships: [],
created: "2014-12-10T15:57:50.959000Z",
edited: "2014-12-20T21:17:50.321000Z",
url: "https://swapi.co/api/people/8/"
},
{
name: "Biggs Darklighter",
height: "",
mass: "",
hair_color: "black",
skin_color: "light",
eye_color: "brown",
birth_year: "24BBY",
gender: "male",
homeworld: "https://swapi.co/api/planets/1/",
films: ["https://swapi.co/api/films/1/"],
species: ["https://swapi.co/api/species/1/"],
vehicles: [],
starships: ["https://swapi.co/api/starships/12/"],
created: "2014-12-10T15:59:50.509000Z",
edited: "2014-12-20T21:17:50.323000Z",
url: "https://swapi.co/api/people/9/"
},
{
name: "Obi-Wan Kenobi",
height: "",
mass: "",
hair_color: "auburn, white",
skin_color: "fair",
eye_color: "blue-gray",
birth_year: "57BBY",
gender: "male",
homeworld: "https://swapi.co/api/planets/20/",
films: [
"https://swapi.co/api/films/2/",
"https://swapi.co/api/films/5/",
"https://swapi.co/api/films/4/",
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/3/",
"https://swapi.co/api/films/1/"
],
species: ["https://swapi.co/api/species/1/"],
vehicles: ["https://swapi.co/api/vehicles/38/"],
starships: [
"https://swapi.co/api/starships/48/",
"https://swapi.co/api/starships/59/",
"https://swapi.co/api/starships/64/",
"https://swapi.co/api/starships/65/",
"https://swapi.co/api/starships/74/"
],
created: "2014-12-10T16:16:29.192000Z",
edited: "2014-12-20T21:17:50.325000Z",
url: "https://swapi.co/api/people/10/"
}
] let template = fs.readFileSync("./template.md").toString() people.forEach(person => {
let output = render(template, person)
fs.writeFileSync(`./people/${person.name}.md`, output)
})
[Tools] Batch Create Markdown Files from a Template with Node.js and Mustache的更多相关文章
- [转]Getting Start With Node.JS Tools For Visual Studio
本文转自:http://www.c-sharpcorner.com/UploadFile/g_arora/getting-started-with-node-js-tools-for-visual-s ...
- How to create PDF files in a Python/Django application using ReportLab
https://assist-software.net/blog/how-create-pdf-files-python-django-application-using-reportlab CONT ...
- [Tools] Create a Simple CLI Tool in Node.js with CAC
Command-line tools can help you with all sorts of tasks. This lesson covers the very basics of setti ...
- Write Custom Java to Create LZO Files
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+LZO LanguageManual LZO Skip to e ...
- Package template (html/template) ... Types HTML, JS, URL, and others from content.go can carry safe content that is exempted from escaping. ... (*Template) Funcs ..
https://godoc.org/text/template GoDoc Home About Go: text/templateIndex | Examples | Files | Directo ...
- Node.js NPM Tutorial: Create, Publish, Extend & Manage
A module in Node.js is a logical encapsulation of code in a single unit. It's always a good programm ...
- Node.js Tools 1.2 for Visual Studio 2015 released
https://blogs.msdn.microsoft.com/visualstudio/2016/07/28/node-js-tools-1-2-visual-studio-2015/ What ...
- org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot create directory /user/hive/warehouse/page_view. Name node is in safe mode
FAILED: Error in metadata: MetaException(message:Got exception: org.apache.hadoop.ipc.RemoteExceptio ...
- 小白学习VUE第二课:环境搭建 VUE Node.js VSCode template模板
环境搭建 VUE Node.js VSCode template模板: 首先安装node:http://www.runoob.com/nodejs/nodejs-install-setup.html ...
随机推荐
- Sql server2008中merge用法
/// <summary> /// 修改:添加条件: AND roleModule.FuncCode = tvpRoleModule.FuncCode /// </summary&g ...
- Simditor学习--vuejs集成simditor
唠叨 因为项目需要我自己研究了和集成在vue方便以后再使用,详情官方文档在这里.希望大家有好的建议提出让我继续改进. simditor介绍 Simditor 是团队协作工具 Tower 使用的富文本编 ...
- jquery中的done和always解决ajax问题
昨天写一个跨域请求json数据的实例.遇到传值问题,尝试了各种方式都不行,后来发现,同一个地址,同一个ip请求次数频繁传值相同的话,ajax会默认跟一个&?时间戳,这就导致我传过去的值是错误的 ...
- coercing to Unicode错误的一个解决办法
http://blog.csdn.net/happen23/article/details/46683813
- pgAdmin III 是 postgresql 的管理工具
ubuntu postgresql 的管理工具
- getAllResponseHeaders() 必须放到onload里面
<html><head> <meta charset="utf-8"> <title>test</title> < ...
- 【我要学python】愣头青之小数点精度控制
写在最前面:今天遇到了棘手的问题,看了两遍才看懂,本文属于转载+修改,原出处是Herbert's Blog 基础 浮点数是用机器上浮点数的本机双精度(64 bit)表示的.提供大约17位的精度和范围从 ...
- cpu亲和性绑定
将进程与cpu绑定,最直观的好处就是减少cpu之间的cache同步和切换,提高了cpu cache的命中率,提高代码的效率.从cpu架构上,NUMA拥有独立的本地内存,节点之间可以通过互换模块做连接和 ...
- 【UOJ #201】【CTSC 2016】单调上升路径
http://uoj.ac/problem/201 别人都一眼秒的题对我而言怎么那么难qwq 这道题就是要构造一个n*n的邻接矩阵,满足矩阵\(A\)是一个拉丁方阵(也是数独?),\(a_{ij}=a ...
- CodeForces - 1000D Yet Another Problem On a Subsequence
题面在这里! 好智障的一个dp啊,一段开头的数字相当于下面要跟多少个数,直接滚动数组dp就行了... #include<bits/stdc++.h> #define ll long lon ...