npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ezpaarse-project/jspdf-md

v1.1.0

Published

jsPDF plugin for rendering MD into PDF in Node

Downloads

93

Readme

jsPDF-MD - Markdown plugin for jsPDF

Generate Markdown pages with JavaScript

This jsPDF plugin adds the ability to render Markdown by using jsPDF functions and not using any HTML. So there's no dependency to Puppeteer or any headless browser.

sample-001

Installation

npm i -S jspdf @ezpaarse-project/jspdf-md

Usage

Checkout examples in example folder.

Using the method provided in jsPDF document

import jsPDF from 'jspdf';
import '@ezpaarse-project/jspdf-md';

const doc = new jsPDF({
  unit: 'px', // Currently not supporting other units
  hotfixes: ['px_scaling'], // Needed if unit is "px", see https://github.com/parallax/jsPDF/blob/master/HOTFIX_README.md#px_scaling
});

doc.mdToPDF('# Hello world !')
  .then(() => doc.save('markdown.pdf'));

// Or use returned value :

doc.mdToPDF('# Hello world !')
  .then((pdf) => pdf.save('markdown.pdf'));

Using the function

import jsPDF from 'jspdf';
import mdToPDF from '@ezpaarse-project/jspdf-md';

const doc = new jsPDF({
  unit: 'px', // Currently not supporting other units
  hotfixes: ['px_scaling'], // Needed if unit is "px", see https://github.com/parallax/jsPDF/blob/master/HOTFIX_README.md#px_scaling
});

mdToPDF(
  doc,
  '# Hello world !',
).then(() => doc.save('markdown.pdf'));

Using directly the parser

import jsPDF from 'jspdf';
import { Parser } from '@ezpaarse-project/jspdf-md';

const remoteRequestor = async (url, method) => {
  const request = await fetch(url, { method });

  return {
    data: await request.arrayBuffer(),
    headers: Object.fromEntries(request.headers.entries()),
  };
};

const pdfDoc = new jsPDF({
  unit: 'px', // Currently not supporting other units
  hotfixes: ['px_scaling'], // Needed if unit is "px", see https://github.com/parallax/jsPDF/blob/master/HOTFIX_README.md#px_scaling
});

// Parse Markdown
const parser = new Parser(md);
parser.parse()
  // Load images found during parse
  .then((mdDoc) => pdfDoc.loadImages(remoteRequestor, 'assetsDir'))
  // Render into PDF
  .then(() => {
    mdDoc.render(pdfDoc);

    pdf.save('markdown.pdf')
  })

Options

Below is a list of all options supported in the plugin

Render options

These options are common to all the usages and are all optional.

  • pageBreak: boolean - Should create a new page if an element is overflowing
  • codeFont: string - Font used when rendering a code element. By default it's Monospace (included in the lib)

Plugin options

These options aren't available when using directly the parser, but these are all optional.

  • remoteRequestor: (...args) => object - The function used to fetch images
    • Parameters:
      • url: args[0].string - The url of the ressource
      • method: args[0].string - The method used to get the ressource
    • Returns: response: object
      • response.data: ArrayBuffer - The data of the ressource
      • response.headers: Record<string, string> - Headers of the response
  • margin: number | MarginOption - Margin between markdown and limit of page. If a number is provided, it's apply to all directions
    • MarginOption: { top?: number, left?: number, bottom?: number, right?: number}

Contributions

Contributions are always welcome, especially on open issues. If you have something major you want to add or change, please post an issue about it first to discuss it further. The workflow for contributing would be something like this:

  • Start watcher with npm run dev
  • Make code changes
  • Make sure all examples works
  • Commit and submit pull request

If you don't use ESLint auto-fix on save, please run npm run lint before opening your PR