@cosense-toolbox/tailwind 0.1.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 qaynam
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,167 @@
1
+ # @cosense-toolbox/tailwind
2
+
3
+ Tailwind CSS のプラグイン。`class="cosense"` を付けた要素の中に、Cosense (旧 Scrapbox) の記法を描画した HTML の既定の見た目を当てる。
4
+ typography プラグインの `prose` と同じ使い方をする。
5
+
6
+ 当てるスタイルは [`@cosense-toolbox/style`](../style) と同じもの。
7
+ Tailwind を使っているなら、style.css を別に読み込まなくてよい。
8
+
9
+ > **beta**:class 名や出力はまだ変わりうる。
10
+
11
+ ## インストール
12
+
13
+ ```sh
14
+ npm install -D @cosense-toolbox/tailwind@beta
15
+ ```
16
+
17
+ Tailwind v4 の CSS に `@plugin` で足す。
18
+
19
+ ```css
20
+ @import "tailwindcss";
21
+ @plugin "@cosense-toolbox/tailwind";
22
+ ```
23
+
24
+ ## 基本の使い方
25
+
26
+ 描画した HTML を `class="cosense"` の要素で包む。
27
+
28
+ ```astro
29
+ <article class="cosense">
30
+ <Content />
31
+ </article>
32
+ ```
33
+
34
+ `@cosense-toolbox/parser` の `toHtml` の出力も同じように包めばよい。
35
+
36
+ ```astro
37
+ <article class="cosense" set:html={toHtml(page)} />
38
+ ```
39
+
40
+ ## 要素ごとに調整する
41
+
42
+ `cosense-{記法}:{utility}` の modifier で、記事の中の特定の記法にだけ utility を当てられる。
43
+ 包んでいる要素に付けるので、記事の HTML には手を入れなくてよい。
44
+
45
+ ```html
46
+ <article
47
+ class="cosense cosense-link:text-sky-700 cosense-link:hover:underline cosense-code:bg-slate-100 cosense-title:text-4xl"
48
+ >
49
+ …
50
+ </article>
51
+ ```
52
+
53
+ `cosense-link:hover:underline` のように続けて書くと、リンクを hover したときだけ当たる。
54
+ `md:cosense-title:text-5xl` や `dark:cosense-link:text-sky-300` のように、画面幅やダークモードの variant とも組み合わせられる。
55
+
56
+ | Modifier | Target | 記法 |
57
+ | :-------------------------------- | :----------------------------- | :---------------------------------------------------------- |
58
+ | `cosense-title:{utility}` | `.title` | タイトル (1 行目) |
59
+ | `cosense-line:{utility}` | `.line` | 各行 |
60
+ | `cosense-heading:{utility}` | `.decoration[data-size-level]` | `[** 見出し]` などの大きな文字 |
61
+ | `cosense-strong:{utility}` | `strong` | `[[太字]]` `[* 太字]` |
62
+ | `cosense-em:{utility}` | `em` | `[/ 斜体]` |
63
+ | `cosense-s:{utility}` | `s` | `[- 打ち消し]` |
64
+ | `cosense-u:{utility}` | `u` | `[_ 下線]` |
65
+ | `cosense-link:{utility}` | `.link` | リンクすべて (`[ページ]` `[https://…]` `[/project/ページ]`) |
66
+ | `cosense-link-external:{utility}` | `.link-external` | 外部リンク `[https://…]` |
67
+ | `cosense-link-project:{utility}` | `.link-project` | 別プロジェクトへのリンク `[/project/ページ]` |
68
+ | `cosense-hashtag:{utility}` | `.hashtag` | `#タグ` |
69
+ | `cosense-code:{utility}` | `.code` | `` `コード` `` |
70
+ | `cosense-code-block:{utility}` | `.code-block > code` | `code:ファイル名` のブロックの各行 |
71
+ | `cosense-code-filename:{utility}` | `.code-block-start` | `code:ファイル名` のファイル名 |
72
+ | `cosense-quote:{utility}` | `.quote` | `>` で始まる引用 |
73
+ | `cosense-table:{utility}` | `.table` | `table:名前` の表 |
74
+ | `cosense-td:{utility}` | `.table td` | 表のセル |
75
+ | `cosense-image:{utility}` | `.image` | 画像 |
76
+ | `cosense-icon:{utility}` | `.icon` | `[ユーザー名.icon]` |
77
+ | `cosense-formula:{utility}` | `.formula` | `[$ 数式]` |
78
+
79
+ 対象は HTML の要素名ではなく、Cosense の記法で分けている。`toHtml` はリンク・タグ・アイコンのどれにも同じ `<a>` を使うため、要素名では区別できない。
80
+ class 名は `toHtml` の既定 (`classNames` を渡さなかったとき) に合わせている。
81
+
82
+ ### 装飾の記号ごとに調整する
83
+
84
+ `cosense-deco-[記号]:{utility}` で、その記号の装飾 (`[| 文字]` なら `|`) だけに utility を当てられる。
85
+ `toHtml` は装飾を `<span class="decoration deco-|">` のように記号ごとの class で出すので、それを選ぶ。
86
+
87
+ ```html
88
+ <article
89
+ class="cosense cosense-deco-[|]:border-l-4 cosense-deco-[|]:pl-3 cosense-deco-[|]:text-slate-600"
90
+ ></article>
91
+ ```
92
+
93
+ - 記号を並べると、それらをすべて持つ装飾に当たる。`cosense-deco-[-/]:` は `[-/ 文字]` など
94
+ - 下線の記号 `_` は `cosense-deco-[\_]:` と書く。Tailwind は `[...]` の中の `_` を空白に変えるため
95
+ - `"` は class 属性の中に書けないので使えない
96
+ - パーサーが既定で装飾として読む記号は `*` `/` `-` `_` の 4 つだけ。`|` などほかの記号の装飾を使うときは、パースのときに [`customDecorations`](https://cosense-toolbox.qaynam.dev/parser/extend/) で記号を足す
97
+
98
+ ```js
99
+ // astro.config.mjs (@cosense-toolbox/astro)
100
+ import { customDecorations } from "@cosense-toolbox/parser/extensions"
101
+
102
+ cosense({ parseOptions: { extensions: [customDecorations(["|"])] } })
103
+ ```
104
+
105
+ ## 一部だけ外す
106
+
107
+ `not-cosense` を付けた要素とその中身には、既定のスタイルも modifier も当てない。`.csnx` に埋め込んだコンポーネントを、ページの見た目から切り離したいときに使う。
108
+
109
+ ```svelte
110
+ <aside class="not-cosense">…</aside>
111
+ ```
112
+
113
+ ## 色や寸法を変える
114
+
115
+ 1 つの記法だけを変えるなら modifier、ページ全体の配色や文字の大きさを変えるなら CSS 変数を使う。
116
+ 変数は style.css と同じもので、一覧は [`@cosense-toolbox/style` の README](../style/README.md#色を変える) にある。
117
+
118
+ ```html
119
+ <article class="cosense [--cosense-link:var(--color-sky-700)] [--cosense-font-size:16px]"></article>
120
+ ```
121
+
122
+ ダークモードでは、`dark:` の variant で変数を差し替える。
123
+
124
+ ```html
125
+ <article
126
+ class="cosense dark:[--cosense-text:var(--color-slate-200)] dark:[--cosense-bg:var(--color-slate-900)]"
127
+ ></article>
128
+ ```
129
+
130
+ ## オプション
131
+
132
+ ```css
133
+ @plugin "@cosense-toolbox/tailwind" {
134
+ classname: article;
135
+ }
136
+ ```
137
+
138
+ | オプション | 既定 | 内容 |
139
+ | :---------- | :-------- | :----------------------------------------------------------------------------------------------------------------------------------- |
140
+ | `className` | `cosense` | スタイルを当てる class 名。除外の class 名は `not-{className}`、modifier は `{className}-link:` `{className}-deco-[…]:` のようになる |
141
+
142
+ ## style.css との違い
143
+
144
+ - style.css の `.page` を `.cosense` に置き換えている。宣言はすべて同じ
145
+ - 各ルールのセレクタを `:where()` で包み、どのルールも `.cosense` 1 つぶんの詳細度 (0,1,0) にしている。typography と同じ作り
146
+ - 包むと元の詳細度の差が無くなるので、ルールは元の詳細度の低い順 (同じなら style.css に書いた順) に並べ直して出す。同じ詳細度のルールどうしは後ろに書いたものが勝つので、どの要素でも style.css と同じルールが勝つ
147
+ - modifier のルールは既定のスタイルより後ろに、同じ詳細度で出るので、既定のスタイルに勝つ
148
+ - 各ルールに `:not(:where([class~="not-cosense"], [class~="not-cosense"] *))` が付く。`:where()` の中なので詳細度は増えない
149
+
150
+ ## 開発
151
+
152
+ スタイルの正は `packages/style/style.css`。このパッケージの `src/styles.generated.ts` は、そこから機械的に作ったもの。
153
+
154
+ ```sh
155
+ bun run generate # style.css を直したら作り直す
156
+ bun run test # 作り直し忘れもここで落ちる
157
+ bun run compare # style.css とプラグインで、ブラウザの計算済みスタイルが一致するかを確かめる
158
+ ```
159
+
160
+ `bun run compare` は、parser の記法仕様のページをすべて描画し、style.css で表示したものとプラグインで表示したものの全要素のスタイルを突き合わせる。あわせて、modifier がどれも対象の要素で既定のスタイルに勝つことを確かめる。
161
+ Chromium が要るので、`npx playwright install chromium` で入れるか、手元の Chromium を `CHROMIUM_PATH` で渡す。
162
+
163
+ `examples/astro-blog` はこのパッケージの `dist/` を読む。プラグインを直したら `bun run build` してから example を動かす。作り直さないと、新しい modifier は Tailwind に知られていない variant として黙って捨てられる。
164
+
165
+ ## ライセンス
166
+
167
+ MIT。