0.1.9 • Published 7 months ago

markdown-to-png-cli v0.1.9

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

Module Version markdown-to-png

markdown-to-png is a NodeJS JavaScript module that converts Markdown files into PNG images using HTML templates for styling. It allows for programmatic control over the layout, theme, and other options for generating styled images from Markdown content.

Please use markdown-to-png-cli for a standalone command-line tool


Table of Contents


Installation

To install the markdown-to-png module for use in your JavaScript project, run:

npm install markdown-to-png

Module Usage

Function Signature

import markdownToPng from 'markdown-to-png';

/**
 * Converts markdown text to PNG images.
 *
 * @param {string} markdownText - The markdown content to convert.
 * @param {string} outputDirectory - Directory where PNGs should be saved.
 * @param {string} baseFileName - Base name to use for the generated PNG files.
 * @param {string} template - HTML template for styling.
 * @param {Object} options - Configuration options.
 */
async function markdownToPng(markdownText, outputDirectory, baseFileName, template, options);

Options

OptionTypeDescriptionDefault
charactersPerPagenumberMaximum characters per page (for pagination)72
layoutstringSelect the page layout: portrait, square, or landscapeN/A
templatestringHTML template for stylingdefault.html
themestringSelect the theme: default, monochrome, dark-purple, medium-purple, dark-orange, bright-orangedefault
limitnumberLimit the number of images to generate10

Example Programmatic Usage

import markdownToPng from "markdown-to-png";
import fs from "fs";
import path from "path";

// Sample Markdown content
const markdownText = fs.readFileSync("path/to/markdown.md", "utf8");

// Path to the template
const template = fs.readFileSync(
  path.resolve("path/to/template.html"),
  "utf-8"
);

// Configuration options
const options = {
  layout: "portrait",
  theme: "default",
  charactersPerPage: 72,
  limit: 5,
};

// Directory to save PNGs
const outputDirectory = "./output";

// Base file name for the PNGs
const baseFileName = "markdown-output";

// Convert the Markdown to PNGs
markdownToPng(markdownText, outputDirectory, baseFileName, template, options)
  .then(() => {
    console.log("PNG images generated successfully!");
  })
  .catch((error) => {
    console.error("Error generating PNG images:", error);
  });

Template Structure

Template Configuration in markdown-to-png

The template in markdown-to-png is dynamically adjusted based on the following script parameters:

  1. layout: Determines the page dimensions.

    • portrait: 1080x1920 px
    • square: 1080x1080 px
    • landscape: 1920x1080 px
    • Injected into the template as data-layout="${layout}" and updates page size (--page-width, --page-height).
  2. theme: Sets the color scheme of the page.

    • Themes: default, monochrome, dark-purple, medium-purple, dark-orange, bright-orange.
    • Injected as data-theme="${theme}", adjusting background and text colors.
  3. Page Content: The Markdown content is injected into the template via the ${content} placeholder.

Example Code:

<html>
  <head>
    <style>
      :root {
        --page-width: /*$width*/ px;
        --page-height: /*$height*/ px;
        --primary-color: #ff6719;
        --text-color: #ffffff;
      }
      body {
        width: var(--page-width);
        height: var(--page-height);
        background: var(--primary-color);
        color: var(--text-color);
      }
    </style>
  </head>
  <body data-layout="${layout}" data-theme="${theme}">
    <div class="inner">${content}</div>
  </body>
</html>

In the above code, ${layout}, ${theme}, and ${content} are placeholders replaced by the script based on the user’s input.


Contributing

Contributions are welcome! If you'd like to contribute to the development of this project, follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Write tests (if applicable).
  4. Submit a pull request with a clear description of the changes.

License

This project is licensed under the MIT License.


Let me know if you need any further changes!

0.1.9

7 months ago

0.1.8

7 months ago

0.1.7

7 months ago

0.1.5

7 months ago

0.1.4

7 months ago