Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

welovedevs/carbonara

 
 

Repository files navigation

Carbonara build

API for carbon.

Getting started

Send POST https://carbonara-42.herokuapp.com/api/cook to take an image of code snippet.

How it works

  • Puppeteer visit https://carbon.now.sh.
  • Mapping all params in properly URL params.
  • Taking a screenshot of the editor.

POST /api/cook

Body is JSON with next params:

parameter default type description
code (required) string code snippet
backgroundColor "rgba(171, 184, 195, 1)" string hex or rgba color
dropShadow true boolean turn on/off shadow
dropShadowBlurRadius "68px" string shadow blur radius
dropShadowOffsetY "20px" string shadow offset y
exportSize "2x" string resolution of exported image, e.g. 1x, 3x
fontSize "14px" string font size
fontFamily "Hack" string font family, e.g. JetBrains Mono, Fira Code. See all names in carbon exported config.
firstLineNumber 1 number first line number
language "auto" string programing language for properly highlighting. See name in carbon exported config. For example bash is named "application/x-sh"
lineHeight "133%" string line height
lineNumbers false boolean turn on/off line number
paddingHorizontal "56px" string horizontal padding
paddingVertical "56px" string vertical padding
prettify false boolean prettify code with prettier. It works with javascript snippets only, like in carbon.
theme "seti" string code theme
watermark false boolean turn on/off watermark
widthAdjustment true boolean turn on/off width adjustment
windowControls true boolean turn on/off window controls
windowTheme "none" "none" "sharp" "bw" window theme

Defaults params are the same as https://carbon.now.sh.

Response is an image of a code snippet.

Example

Creating image and saving to code.png in terminal.

curl -L https://carbonara-42.herokuapp.com/api/cook \
-X POST \
-H 'Content-Type: application/json' \
-d '{
      "code": "export default const sum = (a, b) => a + b",
      "backgroundColor": "#1F816D"
    }' \
> code.png

Easy way to tune image

  1. Visit https://carbon.now.sh.
  2. Set appearance.
  3. Click gear → miscexport config for downloading JSON with the current setting.
  4. Add code property in JSON.
  5. Use JSON in /api/cook request body.

Unsupported params

These options exist in exported config but there is not a possibility pass them via URL: backgroundImage, backgroundImageSelection, backgroundMode, squaredImage, hiddenCharacters, name, loading, icon, isVisible, width.

Docker container

Running the server in 3000 port with docker:

docker run -p 3000:3000 -it petersolopov/carbonara

Running test

docker build -t local/carbonara .
docker run -it --rm local/carbonara npm test

Development

There are two main files:

  • api/cook.js — lambda function (deprecated).
  • src/index.js – nodejs server that running in docker container.

Running development server:

docker build -t local/carbonara .
docker run -v $(pwd):/home/pptruser/app/ -p 3000:3000 -it --rm local/carbonara npm run nodemon

Updating test images:

docker build -t local/carbonara .
docker run -v $(pwd):/home/pptruser/app/ -it --rm local/carbonara npm test

LICENSE

MIT.

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 95.4%
  • Dockerfile 3.7%
  • HTML 0.9%