Skip to content

D2記法のコードブロックに対応 - #185

Open
riiimparm wants to merge 10 commits into
developfrom
feature/d2-codeblock
Open

D2記法のコードブロックに対応#185
riiimparm wants to merge 10 commits into
developfrom
feature/d2-codeblock

Conversation

@riiimparm

@riiimparm riiimparm commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

変更内容

D2記法のコードブロックに対応。

  • @terrastruct/d2をCDNから動的importし、クライアントサイドでd2コードブロックをSVGに変換。配色はmermaidと同じダークテーマ
  • package.json/pnpm-lock.yaml: @terrastruct/d2を追加

横長図
縦長図

テスト用md
---
title: D2コードブロックのサイズ調整検証用デモ
description: 'D2コードブロックのサイズ調整検証用の記事です。'
author: 'RiiiM'
tags: [Tech]
pubDate: '2026-08-13'
---

D2コードブロックのサイズ調整検証用の記事です。デスクトップ幅では横幅80vw・縦幅90vhの制約でアスペクト比を保ったまま表示されることを確認します。

## 横長のD2図(幅80vw基準で収まるケース)

```d2
a -> b: 1
a -> c: 2
a -> d: 3
a -> e: 4
a -> f: 5
a -> g: 6
a -> h: 7
a -> i: 8
a -> j: 9
a -> k: 10
```

## 縦長のD2図(高さ90vh制約でスケールダウンされるケース)

```d2
a -> b
b -> c
c -> d
d -> e
e -> f
f -> g
g -> h
h -> i
i -> j
j -> k
```

## コンテナ(ネスト)の例

```d2
cell tower: {
  satellites: {
    shape: package
  }
  satellites -> transmitter
}

online portal: {
  ui: {
    shape: hexagon
  }
}

data processor: {
  storage: {
    shape: cylinder
    style.multiple: true
  }
}

cell tower.satellites -> data processor.storage: transmit
online portal.ui -> data processor.storage
```

## 多様なシェイプ+スタイルの例

```d2
person: {
  shape: person
}
cloud: {
  shape: cloud
}
queue: {
  shape: queue
}
db: {
  shape: cylinder
  style.multiple: true
}
important: {
  shape: hexagon
  style.fill: "#38bdf8"
  style.stroke: "#0f172a"
  style.shadow: true
}
flagged: {
  style.fill: "#22d3ee"
  style.double-border: true
}
cube: {
  label: 3D Box
  style.3d: true
  style.fill: "#38bdf8"
}

person -> cloud
cloud -> queue
queue -> db
db -> important
important -> flagged
flagged -> cube
```

## 接続線のスタイルバリエーションの例

```d2
a -> b: normal
a -> c: dashed {
  style.stroke-dash: 4
}
a -> d: thick {
  style.stroke-width: 5
}
a <-> e: bidirectional
```

@terrastruct/d2 (ブラウザ向けWASM実装) をCDN経由で動的importし、
mermaidと同じパターンでd2コードブロックをSVGに変換するremarkプラグインを追加。

Shikiが"d2"言語を認識できずplaintextにフォールバックしてしまうため、
markdown.syntaxHighlight.excludeLangsでd2のみシンタックスハイライトを無効化。
コンテナにmax-width:80vw、SVGにwidth/height:auto + max-width:100%/max-height:90vhを
指定し、アスペクト比を保ったまま画面内に収まるようCSSのみで制約(img的なreplaced
element縮小に任せる)。1024px未満(モバイル)は従来通り制約なし。
横長/縦長のD2図を含む再現用記事。デスクトップ幅での80vw/90vh制約の
証跡として残す。
@terrastruct/d2のJS APIはthemeOverrides的なAPIを持たず、themeIDでは
mermaid側の配色(背景#0f172a/ボーダー#38bdf8/ライン#22d3ee/文字#e2e8f0)に
近いものが無かったため、D2のテーマシステムが付与する意味的クラス
(N1-N7, B1-B6, AA/AB1-5)をCSSで直接上書きしてmermaidと同じ配色にした。

また、高さ制約(max-height:90vh基準のスケールダウン)をデスクトップ限定の
メディアクエリの外に出し、モバイルでも常時適用されるようにした
(幅80vwの制約は引き続きデスクトップのみ)。

コンテナはmermaidと同様にflex+justify-content:centerで中央揃えにし、
Tailwindクラス文字列(purge対象外で効かない可能性がある)からinline
style指定に変更した。
connectionのfill-B1(矢印先端マーカーのpolygon)とstroke-B1(線本体の
path、fill="none")を同一セレクタでまとめてfill/strokeを両方
!important上書きしていたため、線本体のfill="none"が上書きされ、
閉じていないbezierパスがブラウザにより暗黙的に閉じられて塗りつぶされ、
扇状に複数エッジが出る図で特に目立つ太いリボン状の見た目になっていた。
fill-B1とstroke-B1を別々のセレクタに分離し、それぞれの役割(マーカー
の塗り/線の色)にのみ対応するプロパティを当てるよう修正。
@riiimparm riiimparm linked an issue Aug 13, 2026 that may be closed by this pull request
@riiimparm riiimparm self-assigned this Aug 14, 2026
@riiimparm
riiimparm requested a review from naoki-00-ito August 14, 2026 16:28

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riiimparm
style指定を src/layouts/BlogLayout.astro に寄せれないかな
当たらなそうだったらこのままでもOK!

@riiimparm riiimparm Aug 22, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d2のあるページだけにstyle追加したいのでBlogLayoutには入れないかな。後述コメにてクライアントレンダリングやめるからstyle変わるかも
mermaidと統合したほうがいいのもそうだなぁ、BlogLayoutで統合できるかみてみる


try {
const { D2 } = await import(
"https://esm.sh/@terrastruct/d2@0.1.33"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cdnでなく、package.jsonで管理している @terrastruct/d2 使えないかな?

Suggested change
"https://esm.sh/@terrastruct/d2@0.1.33"
"@terrastruct/d2"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@terrastruct/d2による事前レンダリングに変更

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mermaidもcdnなのでテーマ合わせる意味でもこのブランチで一緒に直しちゃったほうがいいか

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

D2対応

2 participants