Skip to content

Designs in Markdown ​

Documentation written in Comark Markdown can show parts of an OpenPencil document by naming them: a component as an image, the value a variable holds, a collection's table, a whole spec, or a live canvas. The tags are the ones a spec is written with, plus doc, which names the document. When the Markdown is parsed, openPencil() from @open-pencil/cli/comark opens the document with OpenPencil's engine and draws each one, so the page always shows the design as the file has it.

This Markdown:

md
::spec{doc="checkout" name="Checkout"}
::

The primary button's fill is :token{doc="checkout" name="color/primary"}:

::instance{doc="checkout" component="Button" Variant="Primary"}
::

::canvas{doc="checkout" node="Button" height="280"}
::

renders as this, from the checkout.fig document beside this page:

Checkout button

The button that ends checkout. Its fill is #3366FF and its corners are rounded by 8.

Button

Use the secondary button to step back, never to cancel the order.

Button

Tokens

BrandMode 1
color/primary#3366FF
color/ink#1F2937
color/surface#F3F4F6
radius/control8

Rules

  • One primary button per screen.
  • The label says what happens: Pay now, not Submit.

The primary button's fill is #3366FF:

Button

Name the documents ​

Add the plugin to the parser and name each document the Markdown refers to:

ts
import { parseMarkdown } from 'comark'
import { openPencil } from '@open-pencil/cli/comark'

const tree = await parseMarkdown(markdown, {
  plugins: openPencil({
    documents: {
      checkout: 'design/checkout.fig',
      brand: { path: 'design/brand.fig', url: '/designs/brand.fig' }
    }
  })
})

A document is a path to a .fig file or any other file OpenPencil opens, from the working directory or from root. Give it a url where your visitors can fetch it to draw it on a live canvas. A document is read once and again only when its file changes, so a dev server picks up a saved design on the next parse.

Parse in Node: at build time, in a server route, or in a script. Documents are opened and drawn one at a time, so parsing many pages at once is safe. The result is ordinary Comark elements, which every Comark renderer shows: Vue, React, Svelte, Angular, or HTML.

Write the tags ​

MarkdownShows
::instance{doc="checkout" component="Button" Variant="Primary"}The component, with the props an instance of it takes, as an image
:token{doc="checkout" name="color/primary"}The value the variable holds, with a swatch for a colour
::tokens{doc="checkout" collection="Brand"}A table of the collection's variables, a column per mode
::spec{doc="checkout" name="Checkout"}The spec named Checkout, with its tokens and instances
::canvas{doc="checkout" node="Button" height="280"}A live canvas framed on the layer named Button

name and node take a layer's name or ID. A spec copied out of the editor renders unchanged once its tags name the document with doc. Something the document does not have shows as the name the Markdown gives it, in an op-spec-missing code element, and a document the options do not name stops the parse with an error.

Render it ​

Render the parsed tree with your renderer's document component. With Vue, in a VitePress page or a Nuxt server route, as this page does:

vue
<script setup lang="ts">
import { MarkdownDocument } from '@comark/vue'
import { DocumentCanvas } from '@open-pencil/vue/canvas'

import { data } from './checkout.data'
</script>

<template>
  <Suspense>
    <MarkdownDocument
      :value="{ nodes: data.nodes }"
      :components="{ 'open-pencil-canvas': DocumentCanvas }"
    />
  </Suspense>
</template>

A ::canvas becomes an open-pencil-canvas element with the document's src and the node to frame. Map it to DocumentCanvas from @open-pencil/vue/canvas, which shows the document read-only: visitors can pan, zoom, and select, but not change it. The canvas runs OpenPencil's engine in the browser, so render it only on the client, and serve the engine's files with openPencil() from @open-pencil/core/node/vite in your Vite config.

Style it ​

The rendered parts carry classes for your styles:

ClassOn
op-speca spec's content
op-spec-tokena token's value
op-spec-swatcha colour token's swatch
op-spec-tokensa ::tokens table
op-spec-instancean instance's image
op-spec-canvasa live canvas
op-spec-missingsomething the document does not have

Instance images show at the size the component has on the canvas and stay sharp on dense screens.

Without the engine ​

Where the Markdown cannot be parsed with OpenPencil's engine, such as in a browser or an edge function, export a spec bundle instead: a .spec.json file with a spec's Markdown, the values its tokens show, its tables, and images of its instances. In the editor, add an export in the Export section and choose Spec bundle; from the command line, run openpencil export design.fig --format spec --node <id>. Then render it with specBundle(bundle) from @open-pencil/comark, or with defineSpecComponent(bundle) from @open-pencil/comark/vue:

ts
import { readSpecBundle, specBundle } from '@open-pencil/comark'
import { parseMarkdown } from 'comark'

const read = readSpecBundle(text)
if (!read.ok) throw new Error(read.error)
const tree = await parseMarkdown(read.bundle.markdown, { plugins: specBundle(read.bundle) })

A bundle shows the design as it was when it was exported; export it again when the design changes.