napkin-sketchdocs 1.0.0 GitHub

Using napkin-sketch from code: cheatsheet#

On this page

API hub · Reference · Quickstart · Cheatsheet

Reminders for the package as a dependency: the entries, the calls, their options, the result and what throws.

The entries#

ImportRuns inFor
import { ... } from '@isocialpractice/napkin-sketch'A browser, Node, any bundlerThe editor, the model, compositions, and the language
import { ... } from '@isocialpractice/napkin-sketch/node'Node 22.12 and laterReading and writing files
const { ... } = await import('@isocialpractice/napkin-sketch/node')CommonJSThe same, where require cannot load an ES module
import '@isocialpractice/napkin-sketch/markdown'A browser, Node, any bundlerMarkdown Flow text drawn as its Markdown renders and code colored: marked's lexer and highlight.js, given to @isocialpractice/napkin-sketch

@isocialpractice/napkin-sketch: the language#

CallGives
parseScript(text), validateScript(json){ ok, script, diagnostics }
formatScript(script)Canonical text
evaluate(source, options){ ok, book, diagnostics, stats, output, seed }
drawSvg(source, options){ ok, svg, diagnostics }: the first page, in one call
renderSketch(page, options), renderBook(book, options)Strings and bytes, by format
inkBox(page), renderBox(page, options)The box the output is cut to
sketchToComposition(page)The page as a composition, to add to in code
formatDiagnostic(d, 'card.napkin')card.napkin:3:1: error unknown-verb: ...
SCRIPT_VERSION, SCRIPT_LIMITS, VERBS, DIAGNOSTICSThe language's version, budget and tables

@isocialpractice/napkin-sketch/node: files#

CallDoes
drawFile('card.napkin', options)Reads a script file and writes every format asked for
drawToFiles(source, options)The same, for a script in hand: text or JSON
writeBook(book, options)Writes a book already in hand
readScript('card.napkin')Text, or the JSON value of a .json file
loadAssets({ logo: 'art/logo.png' }, from){ logo: 'data:image/png;base64,...' }
loadDocuments({ badge: 'badge.skbk' }, from)The books, for use
loadFonts({ 'Acme Sans': 'fonts/acme-sans.ttf' }, from){ 'Acme Sans': [bytes] }, for fonts; a list takes a family's faces
resolveLinkFromDir('brand')A link resolver jailed to one folder
imageDataUrl, writeComposition, mediaTypeOfThe composition file helpers

drawFile and drawToFiles options#

OptionDefaultMeaning
outthe working folderWhere the files go; made when missing
formats['svg']Any of svg, png, pdf, skbk, jsx
namethe first page'sThe files' name
assets, documentsnoneData URLs and books, or paths read with the loaders
fontsnoneFont files by family, bytes or paths: text is laid out by them, and a PNG and a PDF draw its glyphs as shapes
basethe script's folder for drawFileThe folder links are read inside, and a .jsx places them from
strictfalseWrite nothing unless nothing at all was reported
crop, registration, transparent, scalethe script's ownAs the render calls take them
seed, timestamp, limitsAs evaluate takes them

The result#

FieldHolds
okNo errors; with strict, nothing reported at all
files{ path, format, page }, page from 1 for SVG and PNG
diagnosticsEverything the script reported, in reading order
statsinstructions, marks, anchors, points, pages
warningsWhat a format left out or drew as a stand-in

File names#

BookSVG, PNGPDF, .skbk, .jsx
One pagecard.svgcard.pdf
Three pagescard-1.svg, card-2.svg, card-3.svgcard.pdf, all three pages

What throws#

ThrowsWhen
readScript, drawFileThe file cannot be read; a .json file is not a JSON array
loadAssets, loadDocuments, loadFonts, and the draws given themA file cannot be read, or a document is not a book
drawFile, drawToFiles, writeBookout cannot be made or written to
The render callsAn unknown format

Common mistakes#

WrongRightWhy
import { drawFile } from '@isocialpractice/napkin-sketch'from '@isocialpractice/napkin-sketch/node'The first entry reads no files.
require('@isocialpractice/napkin-sketch/node')await import('@isocialpractice/napkin-sketch/node')Every entry is an ES module, which require loads only on a Node that can.
try { await drawFile(...) } catch for script errorsCheck ok and diagnosticsScript errors are values, not exceptions.