md-template

1.0.0 • Public • Published

md-template

Build markdown from es6-templates and js

All js files will be executed and exported object will be deeply merged together in order. This data will be made available to the template.

Example

md-template ./template.md ./data.js > README.md

CLI

module.exports = () => {
  const { execSync } = require("child_process");
  const fs = require("fs");
  const path = require("path");
  const os = require("os");
  const uuid = require("uuid/v1");
 
  const expected = `
    # one
    1
 
    ## two
    2
 
    ## RGB
    red green blue
  `;
 
  const template = `
    # one
    \${one}
 
    ## two
    \${two}
 
    ## RGB
    \${color_channels.slice(0,3).join(" ")}
  `; // NOTE: backslashes not neccissary in actual file
 
  const data = `
    module.exports = {
      one: 1,
      two: 2,
      color_channels: ["red", "green", "blue", "alpha"]
    };
  `;
  const demoDir = path.join(os.tmpdir(), uuid());
  fs.mkdirSync(demoDir);
  const templatePath = path.join(demoDir, "template.md");
  const dataPath = path.join(demoDir, "data.js");
 
  fs.writeFileSync(templatePath, template);
  fs.writeFileSync(dataPath, data);
 
  const result = execSync(`node cli.js ${templatePath} ${dataPath}`, {
    encoding: "utf8"
  });
  return result === expected;
};
 

Usage

md-template:
  type: object
  example: md-template <TEMPLATE> <...DATA> [options]
  description: >
    Generates document from templated object and exported data.
    Prints new document to stdout
  properties:
    TEMPLATE:
      type: string
      example: template.md
      description: >
        Path to document whose text content will be used as
        a template string
    DATA:
      type: array
      description: >
        Paths to json files which will be required,
        properties exported by these scripts will
        be deeply merged in the order in the command
        and made available to template
      items:
        type: string
        example: data.js
    options:
      type: object
      properties:
        "-h":
          type: boolean
          description: >
            When present, prints help file to stderr
            and returns non-0 exit code
 

API

module.exports = async () => {
  const { execSync } = require("child_process");
  const fs = require("fs");
  const path = require("path");
  const os = require("os");
  const uuid = require("uuid/v1");
 
  const expected = `
    # one
    1
 
    ## two
    2
 
    ## RGB
    red green blue
  `;
 
  const template = `
    # one
    \${one}
 
    ## two
    \${two}
 
    ## RGB
    \${color_channels.slice(0,3).join(" ")}
  `; // NOTE: backslashes not neccissary in actual file
 
  const data = `
    module.exports = {
      one: 1,
      two: 2,
      color_channels: ["red", "green", "blue", "alpha"]
    };
  `;
 
  const demoDir = path.join(os.tmpdir(), uuid());
  fs.mkdirSync(demoDir);
  const templatePath = path.join(demoDir, "template.md");
  const dataPath = path.join(demoDir, "data.js");
 
  fs.writeFileSync(templatePath, template);
  fs.writeFileSync(dataPath, data);
  const results = await require("../index")({
    templatePath: templatePath,
    scriptPaths: [dataPath]
  });
  return results === expected;
};
 

arguments

templatePath

type: string

path to template file

scriptPaths

type: string | array

paths of scripts which will be executed and their results deeply merged together in order, and this data will be available to the template

Package Sidebar

Install

npm i md-template

Weekly Downloads

0

Version

1.0.0

License

ISC

Last publish

Collaborators

  • errorstream