Socket
Socket
Sign inDemoInstall

md-template

Package Overview
Dependencies
15
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    md-template

Build markdown from es6-templates and js


Version published
Weekly downloads
1
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 05 Nov 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc