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. Mybatis框架-2

    1.Mybatis中的接口形式 在Mybatis中使用接口形式将通过代理对象调用方法,从而实现sql的执行 1)定义一个接口 package mapper; import java.util.List ...

  2. ZOJ 3279-Ants(线段树)

    传送门:zoj 3279 Ants Ants Time Limit: 2 Seconds      Memory Limit: 32768 KB echo is a curious and cleve ...

  3. mysql source 乱码

    mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql

  4. Storm基本概念以及Topology的并发度

    Spouts,流的源头 Spout是Storm里面特有的名词,Stream的源头,通常是从外部数据源读取tuples,并emit到topology Spout可以同时emit多个tupic strea ...

  5. NBUT 1218 You are my brother

    $dfs$. 记录一下每一个节点的深度就可以了. #include<cstdio> #include<cstring> #include<cmath> #inclu ...

  6. upm配置文件

    组件配置说明 Ø 配置文件规范 Ø 组件分为公共组件和私有组件,分别在public段和private段,如下所示. <?xml version="1.0" encoding= ...

  7. js数组,在遍历中删除元素

    /** * 有效的方式 - 改变下标,控制遍历 */ for (var i = 0; i < arr.length; i++) { if (...) { arr.splice(i, 1); // ...

  8. Check if a user is in a group

    groups or groups user

  9. Hive 空指针(NPE)异常

    空指针NullPointerException 1 Hive之前的一些BUG [HIVE-9430] - NullPointerException on ALTER TABLE ADD PARTITI ...

  10. 【线段树区间合并】BZOJ1593-[Usaco2008 Feb]Hotel 旅馆

    好无聊,以前写过没什么好讲的,水过.戳 #include<iostream> #include<cstdio> #include<cstdlib> #define ...