rehype プラグイン
new@love-rox/ptpt-rehype
印を付けた Markdown / HTML をスプリットフラップ盤面に変換する rehype プラグイン + クライアント hydrator。build 時に data-ptpt-* 付きの読めるテキストを出力し、hydrate がブラウザでアニメーションさせます。JS がなくても文字は読めます。
Install
インストール
@love-rox/ptpt-core はピア依存です(クライアント hydrator が利用します)。unified ベースのビルド(remark / rehype / Astro / eleventy など)で使えます。Astro 専用に最適化された API が必要な場合は Astro 版 を使ってください。
bun
bun add @love-rox/ptpt-rehypepnpm
pnpm add @love-rox/ptpt-rehypenpm
npm i @love-rox/ptpt-rehypeyarn
yarn add @love-rox/ptpt-rehypeUsage
Markdown パイプラインに組み込む
rehypePlugins に rehypePtpt を追加します。認識される入力は2種類——マーカークラスを付けたインライン要素(<span class="patapata">HELLO</span>)と、 `patapata でタグ付けしたフェンス(1行が盤面1行)です。あとは hydrate() が見つけたものをすべて動かします。
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeRaw from "rehype-raw";
import rehypeStringify from "rehype-stringify";
import rehypePtpt from "@love-rox/ptpt-rehype";
// Markdown marks a board with the marker class (or a ```patapata fence):
const md = '<span class="patapata" data-preset="alphanumeric">TOKYO</span>';
const html = String(
await unified()
.use(remarkParse)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypePtpt)
.use(rehypeStringify)
.process(md),
);
// <span class="ptpt" data-ptpt-targets="TOKYO" data-ptpt-preset="alphanumeric">TOKYO</span>クライアントで hydrate する
プラグインは静的なマークアップを出力するだけです。ブラウザで hydrate を一度呼べば、出力された [data-ptpt-targets] をすべてアニメーション化します:
// Browser entry — animate everything the plugin emitted.
import { hydrate } from "@love-rox/ptpt-rehype/client";
import "@love-rox/ptpt-core/styles.css";
// scans the document for [data-ptpt-targets] and flips them into boards
hydrate();Options
オプション
プラグインは className / fenceLanguage / defaultPreset を受け取ります。下記の共通セルオプションは盤面ごとに data-ptpt-* 属性から読まれ、React / Vue / Astro と同じ意味で動きます。
共通セルオプション(data-ptpt-* 経由)
| オプション | 型 | 既定値 | 説明 |
|---|---|---|---|
| preset | PresetDefinition | string | defaultPreset | フラップに使う文字セット。PresetDefinition(recommendedDuration / locale を既定値に使う)か、フラップ順を表す文字列を渡します。frames とは排他です。 |
| frames | FrameInput[] | undefined | 明示的なフレーム集合。文字だけでなく画像や任意要素も指定できます。preset とは排他です。 |
| durationPerFlap | number | プリセットの推奨値、なければ 200 | 1 枚をめくるミリ秒数。プリセットの recommendedDuration を上書きします。 |
| direction | 'forward' | 'shortest' | 'forward' | フレームのリングをめくる向き。shortest は近いほうへ逆回りすることもあります。 |
| reducedMotion | 'instant' | 'minimal' | 'ignore' | 'instant' | OS が「視差効果を減らす」設定のときの挙動。instant は瞬時に切り替えます。 |
rehype プラグインオプション
| オプション | 型 | 既定値 | 説明 |
|---|---|---|---|
| className | string | 'patapata' | インライン要素を盤面に変換する目印となるクラス名。 |
| fenceLanguage | string | 'patapata' | 盤面に変換するフェンスコードブロックのコードフェンス言語。 |
| defaultPreset | string | undefined | 要素/ブロックがプリセットを指定しないときに使うプリセット名。 |
Behaviour
挙動の注記
- プログレッシブです。build はプレーンで読めるテキストを出力するので、JS がなくても内容は残ります。
hydrateはアニメーションを足すだけです。 - インラインのマーカーは
<span class="ptpt" data-ptpt-targets="…">になり、`patapataブロックは1行ごとの盤面行になります。 - マーカーに付けた
data-preset/data-duration/data-directionは、出力のdata-ptpt-*にそのまま引き継がれます。 - 選んだプリセットは対象文字をすべて含んでいる必要があります。含まないとその盤面は空欄になります——混在テキストには
default/alphanumericを選んでください。
Related
Astro を使っているなら
Astro 4+ で .md / .mdx / .astro を扱うなら、このプラグインの登録・スタイルシートの注入・hydrator の実行をまとめて行い、.astro 用の <Patapata> コンポーネントも同梱した Astro インテグレーション が便利です。