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)
})

Code

[Tools] Batch Create Markdown Files from a Template with Node.js and Mustache的更多相关文章

  1. [转]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 ...

  2. 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 ...

  3. [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 ...

  4. Write Custom Java to Create LZO Files

    https://cwiki.apache.org/confluence/display/Hive/LanguageManual+LZO LanguageManual LZO     Skip to e ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 小白学习VUE第二课:环境搭建 VUE Node.js VSCode template模板

    环境搭建 VUE Node.js VSCode template模板: 首先安装node:http://www.runoob.com/nodejs/nodejs-install-setup.html ...

随机推荐

  1. npm --save-dev --save 的区别【转载】

    源地址:http://blog.csdn.net/juzipchy/article/details/65653683 npm install 在安装 npm 包时,有两种命令参数可以把它们的信息写入 ...

  2. 2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) 日常训练

    A - Archery Tournament 题目大意:按时间顺序出现靶子和射击一个位置,靶子的圆心为(x, y)半径为r,即圆与x轴相切,靶子不会重叠,靶子被击中后消失, 每次射击找出哪个靶子被射中 ...

  3. schtasks命令

    1.创建任务 在每天的22.44定时执行一次. schtasks /create /tn : 在特定时间运行一次. schtasks /create /tn : /sd // 2.运行一次任务 创建任 ...

  4. 【剑指offer】面试题 5. 替换空格

    面试题 5. 替换空格 题目:请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy. 则经过替换之后的字符串为We%20Are%20Hap ...

  5. 关于Vue-cli的跨域解决

    由于Vue-cli服务器是跑在node环境下的8080端口,我们的php代码可能在Apache环境下的7070端口,这个时候就会出现跨域 此刻这段php代码在7070端口上 如果直接去访问 页面报错 ...

  6. 转:攻击JavaWeb应用[3]-SQL注入

    转:http://static.hx99.net/static/drops/tips-236.html 攻击JavaWeb应用[3]-SQL注入 园长 · 2013/07/16 18:28 注:本节重 ...

  7. 洛谷P1392 取数 [堆]

    题目传送门 取数 题目描述 在一个n行m列的数阵中,你须在每一行取一个数(共n个数),并将它们相加得到一个和.对于给定的数阵,请你输出和前k小的取数方法. 输入输出格式 输入格式: 第一行,三个数n, ...

  8. 一个微服务+DDD(领域驱动设计)的代码结构示例

    前有幸拜读过诸多大神关于DDD的实现落地等文章,学习较多,受益匪浅,在此推荐 : https://www.cnblogs.com/hafiz/p/9388334.htmlhttps://blog.cs ...

  9. Xamarin iOS项目找不到模拟器

    Xamarin iOS项目找不到模拟器 在Visual Studio中,突然找不到模拟器,并且项目提示试用期过期:Trial period has expired.出现这种情况,是由于苹果系统中Xam ...

  10. BZOJ 3809 Gty的二逼妹子序列(莫队+分块)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...