3.0.0 • Published 13 days ago

src2img v3.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
13 days ago

Install

$ npm i src2img

Usage

import fs from 'node:fs/promises'
import { join } from 'node:path'
import src2img from 'src2img'

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({
  // Font size and unit control the size and quality of the image.
  fontSize: 20,
  fontSizeUnit: `pt`,
  padding: 3,
  // Using 'px' does not scale with font size.
  paddingUnit: `vw`,
  // Png or jpeg
  type: `png`,
  src: srcs.map(({ src }) => [
    src,
    // https://prismjs.com/index.html#languages-list
    `javascript`,
    // 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.replaceAll(/\.[^.]+$/gu, ``)}.png`),
      image,
    ),
  ),
)

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

Some rendered code:

example

Related

Contributing

Stars are always welcome!

For bugs and feature requests, please create an issue.

License

MIT © Tomer Aberbach

3.0.0

13 days ago

2.0.1

2 years ago

2.0.0

3 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago