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. shell日志重定向到null

    用输出重定向符号> 即可,格式如下:shell命令 >/dev/null 若要将标准错误输出也一并重定向,如下:shell命令 >/dev/null 2>&1这样就不管 ...

  2. Java实现蛇形矩阵

    public class Solution { //下x++ 左y-- 上x-- 右y++ public void prints(int n) { int[][] mp = new int[n][n] ...

  3. NYOJ 228 士兵杀敌(五)【差分标记裸题】

    题目链接 所有元素初始值为0才能这么做: ①l--r全加1 a[l]++; a[r+1]--; 求一遍前缀和为元素本身. 求两遍前缀和为元素前缀和. #include<cstdio> #i ...

  4. POJ1733 Party game [带权并查集or扩展域并查集]

    题目传送 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10870   Accepted: 4182 ...

  5. 洛谷P2127 序列排序 [贪心]

    题目传送门 题目描述 小C有一个N个数的整数序列,这个序列的中的数两两不同.小C每次可以交换序列中的任意两个数,代价为这两个数之和.小C希望将整个序列升序排序,问小C需要的最小代价是多少? 输入输出格 ...

  6. SecureFXPortable中文乱码

    SecureFXPortable有一个非常奇怪的地方,明明在全局选项中已经将编码设置为UTF-8,但连接Linux还是会出现中文乱码. 后来发现这个全局选项竟然不对SecureFXPortable生效 ...

  7. shell 读配置文件

    今天跟同事探讨了一下 shell 脚本中对配置文件的读写问题.在此总结一下常用的配置文件的读写方式.大多数的配置文件都是以key=value形式存在的.配置项完全由键值对组成.这样的配置文件读写也是最 ...

  8. 自定义word快捷键,设置插入图片快捷键

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 自定义word快捷键,设置插入图片快捷键 文件→选项→自定义功能区 选择键盘快捷方式 自 ...

  9. UML功能模型(用例图)

        在UML系统开发中有三个主要的模型:功能模型(从用户角度展示系统的功能,包括用例图).对象模型(采用对象,属性,操作关联等概念展示系统的结构和基础,包括类图.对象图.包图).动态模型(展示系统 ...

  10. [UOJ409]Highway Tolls

    题意:交互题,给定一个简单无向图和$A,B(1\leq A\lt B)$,你可以对每条边指定其边权为$A$或$B$后通过交互库询问$S\rightarrow T$的最短路($S,T$在程序运行之前已经 ...