hyogen.md

0.14.0

dataSources / loadDataSources

Load external data files (YAML / JSON / CSV) only through the public API and bind them as template variables. DSL import / require cannot read files.

Version: 0.11.0

import { renderServer, loadDataSources, build } from '@b4moss/hyogen-md'

Option dataSources

Pass a variable name → file path map in renderServer / build options.

const result = await renderServer('./page.md', {}, {
  root: './site',
  dataSources: {
    site: './data/site.yaml',
    products: './data/products.json',
    rows: './data/rows.csv',
  },
})

Map keys become template variable names:

# {{ site.title }}

{{#each products.items}}
- {{ this.name }}
{{/each}}
ItemRule
PathsRelative to options.root (or .doc_root / cwd when omitted)
KeysNo constraints (any string)
Size limit5MB per file (data_source_too_large when exceeded)
Remote URLsUnsupported (http:// / https:// → load_failed)
ClientPassing to renderClient throws data_sources_on_client (empty {} is treated as omitted)

Function loadDataSources

You can load files first and merge into context yourself.

const fromFiles = await loadDataSources(
  { site: './data/site.yaml' },
  { root: './site' },
)

await renderServer('./page.md', { ...fromFiles, extra: 1 })
  • Exported only from @b4moss/hyogen-md (server package)
  • Not exported from @b4moss/hyogen-md/client
  • Same parse rules, size limit, and remote rejection as the dataSources option

Context merge order

For renderServer / build, shallow-merge in this order (later wins):

  1. Result of loadDataSources(dataSources)
  2. Caller context
  3. serverContext

Front matter is applied even later inside rendering.

Formats

ExtensionValue bound to the variable
.jsonJSON.parse result (object / array / primitive)
.yaml / .ymlResult from the yaml package
.csvHeader row required → array of objects (all fields are strings)

YAML

  • Types (dates, numbers, booleans, null, …) follow the library defaults
  • Alias / anchor are allowed
  • Duplicate keys follow the parser default (later wins)
  • Invalid YAML → parse_error

CSV (strict)

  • RFC 4180 subset (comma, double quotes, \r\n / \n)
  • Blank rows are skipped
  • Column-count mismatch → parse_error
  • No BOM stripping
  • TSV / other delimiters are unsupported

Common errors

SituationCode
Empty fileparse_error
Unsupported extensionparse_error (details.format)
Missing filefile_not_found
Remote URLload_failed
Over 5MBdata_source_too_large

Behavior in build

dataSources is loaded once before the entry loop and shared across all entries.

await build({
  input: './pages/**/*.md',
  outDir: './out',
  dataSources: { config: './data/config.yaml' },
})

See also