MATRA / EXPERIMENTAL STRUCTURAL LANGUAGE

文書をコードで組み立てる。

Matraは、文書・データ・プログラムを共通構造で扱うことを目指す実験的な構造言語です。現在のv0.3では、文書構造を記述し、JavaScriptの値と接続し、文書ASTからHTMLを生成する経路を試せます。技術書典の技術書で設計に触れた方は、まずCLIで動かし、ソースと出力を比較しながら仕様・実装へ進んでください。

Core v0.3.1 / HTML v0.3.1。Node.js向けの実験版です。

実験版CLI。Node.js 22/24とnpmが必要です。

npm install --global @matra/[email protected]

SOURCE → DOCUMENT AST → HTML

ソースと生成結果を比較する

このサイト自身もMatraで生成しています。以下はbuildで実行するソースと実際のHTML出力です。Markdownの名前付きコードfenceを入口に、JavaScriptの値を参照して文書を評価し、文書ASTをHTML rendererへ渡します。

左は入力Markdown文書の全体です。matra.entryがmain.matraを選び、matra.valuesのJavaScript式をcontentとして渡します。@matra/coreが文書を評価し、@matra/site-generationがページ・共通layout・データからサイトを生成します。CLIは新規プロジェクトの作成とbuild/previewに使います。下のコードは各packageの利用例です。

MARKDOWNexample.md
---
matra:
  entry: main.matra
  values:
    - content.js.part
---

# Compose a page with Matra packages

```main.matra
html(lang="en") {
  head {
    title { content.pageTitle(content.title) }
  }
  body {
    header {
      p { "MATRA / PACKAGE EXAMPLES" }
      h1 { content.title }
      p { content.description }
    }
    main {
      ...for (item in content.packages) {
        section(id=item.id) {
          h2 { item.name }
          p { item.description }
          pre { code { item.example } }
        }
      }
    }
    footer {
      p { "These package workflows are experimental; check each package's release notes." }
    }
  }
}
```

```content.js.part
({
  title: "Compose a page with Matra packages",
  description: "Use the CLI to start a project, Core to evaluate Matra documents, and site-generation to build a site.",
  pageTitle: title => `${title} | Matra`,
  packages: [
    {
      id: "cli",
      name: "@matra/cli",
      description: "Create and preview a starter project with the experimental CLI.",
      example: "npm install --global @matra/[email protected]\nmatra init hello\ncd hello && matra dev"
    },
    {
      id: "core",
      name: "@matra/core",
      description: "Build and run a literate Matra document in Node.js.",
      example: 'const value = await runLiterate(await buildLiterate(source))'
    },
    {
      id: "site-generation",
      name: "@matra/site-generation",
      description: "Generate pages from Matra templates, page data, and a shared layout.",
      example: 'await generateSite({ root, output: "dist", pages })'
    }
  ]
})
```
HTMLgenerated output
<html lang="en">
  <head>
    <title>Compose a page with Matra packages | Matra</title>
  </head>
  <body>
    <header>
      <p>MATRA / PACKAGE EXAMPLES</p>
      <h1>Compose a page with Matra packages</h1>
      <p>Use the CLI to start a project, Core to evaluate Matra documents, and site-generation to build a site.</p>
    </header>
    <main>
      <section id="cli">
        <h2>@matra/cli</h2>
        <p>Create and preview a starter project with the experimental CLI.</p>
        <pre><code>npm install --global @matra/[email protected]
matra init hello
cd hello &amp;&amp; matra dev</code></pre>
      </section>
      <section id="core">
        <h2>@matra/core</h2>
        <p>Build and run a literate Matra document in Node.js.</p>
        <pre><code>const value = await runLiterate(await buildLiterate(source))</code></pre>
      </section>
      <section id="site-generation">
        <h2>@matra/site-generation</h2>
        <p>Generate pages from Matra templates, page data, and a shared layout.</p>
        <pre><code>await generateSite({ root, output: &quot;dist&quot;, pages })</code></pre>
      </section>
    </main>
    <footer>
      <p>These package workflows are experimental; check each package&#39;s release notes.</p>
    </footer>
  </body>
</html>

技術書の、その先へ

動かす。構造を読む。実装を追う。

開発状況

実装済みの経路と、実験版の契約。

CLIとNode.js向けliterate APIは実験版です。buildは文書内のJavaScriptを実行するため、信頼するsourceだけを使ってください。PlaygroundはHTML限定、SVG・MathMLはCore/HTMLによる限定デモです。専用Graphics/Math APIや汎用sandboxを提供するものではありません。

導入手順と公開API契約