作例

書いた文書が、HTMLになる。

Matraの入口、JavaScript値、HTML rendererをひとつの短い例で確認できます。掲載しているsourceはwebsite buildでも実行し、右側には実際のrenderer出力を表示しています。

サンプルはwebsite buildで実行されます。コードのJavaScript値を信頼できる環境でのみ実行してください。

実行されるサンプル

JavaScript関数と値からHTML文書を生成

この例をNode.jsで再現する手順は、導入ページにあります。

左は入力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>

INTERACTIVE DEMOS

値を変えて、図と数式を見る。

スライダーや入力欄を操作すると、固定のMatra文書を再実行し、表示とソースを更新します。

Core/HTML v0.3.0の文書APIから、ブラウザ標準のSVG・MathMLを生成します。専用の@matra/graphics・@matra/math APIや数式の記号計算ではありません。任意コードの実行や外部module importは提供しません。

SVG:円を並べる

円の個数と大きさを変えて、繰り返しと回転によるパターンを試します。

8
16
SVG:円を並べる

初期結果を表示しています。操作にはJavaScriptを有効にしてください。

実行したMatraソースを見る
let count = 8;
let size = 16;
let indices = [0,1,2,3,4,5,6,7];
svg(viewBox="0 0 320 320" role="img" aria-label="SVG:円を並べる") {
  title { "SVG:円を並べる" }
  circle(cx=160 cy=160 r=80 fill="none" stroke="#d8e2ef") {}
  ...for (i in indices) {
    g(transform="rotate(" + (i * 360 / count) + " 160 160)") {
      circle(cx=160 cy=80 r=size fill="#006fe5" fill-opacity=0.65) {}
    }
  }
}

MathML:a × x²

係数aと入力xを変え、数式の組版と数値結果を確認します。計算はCoreの基本算術です。

2
3
2×(3)2=18

初期結果を表示しています。操作にはJavaScriptを有効にしてください。

実行したMatraソースを見る
let a = 2;
let x = 3;
let result = a * x * x;
math(display="block") {
  mrow {
    mn { a }
    mo { "×" }
    msup {
      mrow {
        mo { "(" }
        mn { x }
        mo { ")" }
      }
      mn { 2 }
    }
    mo { "=" }
    mn { result }
  }
}

次に見るもの

境界を知ってから、広げる。

01

HTML page

Matraで文書構造を、JavaScriptでデータや関数を記述し、HTMLを生成します。

導入手順 →

03

Webでの編集

HTML限定のPlaygroundでMatra文書を編集・実行できます。Workerの停止と10秒timeoutに対応しています。

Playgroundを試す →

再現性

配布packageをversion固定して利用する。

npm install --save-exact @matra/[email protected] @matra/[email protected]

導入方法