Skip to content

TomerAberbach/src2img

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

src2img

Converts source code to high quality images.

Install

$ npm i src2img

Usage

import { promises as fs } from 'fs'
import src2img from 'src2img'
import { join } from 'path'

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

const names = await fs.readdir(src)
const srcs = Promise.all(
  names.map(async name => ({
    name,
    src: await fs.readFile(join(src, name), `utf8`)
  }))
)

const images = await 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: srcs.map(({ src }) => [
    src,
    `javascript` // https://prismjs.com/index.html#languages-list
    // See https://www.npmjs.com/package/filename2prism for getting alias from filename
  ])
})

await Promise.all(
  images.map((image, i) =>
    fs.writeFile(
      join(out, `${srcs[i].name.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

Stars are always welcome!

For bugs and feature requests, please create an issue.

License

MIT © Tomer Aberbach