Skip to content

@ogygia/content · Macros

Code highlighting

import.meta.og.code(source, lang, meta?) highlights a snippet when you build, through the same Shiki settings your markdown uses, and puts the result in the module as a static region. The page gets HTML and no highlighter.

On this page

A component that shows code (a feature on a landing page, an example beside a demo) needs it highlighted, and shipping a highlighter to the browser for that is a waste. import.meta.og.code(source, lang, meta?) highlights the snippet at build time, through your app’s own markdown code settings: the same themes, transformers and meta as a code block in a .svx file. The build replaces the call with the finished HTML as a region, and you render it with <Region of={…} />. It ships no JavaScript.

code comes with @ogygia/content: list its content() plugin beside ogygia() and add /// <reference types="@ogygia/content/types" /> to src/ogygia.d.ts (Install). It works without the markdown option, at the default code settings. Without the plugin, a call fails the build with macro_needs_plugin.

<script lang="ts">
  import { Region } from 'ogygia';

  const example = import.meta.og.code(`
    const cart = new Cart();
    cart.add(item);
  `, 'ts', 'title=src/lib/cart.ts');
</script>

<Region of={example} />

Arguments

  • source is the code. A template literal is usually easiest, since code is full of quotes and <, none of which need escaping in one.
  • lang is the language, as you’d write it after the backticks of a code block: 'ts', 'svelte', 'bash'. A language your settings don’t load renders as plain text.
  • meta (optional) is the rest of that opening line, exactly as in a code block: 'title=app.ts', and '{2-4}' or 'twoslash' when you’ve added the transformers that read them (Line highlights and type hovers).

All three are read at build time, so each must be written in the source: a string or a template literal with no ${…}. An interpolation fails with content_code_interpolated, a variable or expression with content_code_not_static, and fewer than two or more than three arguments with content_code_arity.

Dedent

A snippet inside a call is indented to match the code around the call. code() takes off the indentation every line shares and trims blank lines at the start and end, so the snippet renders flush left with its own indentation kept. Write it nested where it sits.

Caching

The result is cached in node_modules/.ogygia/fences/, the same cache markdown code blocks use, by the snippet, its language and meta, and your highlighting settings. The same snippet in two places renders once, and a build with a warm cache renders none. The HTML matches the same code block in a markdown file, except that a snippet has no id (a block in a document gets one from its heading).

Supported places

code() is rewritten in .ts and .js modules and in a .svelte component’s <script>. It isn’t rewritten in markup: call it in the script and use the value in the template (macro_in_markup). The <script> of a .svx or .md file isn’t compiled for it yet either, and a call there fails the build with the same code. In a markdown page, write a fenced code block, or call code() in a .ts module and import the region. The same call in a comment or a string is left alone.

For code inside prose, write a code block in markdown. For a whole passage with headings and lists, use md. All three end up as static HTML.