[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 ...
随机推荐
- [水煮 ASP.NET Web API2 方法论](1-2)在 WebForm 应用程序中添加 ASP.NET Web API
问题 怎么样将 Asp.Net Web Api 加入到 Asp.Net Web From 应用程序中 解决方案 在 Visual Studio 2013 中,创建新的 Web From,可以直接在&q ...
- 【剑指offer】面试题 65. 不用加减乘除做加法
面试题 65. 不用加减乘除做加法 题目描述 题目:写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. Java 实现 public class Solution { ...
- STL模板整理 Binary search(二分查找)
前言: 之前做题二分都是手动二分造轮子,用起来总是差强人意,后来看到STL才发现前辈们早就把轮子造好了,不得不说比自己手动实现好多了. 常用操作 1.头文件 #include <algorith ...
- IEDA快捷键
前言 开发工具从eclipse过渡到idea了:在刚开始使用的时候被idea强大的快捷键都惊呆了,这里记录了常见的一些快捷键和小伙伴们分享. 快捷键 鼠标悬停在单词上自动提示 : settings-- ...
- 最正确的React事件绑定方式
参考这篇文章:Choosing the Best Approach for React Event Handlers 1.function.bind()方式 2.inline arrow functi ...
- 洛谷——P2952 [USACO09OPEN]牛线Cow Line
P2952 [USACO09OPEN]牛线Cow Line 题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a l ...
- Heavy Transportation(POJ - 1797 变形版 dijkstra)
Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...
- luogu P3375 【模板】KMP字符串匹配
题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果你不知道这是什么意思也不要问,去百度搜[ ...
- hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)
Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K ...
- 浅析position:relative position:absolute
定位一直是WEB标准应用中的难点,如果理不清楚定位那么可能应实现的效果实现不了,实现了的效果可能会走样.如果理清了定位的原理,那定位会让网页实现的更加完美. 定位的定义: 在CSS中关于定位的内容是: ...