src2img NPM Module

Converts source code to high quality images.

Install

Install with npm:

$ npm i src2img --save

Usage

const fs = require('fs')
const path = require('path')
const src2img = require('src2img')

const src = 'path/to/sources'
const out = 'path/to/out'

const names = fs.readdirSync(src)

src2img({
  fontSize: 20, // Font size and unit control the size and quality of the image
  fontSizeUnit: 'pt',
  padding: 3,
  paddingUnit: 'vw', // Using 'px' does not scale with font size
  type: 'png', // png or jpeg
  src: names.map(name => [
    fs.readFileSync(path.join(src, name)).toString(),
    'javascript' // https://prismjs.com/index.html#languages-list
    // See https://www.npmjs.com/package/filename2prism for getting alias from filename
  ])
}).then(images => Promise.all(images.map(
  (image, i) => fs.writeFileSync(path.join(out, `${names[i].replace(/\.[^.]+$/g, '')}.png`), image))
))

Look at the CLI package if you’d like to use this from the command line.

Some rendered code:

example

Contributing

Pull requests and stars on the GitHub repository are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Install dev dependencies:

$ npm i -d && npm test

License

Copyright © 2018 Tomer Aberbach Released under the MIT license.