note-blog-kit 0.0.0-stage → 0.1.0

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 ludy-kida
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 CHANGED
@@ -1,3 +1,226 @@
1
- # Temporary Holding Version
1
+ # note-blog-kit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 自分の note 記事を、React + TypeScript のサイトに数十行でブログとして載せるためのライブラリ(開発中・仮称)。
4
+
5
+ ## 注意事項
6
+
7
+ - note の**非公式 API** を利用しており、予告なく動かなくなる可能性があります
8
+ - note 株式会社とは無関係のライブラリです
9
+ - **自分の記事**を表示する用途を想定しています(他人の記事の転載はしないでください)
10
+ - キャッシュを有効にし、高頻度なアクセスはしないでください
11
+ - canonical は note の元 URL を推奨します
12
+ - 有料部分は表示しません
13
+
14
+ ## 導入手順(Next.js App Router)
15
+
16
+ ### 1. インストール
17
+
18
+ ```bash
19
+ npm install note-blog-kit
20
+ ```
21
+
22
+ React のコンポーネント(`note-blog-kit/react`)を使う場合は React 18 / 19 が必要です。取得(`note-blog-kit/server`)は Node.js 20 以上で動きます。
23
+
24
+ ### 2. デフォルト CSS を読み込む(任意)
25
+
26
+ ```tsx
27
+ // app/layout.tsx
28
+ import 'note-blog-kit/styles.css';
29
+ ```
30
+
31
+ ### 3. 一覧ページを作る
32
+
33
+ ```tsx
34
+ // app/blog/page.tsx
35
+ import { unstable_cache } from 'next/cache';
36
+ import { notFound } from 'next/navigation';
37
+ import { NoteArticleList, NotePagination } from 'note-blog-kit/react';
38
+ import { listArticles } from 'note-blog-kit/server';
39
+
40
+ // 取得結果を1時間キャッシュする(キャッシュに当たれば note へのリクエストも、ライブラリの間隔待ちも起きない)。
41
+ // ユーザー名は引数にしてキャッシュのキーに含める(変えたときに前のユーザーの記事が出ないように)
42
+ const getPage = unstable_cache(
43
+ (user: string, page: number) => listArticles({ user, page }),
44
+ ['note-articles'],
45
+ { revalidate: 3600 },
46
+ );
47
+
48
+ type Props = { searchParams: Promise<{ page?: string }> };
49
+
50
+ export default async function BlogPage({ searchParams }: Props) {
51
+ const page = Number((await searchParams).page ?? '1');
52
+ if (!Number.isInteger(page) || page < 1 || page > 100) notFound();
53
+ const { articles, isLastPage, totalCount } = await getPage('your_urlname', page);
54
+ if (articles.length === 0 && page > 1) notFound();
55
+ return (
56
+ <main>
57
+ <h1>ブログ</h1>
58
+ <NoteArticleList articles={articles} />
59
+ <NotePagination page={page} isLastPage={isLastPage} totalCount={totalCount} getHref={(p) => `/blog?page=${p}`} />
60
+ </main>
61
+ );
62
+ }
63
+ ```
64
+
65
+ 記事のリンク先は、既定では note の記事ページです。自分のサイトの詳細ページにしたい場合は `getHref` と `linkComponent={Link}` を渡します(詳細ページの作り方は今後のリリースで追加します)。
66
+
67
+ ### キャッシュの推奨
68
+
69
+ - `listArticles` は呼ぶたびに note へリクエストします。**呼び出しの結果ごとキャッシュしてください**(Next.js なら上の例の `unstable_cache`、ほかの環境ではビルド時に JSON へ書き出すなど)
70
+ - 同じプロセス内の連続リクエストは、ライブラリが自動で1秒以上空けます。この待ちは `listArticles` を呼ぶたびに入るため、`fetch` 単位のキャッシュ(`next: { revalidate }`)だけでは、同時アクセスが多いとページの表示が遅れます
71
+ - **ページ番号に上限を設けてください**(上の例では100)。上限がないと、`?page=` を変えたアクセスでキャッシュに無いページを取りに行かせ続けられます
72
+ - 全件の取得(`listAllArticles`)は1ページ6件ずつ取りに行くため時間がかかります。ビルド時やキャッシュの更新時に行ってください
73
+
74
+ 動く例は [`examples/next`](https://github.com/ludy-kida/note-blog-kit/tree/main/examples/next) にあります。
75
+
76
+ ## 使い方(開発中・API は 0.x の間に変わることがあります)
77
+
78
+ ### 記事一覧を取得する(サーバー側)
79
+
80
+ note は CORS に対応していないため、取得はサーバー側(Next.js の Server Component・Route Handler、ビルド時のスクリプトなど)で行います。
81
+
82
+ ```ts
83
+ import { isNoteError, listArticles } from 'note-blog-kit/server';
84
+
85
+ try {
86
+ const { articles, isLastPage, totalCount } = await listArticles({ user: 'your_urlname', page: 1 });
87
+ } catch (error) {
88
+ if (isNoteError(error) && error.code === 'NOTE_NOT_FOUND') {
89
+ // ユーザーが見つからない
90
+ }
91
+ throw error;
92
+ }
93
+ ```
94
+
95
+ | オプション | 既定値 | 説明 |
96
+ | --- | --- | --- |
97
+ | `user` | (必須) | note のユーザー名(英数字・`_`・`-`) |
98
+ | `page` | `1` | ページ番号。1ページは6件 |
99
+ | `timeoutMs` | `10000` | タイムアウト(ミリ秒) |
100
+ | `minIntervalMs` | `1000` | 連続リクエストの最小間隔(ミリ秒)。プロセス全体で1本の列にまとめて送ります |
101
+ | `signal` | — | 中断用の `AbortSignal` |
102
+ | `fetch` | `globalThis.fetch` | fetch の差し替え(テスト・キャッシュ層用)。Cookie・認証ヘッダーは付けないでください |
103
+
104
+ エラーは `code` で判別します:`NOTE_NOT_FOUND`(404)、`NOTE_HTTP_ERROR`(その他の HTTP エラー、`status` あり)、`NOTE_NETWORK_ERROR`、`NOTE_TIMEOUT`、`NOTE_SCHEMA_ERROR`(note のレスポンスの形が変わった)。
105
+
106
+ 結果は必ずキャッシュし、高頻度に呼び出さないでください(方法は上の「キャッシュの推奨」を参照)。
107
+
108
+ ### 全件を取得する(ビルド時向け)
109
+
110
+ 1ページは6件固定のため、全件の取得には多くのリクエストが必要です(237件なら約40回、1秒間隔で約40秒)。全件取得はビルド時やキャッシュの更新時に行ってください。既定では **20ページ(120件)で打ち切ります**。
111
+
112
+ ```ts
113
+ import { iterateArticles, listAllArticles } from 'note-blog-kit/server';
114
+
115
+ // 配列でまとめて取得する
116
+ const { articles, truncated } = await listAllArticles({ user: 'your_urlname', maxPages: 50 });
117
+ if (truncated) {
118
+ // maxPages に達して打ち切った(続きがある)
119
+ }
120
+
121
+ // 1件ずつ処理する
122
+ for await (const article of iterateArticles({ user: 'your_urlname' })) {
123
+ console.log(article.title);
124
+ }
125
+ ```
126
+
127
+ `maxPages`(既定 `20`)以外のオプションは `listArticles` と同じです(`page` は指定できません)。取得中に新しい記事が公開されてページがずれた場合も、同じ記事は1回だけ返します。
128
+
129
+ - `truncated: true` は `maxPages` に達して打ち切ったことを表します(続きがある可能性があります)
130
+ - `listAllArticles` は途中のページで失敗すると、それまでに集めた記事を返さずにエラーを投げます。途中までの記事も使いたい場合は `iterateArticles` を使ってください
131
+ - `for await` では打ち切りの有無を受け取れません。必要なら `listAllArticles` を使うか、`next()` を手で回して戻り値(`done: true` のときの `value`)を読んでください
132
+
133
+ ### 一覧を表示する(React)
134
+
135
+ `note-blog-kit/react` のコンポーネントは、取得済みのデータを受け取って描画するだけです。見た目は持たず、`nbk-` から始まるクラス名を付けます。フックを使わないため、Server Component としても使えます(React 18 / 19)。
136
+
137
+ ```tsx
138
+ import Link from 'next/link';
139
+ import { NoteArticleList, NotePagination } from 'note-blog-kit/react';
140
+ import { listArticles } from 'note-blog-kit/server';
141
+
142
+ export default async function BlogPage() {
143
+ const { articles, page, isLastPage, totalCount } = await listArticles({ user: 'your_urlname' });
144
+ return (
145
+ <>
146
+ <NoteArticleList articles={articles} linkComponent={Link} getHref={(article) => `/blog/${article.key}`} />
147
+ <NotePagination page={page} isLastPage={isLastPage} totalCount={totalCount} getHref={(p) => `/blog?page=${p}`} />
148
+ </>
149
+ );
150
+ }
151
+ ```
152
+
153
+ | コンポーネント | 主な props と既定値 |
154
+ | --- | --- |
155
+ | `NoteArticleList` | `articles`(必須)、`getHref`(既定は note の記事 URL)、`linkComponent`(既定 `'a'`)、`formatDate`(既定は日本時間の `YYYY/MM/DD`)、`headingLevel`(既定 `2`)、`renderItem`(各記事の描画を差し替え)、`empty`(既定「記事がありません」) |
156
+ | `NoteArticleCard` | `article`(必須)、`href`(既定は note の記事 URL)、`linkComponent`、`formatDate`、`headingLevel`、`labels.paid`(既定「有料」) |
157
+ | `NotePagination` | `page`・`isLastPage`・`getHref`(必須)、`totalCount`(あれば「3 / 40」と表示)、`perPage`(既定 `6`)、`linkComponent`、`labels`(既定「前へ」「次へ」、aria-label「ページ送り」) |
158
+
159
+ - **クラス名**:`nbk-card`(`__eyecatch`・`__title`・`__link`・`__date`・`__badge`・`__excerpt`・`__tags`・`__tag`)、`nbk-article-list`(`__item`、0件のときは `--empty` と `__empty`)、`nbk-pagination`(`__prev`・`__next`・`__status`)。`className` を渡すと、既定のクラス名に追加されます
160
+ - **リンク先に信頼できない値を渡さない**:`href`・`getHref` の値はそのまま `href` 属性に入ります。React 18 は `javascript:` の URL をブロックしません。既定値(note の記事 URL)は、http(s) でなければリンクにしません
161
+ - **Next.js の typedRoutes**:有効にしている場合は `Link` の `href` が文字列を受け付けないため、`Link` を包んだコンポーネントを `linkComponent` に渡してください
162
+ - **見出し画像**:note の CDN(assets.st-note.com)から直接読み込みます
163
+
164
+ ### デフォルト CSS(任意)
165
+
166
+ note の見た目に合わせた CSS を用意しています。読み込まなければ、コンポーネントは見た目を持ちません。
167
+
168
+ ```ts
169
+ // Next.js(App Router)なら app/layout.tsx、Pages Router なら pages/_app.tsx、Vite なら main.tsx などで読み込む
170
+ import 'note-blog-kit/styles.css';
171
+ ```
172
+
173
+ CSS は `:root`(変数の既定値)と `nbk-` から始まるクラスにだけ当たり、サイトの他の要素には影響しません。色や余白は CSS 変数で変えられます。
174
+
175
+ | 変数 | 既定値 | 用途 |
176
+ | --- | --- | --- |
177
+ | `--nbk-font-family` | Hiragino Sans など | 文字(サイトのフォントを使うなら `inherit`) |
178
+ | `--nbk-color-text` | `#08131a` | タイトルなど |
179
+ | `--nbk-color-muted` | `#6e7b85` | 日付・抜粋・タグ・件数 |
180
+ | `--nbk-color-accent` | `#41c9b4` | ホバー・フォーカス |
181
+ | `--nbk-color-border` | `#e6e6e6` | 枠線 |
182
+ | `--nbk-color-placeholder` | `#f2f2f2` | 見出し画像の読み込み中の下地 |
183
+ | `--nbk-color-surface` | `transparent` | カードの背景(既定はページの背景のまま) |
184
+ | `--nbk-color-badge-bg` / `--nbk-color-badge-text` | `#08131a` / `#fff` | 「有料」バッジ |
185
+ | `--nbk-radius` | `8px` | 角丸 |
186
+ | `--nbk-list-gap` | `24px` | 一覧のカードの間隔、ページ送りの上下の余白 |
187
+ | `--nbk-card-min-width` | `280px` | 一覧で1枚のカードが取る最小幅 |
188
+
189
+ #### 優先順位(カスケードレイヤー)
190
+
191
+ すべてのルールは `@layer note-blog-kit` の中にあります。
192
+
193
+ - **レイヤーに入っていないあなたの CSS** は、詳細度に関係なく note-blog-kit より優先されます。サイト全体の要素スタイル(例:`a { text-decoration: underline }`、`h2 { margin: ... }`)もカードに効くので、必要ならクラス名で打ち消してください
194
+ - **あなたがレイヤーを使っている場合**(Tailwind CSS v4 など)は、レイヤーの順序は最初に宣言された順で決まります。読み込む順番に頼らず、順序を宣言してください。Tailwind v4 なら、リセット(`base`)より後・ユーティリティ(`utilities`)より前に置くと、リセットで太字や余白が消えず、`className` に渡したユーティリティクラスが効きます
195
+
196
+ ```css
197
+ /* Tailwind v4 の例:グローバル CSS の先頭で順序を宣言する */
198
+ @layer theme, base, note-blog-kit, components, utilities;
199
+ @import "tailwindcss";
200
+ ```
201
+
202
+ ```css
203
+ /* ブランド色に合わせる */
204
+ :root {
205
+ --nbk-color-accent: #e5004f;
206
+ }
207
+
208
+ /* ダークモード(既定では配色を変えません) */
209
+ @media (prefers-color-scheme: dark) {
210
+ :root {
211
+ --nbk-color-text: #f1f1f1;
212
+ --nbk-color-muted: #a3adb4;
213
+ --nbk-color-border: #333;
214
+ --nbk-color-placeholder: #222;
215
+ }
216
+ }
217
+
218
+ /* 個別の調整はクラス名で(レイヤーに入れなければ詳細度を気にせず上書きできる) */
219
+ .nbk-card__title {
220
+ font-size: 1.125rem;
221
+ }
222
+ ```
223
+
224
+ ## ライセンス
225
+
226
+ MIT
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli/run.ts
4
+ import { parseArgs } from "util";
5
+ var USAGE = `\u4F7F\u3044\u65B9: note-blog-kit <command>
6
+
7
+ \u30B3\u30DE\u30F3\u30C9:
8
+ sync \u8A18\u4E8B\u3092\u53D6\u5F97\u3057\u3066 JSON \u306B\u66F8\u304D\u51FA\u3059\uFF08Phase 4 \u3067\u5B9F\u88C5\u4E88\u5B9A\uFF09`;
9
+ function run(argv) {
10
+ const { positionals, values } = parseArgs({
11
+ args: argv,
12
+ allowPositionals: true,
13
+ strict: false,
14
+ options: { help: { type: "boolean", short: "h" } }
15
+ });
16
+ const [command] = positionals;
17
+ if (values.help === true || command === void 0) {
18
+ console.log(USAGE);
19
+ return 0;
20
+ }
21
+ if (command === "sync") {
22
+ console.error("sync \u306F\u307E\u3060\u5B9F\u88C5\u3055\u308C\u3066\u3044\u307E\u305B\u3093\u3002");
23
+ return 1;
24
+ }
25
+ console.error(`\u4E0D\u660E\u306A\u30B3\u30DE\u30F3\u30C9\u3067\u3059: ${command}
26
+
27
+ ${USAGE}`);
28
+ return 2;
29
+ }
30
+
31
+ // src/cli/index.ts
32
+ process.exitCode = run(process.argv.slice(2));
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,75 @@
1
+ /**
2
+ * 公開型。note の生 JSON は公開せず、すべてこの型に正規化する。
3
+ * 確定の経緯は docs/phase0-findings.md を参照。変更は人の承認が必要。
4
+ */
5
+ /** 取得元 */
6
+ type NoteSource = 'api' | 'rss';
7
+ /** 記事の著者 */
8
+ interface NoteAuthor {
9
+ /** 表示名 */
10
+ name: string;
11
+ /** note.com/{urlname} の部分 */
12
+ urlname: string;
13
+ imageUrl: string | null;
14
+ }
15
+ /** 一覧に出す記事。API(v2 contents)と RSS のどちらからも作れる */
16
+ interface NoteArticleSummary {
17
+ /** 記事キー(例: n55256baa5425) */
18
+ key: string;
19
+ title: string;
20
+ /** note 上の記事 URL。canonical の既定値 */
21
+ url: string;
22
+ /** ISO 8601 */
23
+ publishedAt: string;
24
+ /** 本文先頭のプレーンテキスト */
25
+ excerpt: string;
26
+ eyecatchUrl: string | null;
27
+ /** 先頭の # を除いたハッシュタグ。RSS では常に空 */
28
+ hashtags: string[];
29
+ /** RSS では null */
30
+ likeCount: number | null;
31
+ /** 価格(円)。0 は無料。RSS では null */
32
+ price: number | null;
33
+ author: NoteAuthor;
34
+ source: NoteSource;
35
+ }
36
+ /** 見出し画像 */
37
+ interface NoteEyecatch {
38
+ url: string;
39
+ width: number | null;
40
+ height: number | null;
41
+ alt: string;
42
+ }
43
+ /** 目次の項目 */
44
+ interface NoteTocItem {
45
+ /** 本文中の見出しの id */
46
+ id: string;
47
+ text: string;
48
+ level: 2 | 3;
49
+ }
50
+ /** 本文中の埋め込み */
51
+ interface NoteEmbed {
52
+ key: string;
53
+ service: 'note' | 'external-article' | (string & {});
54
+ url: string;
55
+ }
56
+ /** 詳細ページの記事(v3 notes から作る) */
57
+ interface NoteArticle extends NoteArticleSummary {
58
+ /** サニタイズ済みの本文 HTML */
59
+ bodyHtml: string;
60
+ /** 有料部分があり、本文が途中までのとき true(「続きは note で」を出す) */
61
+ isPartial: boolean;
62
+ eyecatch: NoteEyecatch | null;
63
+ toc: NoteTocItem[];
64
+ embeds: NoteEmbed[];
65
+ }
66
+ /** 記事一覧の1ページ */
67
+ interface NoteArticlePage {
68
+ articles: NoteArticleSummary[];
69
+ page: number;
70
+ /** RSS では null */
71
+ totalCount: number | null;
72
+ isLastPage: boolean;
73
+ }
74
+
75
+ export type { NoteArticle, NoteArticlePage, NoteArticleSummary, NoteAuthor, NoteEmbed, NoteEyecatch, NoteSource, NoteTocItem };
@@ -0,0 +1,75 @@
1
+ /**
2
+ * 公開型。note の生 JSON は公開せず、すべてこの型に正規化する。
3
+ * 確定の経緯は docs/phase0-findings.md を参照。変更は人の承認が必要。
4
+ */
5
+ /** 取得元 */
6
+ type NoteSource = 'api' | 'rss';
7
+ /** 記事の著者 */
8
+ interface NoteAuthor {
9
+ /** 表示名 */
10
+ name: string;
11
+ /** note.com/{urlname} の部分 */
12
+ urlname: string;
13
+ imageUrl: string | null;
14
+ }
15
+ /** 一覧に出す記事。API(v2 contents)と RSS のどちらからも作れる */
16
+ interface NoteArticleSummary {
17
+ /** 記事キー(例: n55256baa5425) */
18
+ key: string;
19
+ title: string;
20
+ /** note 上の記事 URL。canonical の既定値 */
21
+ url: string;
22
+ /** ISO 8601 */
23
+ publishedAt: string;
24
+ /** 本文先頭のプレーンテキスト */
25
+ excerpt: string;
26
+ eyecatchUrl: string | null;
27
+ /** 先頭の # を除いたハッシュタグ。RSS では常に空 */
28
+ hashtags: string[];
29
+ /** RSS では null */
30
+ likeCount: number | null;
31
+ /** 価格(円)。0 は無料。RSS では null */
32
+ price: number | null;
33
+ author: NoteAuthor;
34
+ source: NoteSource;
35
+ }
36
+ /** 見出し画像 */
37
+ interface NoteEyecatch {
38
+ url: string;
39
+ width: number | null;
40
+ height: number | null;
41
+ alt: string;
42
+ }
43
+ /** 目次の項目 */
44
+ interface NoteTocItem {
45
+ /** 本文中の見出しの id */
46
+ id: string;
47
+ text: string;
48
+ level: 2 | 3;
49
+ }
50
+ /** 本文中の埋め込み */
51
+ interface NoteEmbed {
52
+ key: string;
53
+ service: 'note' | 'external-article' | (string & {});
54
+ url: string;
55
+ }
56
+ /** 詳細ページの記事(v3 notes から作る) */
57
+ interface NoteArticle extends NoteArticleSummary {
58
+ /** サニタイズ済みの本文 HTML */
59
+ bodyHtml: string;
60
+ /** 有料部分があり、本文が途中までのとき true(「続きは note で」を出す) */
61
+ isPartial: boolean;
62
+ eyecatch: NoteEyecatch | null;
63
+ toc: NoteTocItem[];
64
+ embeds: NoteEmbed[];
65
+ }
66
+ /** 記事一覧の1ページ */
67
+ interface NoteArticlePage {
68
+ articles: NoteArticleSummary[];
69
+ page: number;
70
+ /** RSS では null */
71
+ totalCount: number | null;
72
+ isLastPage: boolean;
73
+ }
74
+
75
+ export type { NoteArticle, NoteArticlePage, NoteArticleSummary, NoteAuthor, NoteEmbed, NoteEyecatch, NoteSource, NoteTocItem };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,2 @@
1
+
2
+ export { };
@@ -0,0 +1,2 @@
1
+
2
+ export { };
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,103 @@
1
+ 'use strict';
2
+
3
+ var jsxRuntime = require('react/jsx-runtime');
4
+
5
+ // src/react/class-names.ts
6
+ function classNames(...names) {
7
+ return names.filter((name) => typeof name === "string" && name !== "").join(" ");
8
+ }
9
+
10
+ // src/react/format-date.ts
11
+ var formatter = new Intl.DateTimeFormat("ja-JP", {
12
+ timeZone: "Asia/Tokyo",
13
+ year: "numeric",
14
+ month: "2-digit",
15
+ day: "2-digit"
16
+ });
17
+ function formatNoteDate(iso) {
18
+ const date = new Date(iso);
19
+ return Number.isNaN(date.getTime()) ? iso : formatter.format(date);
20
+ }
21
+
22
+ // src/react/safe-url.ts
23
+ function safeHttpUrl(url) {
24
+ if (typeof url !== "string" || !/^https?:\/\//i.test(url)) return null;
25
+ for (let i = 0; i < url.length; i += 1) {
26
+ const code = url.charCodeAt(i);
27
+ if (code <= 32 || code === 127) return null;
28
+ }
29
+ return url;
30
+ }
31
+ function NoteArticleCard({
32
+ article,
33
+ href,
34
+ linkComponent: Link = "a",
35
+ formatDate = formatNoteDate,
36
+ headingLevel = 2,
37
+ labels,
38
+ className
39
+ }) {
40
+ const Heading = `h${headingLevel}`;
41
+ const linkHref = href ?? safeHttpUrl(article.url);
42
+ const eyecatchUrl = safeHttpUrl(article.eyecatchUrl);
43
+ return /* @__PURE__ */ jsxRuntime.jsxs("article", { className: classNames("nbk-card", className), children: [
44
+ eyecatchUrl !== null && /* @__PURE__ */ jsxRuntime.jsx("img", { className: "nbk-card__eyecatch", src: eyecatchUrl, alt: "", loading: "lazy", decoding: "async" }),
45
+ /* @__PURE__ */ jsxRuntime.jsx(Heading, { className: "nbk-card__title", children: linkHref === null ? article.title : /* @__PURE__ */ jsxRuntime.jsx(Link, { href: linkHref, className: "nbk-card__link", children: article.title }) }),
46
+ /* @__PURE__ */ jsxRuntime.jsx("time", { className: "nbk-card__date", dateTime: article.publishedAt, children: formatDate(article.publishedAt) }),
47
+ article.price !== null && article.price > 0 && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "nbk-card__badge", children: labels?.paid ?? "\u6709\u6599" }),
48
+ article.excerpt !== "" && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "nbk-card__excerpt", children: article.excerpt }),
49
+ article.hashtags.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "nbk-card__tags", children: article.hashtags.map((tag) => /* @__PURE__ */ jsxRuntime.jsxs("li", { className: "nbk-card__tag", children: [
50
+ "#",
51
+ tag
52
+ ] }, tag)) })
53
+ ] });
54
+ }
55
+ function NoteArticleList({
56
+ articles,
57
+ getHref,
58
+ linkComponent,
59
+ formatDate,
60
+ headingLevel,
61
+ renderItem,
62
+ empty = "\u8A18\u4E8B\u304C\u3042\u308A\u307E\u305B\u3093",
63
+ className
64
+ }) {
65
+ if (articles.length === 0) {
66
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: classNames("nbk-article-list", "nbk-article-list--empty", className), children: /* @__PURE__ */ jsxRuntime.jsx("p", { className: "nbk-article-list__empty", children: empty }) });
67
+ }
68
+ const cardProps = {
69
+ ...linkComponent !== void 0 && { linkComponent },
70
+ ...formatDate !== void 0 && { formatDate },
71
+ ...headingLevel !== void 0 && { headingLevel }
72
+ };
73
+ return /* @__PURE__ */ jsxRuntime.jsx("ul", { className: classNames("nbk-article-list", className), children: articles.map((article) => /* @__PURE__ */ jsxRuntime.jsx("li", { className: "nbk-article-list__item", children: renderItem !== void 0 ? renderItem(article) : /* @__PURE__ */ jsxRuntime.jsx(
74
+ NoteArticleCard,
75
+ {
76
+ article,
77
+ ...getHref !== void 0 && { href: getHref(article) },
78
+ ...cardProps
79
+ }
80
+ ) }, article.key)) });
81
+ }
82
+ var NOTE_PER_PAGE = 6;
83
+ function NotePagination({
84
+ page,
85
+ isLastPage,
86
+ totalCount = null,
87
+ perPage = NOTE_PER_PAGE,
88
+ getHref,
89
+ linkComponent: Link = "a",
90
+ labels,
91
+ className
92
+ }) {
93
+ const totalPages = totalCount !== null && perPage > 0 ? Math.max(1, Math.ceil(totalCount / perPage)) : null;
94
+ return /* @__PURE__ */ jsxRuntime.jsxs("nav", { className: classNames("nbk-pagination", className), "aria-label": labels?.navigation ?? "\u30DA\u30FC\u30B8\u9001\u308A", children: [
95
+ page > 1 && /* @__PURE__ */ jsxRuntime.jsx(Link, { href: getHref(page - 1), className: "nbk-pagination__prev", children: labels?.previous ?? "\u524D\u3078" }),
96
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "nbk-pagination__status", "aria-current": "page", children: totalPages === null ? `${page}` : `${page} / ${totalPages}` }),
97
+ !isLastPage && /* @__PURE__ */ jsxRuntime.jsx(Link, { href: getHref(page + 1), className: "nbk-pagination__next", children: labels?.next ?? "\u6B21\u3078" })
98
+ ] });
99
+ }
100
+
101
+ exports.NoteArticleCard = NoteArticleCard;
102
+ exports.NoteArticleList = NoteArticleList;
103
+ exports.NotePagination = NotePagination;
@@ -0,0 +1,111 @@
1
+ import { NoteArticleSummary } from '../index.cjs';
2
+ export { NoteArticle, NoteArticlePage, NoteAuthor, NoteEmbed, NoteEyecatch, NoteSource, NoteTocItem } from '../index.cjs';
3
+ import { ElementType, ReactNode, ReactElement } from 'react';
4
+
5
+ /**
6
+ * リンクに使う要素。既定は `'a'`。
7
+ * `href`(文字列)・`className`・`children` を受け取るコンポーネント(Next.js の `Link` など)をそのまま渡せる。
8
+ *
9
+ * Next.js の typedRoutes を有効にしている場合は `Link` の `href` が文字列を受け付けないため、
10
+ * `({ href, ...props }) => <Link href={href as Route} {...props} />` のように包んだコンポーネントを渡す。
11
+ */
12
+ type NoteLinkComponent = ElementType<{
13
+ href: string;
14
+ className?: string;
15
+ children?: ReactNode;
16
+ }>;
17
+ /** 見出しのレベル(`h2`〜`h6`) */
18
+ type NoteHeadingLevel = 2 | 3 | 4 | 5 | 6;
19
+
20
+ /** {@link NoteArticleCard} の props */
21
+ interface NoteArticleCardProps {
22
+ /** 表示する記事(`listArticles` などで取得したもの) */
23
+ article: NoteArticleSummary;
24
+ /**
25
+ * リンク先。既定は `article.url`(note の記事 URL。http(s) でなければリンクにしない)。
26
+ * 利用者が渡す値はそのまま `href` に入るため、信頼できない値(`javascript:` など)を渡さないこと。
27
+ * React 18 はこうした値をブロックしない。
28
+ */
29
+ href?: string;
30
+ /** リンクに使う要素。既定は `'a'` */
31
+ linkComponent?: NoteLinkComponent;
32
+ /** 日付の表示。既定は Asia/Tokyo の `YYYY/MM/DD` */
33
+ formatDate?: (iso: string) => string;
34
+ /** タイトルの見出しレベル。既定は 2(`h2`)。一覧を `h2` の節の中に置くなら 3 にする */
35
+ headingLevel?: NoteHeadingLevel;
36
+ /** 文言の差し替え。既定は「有料」 */
37
+ labels?: {
38
+ paid?: ReactNode;
39
+ };
40
+ /** 既定のクラス名(`nbk-card`)に追加するクラス名 */
41
+ className?: string;
42
+ }
43
+ /**
44
+ * 記事1件のカード。見た目は持たず、`nbk-card` から始まるクラス名を付ける。
45
+ * フックを使わないため、Server Component としても使える。
46
+ */
47
+ declare function NoteArticleCard({ article, href, linkComponent: Link, formatDate, headingLevel, labels, className, }: NoteArticleCardProps): ReactElement;
48
+
49
+ /** {@link NoteArticleList} の props */
50
+ interface NoteArticleListProps {
51
+ /** 表示する記事の一覧(`listArticles` の `articles` など) */
52
+ articles: readonly NoteArticleSummary[];
53
+ /**
54
+ * 各記事のリンク先。既定は `article.url`(note の記事 URL)。
55
+ * 戻り値はそのまま `href` に入るため、信頼できない値(`javascript:` など)を返さないこと。
56
+ */
57
+ getHref?: (article: NoteArticleSummary) => string;
58
+ /** リンクに使う要素。既定は `'a'` */
59
+ linkComponent?: NoteLinkComponent;
60
+ /** 日付の表示。既定は Asia/Tokyo の `YYYY/MM/DD` */
61
+ formatDate?: (iso: string) => string;
62
+ /** 各カードのタイトルの見出しレベル。既定は 2 */
63
+ headingLevel?: NoteHeadingLevel;
64
+ /** 各記事の描画を丸ごと差し替える。既定は {@link NoteArticleCard} */
65
+ renderItem?: (article: NoteArticleSummary) => ReactNode;
66
+ /** 記事が0件のときの表示。既定は「記事がありません」 */
67
+ empty?: ReactNode;
68
+ /** 既定のクラス名(`nbk-article-list`)に追加するクラス名。0件のときも同じ要素に付く */
69
+ className?: string;
70
+ }
71
+ /**
72
+ * 記事の一覧。見た目は持たず、`nbk-article-list` から始まるクラス名を付ける。
73
+ * 記事があれば `ul.nbk-article-list`、0件なら `div.nbk-article-list.nbk-article-list--empty` を出す。
74
+ * フックを使わないため、Server Component としても使える。
75
+ */
76
+ declare function NoteArticleList({ articles, getHref, linkComponent, formatDate, headingLevel, renderItem, empty, className, }: NoteArticleListProps): ReactElement;
77
+
78
+ /** {@link NotePagination} の props */
79
+ interface NotePaginationProps {
80
+ /** 現在のページ(1 始まり) */
81
+ page: number;
82
+ /** 最終ページかどうか(`listArticles` の戻り値の `isLastPage`) */
83
+ isLastPage: boolean;
84
+ /** 総件数。分かれば「3 / 40」のように総ページ数も表示する。RSS では null */
85
+ totalCount?: number | null;
86
+ /** 1ページの件数。総ページ数の計算に使う。既定は 6(note の一覧 API の件数) */
87
+ perPage?: number;
88
+ /**
89
+ * ページ番号からリンク先を作る。
90
+ * 戻り値はそのまま `href` に入るため、信頼できない値(`javascript:` など)を返さないこと。
91
+ */
92
+ getHref: (page: number) => string;
93
+ /** リンクに使う要素。既定は `'a'` */
94
+ linkComponent?: NoteLinkComponent;
95
+ /** 文言の差し替え。既定は「前へ」「次へ」、nav の aria-label は「ページ送り」 */
96
+ labels?: {
97
+ previous?: ReactNode;
98
+ next?: ReactNode;
99
+ navigation?: string;
100
+ };
101
+ /** 既定のクラス名(`nbk-pagination`)に追加するクラス名 */
102
+ className?: string;
103
+ }
104
+ /**
105
+ * 前後のページへのリンク。見た目は持たず、`nbk-pagination` から始まるクラス名を付ける。
106
+ * フックを使わないため、Server Component としても使える。
107
+ */
108
+ declare function NotePagination({ page, isLastPage, totalCount, perPage, getHref, linkComponent: Link, labels, className, }: NotePaginationProps): ReactElement;
109
+
110
+ export { NoteArticleCard, NoteArticleList, NoteArticleSummary, NotePagination };
111
+ export type { NoteArticleCardProps, NoteArticleListProps, NoteHeadingLevel, NoteLinkComponent, NotePaginationProps };