yaakdoc

Node API

Build, serve and eject from code, and the building blocks behind the integrations.

Everything the CLI does is available from Node:

import { build, createDevServer, eject, init, preview } from "yaakdoc";

const { outDir, files } = await build({
  cwd: "docs",
  overrides: {
    pages: "per-request",
  },
});

const server = await createDevServer({
  cwd: "docs",
  port: 4000,
});

console.log(server.url);

await server.close();

yaakdoc also re-exports defineConfig, defineTheme and defineSnippet.

Options

build, createDevServer and resolveContext take:

OptionDescription
cwdProject directory. Default: process.cwd().
configFileAn explicit config file, or false to not load one.
overridesConfig values that win over the config file.
exportDataThe Yaak export as an object, used instead of reading it from a file.
publicDirThe folder copied to the site root. Default: public/ in the project; false for none.

Integration building blocks

The lower-level pieces behind the integrations are exported too:

  • createDevMiddleware(options): the dev server as a middleware ({ middleware, vite, getContext, close }).
  • createStaticMiddleware({ outDir, base }): serves a built site.
  • mountOptions(options): turns integration options (path, config, export, ...) into engine options.

Without Vite: yaakdoc/static

yaakdoc/static is the part of the engine that doesn't need Vite. It loads the config and export, prepares the document, and writes a site with a theme that's already built. Use it to bundle yaakdoc into an environment where Vite can't run, the way the Yaak plugin does.

import {
  emptyOutDir,
  resolveContext,
  themeFromDefinition,
  withBase,
  writeSite,
} from "yaakdoc/static";

const context = await resolveContext({
  exportData,
  overrides: {
    outDir: "site",
  },

  // Supply the theme instead of loading it from a package or path.
  resolveTheme: (spec, { themeOptions }) => {
    return themeFromDefinition(myTheme, {
      manifest: "/themes/my-theme/theme.ts",
      themeOptions,
    });
  },
});

await emptyOutDir(context.config.outDir, context.root);

// ...copy the theme's prebuilt client assets into the output directory...

await writeSite(context, {
  render,
  assets: withBase(assets, context.config.base),
});

The theme's client assets and its render function come from bundleTheme(context, outDir, { base: "./" }) in the main entry. It builds the theme with Vite once, and the "./" base makes the client assets work under any base path.

themePackageCandidates(spec) lists the npm packages a theme name can refer to, in the order yaakdoc tries them (winxp → @yaakdoc/theme-winxp, yaakdoc-theme-winxp, winxp).

On this page