note-blog-kit 0.1.0 → 0.2.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.
@@ -1,10 +1,325 @@
1
1
  import { jsxs, jsx } from 'react/jsx-runtime';
2
2
 
3
+ // src/react/body/parse.ts
4
+ var ALLOWED_TAGS = /* @__PURE__ */ new Set([
5
+ "p",
6
+ "br",
7
+ "h2",
8
+ "h3",
9
+ "h4",
10
+ "strong",
11
+ "em",
12
+ "s",
13
+ "a",
14
+ "ul",
15
+ "ol",
16
+ "li",
17
+ "blockquote",
18
+ "figure",
19
+ "figcaption",
20
+ "img",
21
+ "pre",
22
+ "code",
23
+ "hr",
24
+ "table",
25
+ "thead",
26
+ "tbody",
27
+ "tr",
28
+ "th",
29
+ "td",
30
+ "div"
31
+ ]);
32
+ var DISCARD_TAGS = /* @__PURE__ */ new Set([
33
+ "script",
34
+ "style",
35
+ "iframe",
36
+ "frame",
37
+ "frameset",
38
+ "object",
39
+ "embed",
40
+ "applet",
41
+ "form",
42
+ "button",
43
+ "textarea",
44
+ "select",
45
+ "option",
46
+ "svg",
47
+ "math",
48
+ "template",
49
+ "noscript",
50
+ "title",
51
+ "xmp",
52
+ "plaintext",
53
+ "noembed",
54
+ "noframes"
55
+ ]);
56
+ var VOID_TAGS = /* @__PURE__ */ new Set(["br", "hr", "img"]);
57
+ var NO_TEXT_TAGS = /* @__PURE__ */ new Set(["table", "thead", "tbody", "tr"]);
58
+ var CLOSES_P = /* @__PURE__ */ new Set(["p", "h2", "h3", "h4", "ul", "ol", "blockquote", "figure", "pre", "hr", "table", "div"]);
59
+ var MAX_DEPTH = 128;
60
+ var TOKEN_RE = /<!--[\s\S]*?(?:-->|$)|<(\/?)([a-zA-Z][a-zA-Z0-9-]*)((?:\s+[^\s=>/"'<]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>"'<]+))?)*)\s*\/?>|[^<]+|</g;
61
+ var ATTR_RE = /([^\s=>/"'<]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>"'<]+)))?/g;
62
+ var NAMED_ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: "\xA0" };
63
+ function decodeEntities(text) {
64
+ return text.replace(/&(#x[0-9a-fA-F]{1,8}|#[0-9]{1,10}|[a-zA-Z]+);/g, (whole, name) => {
65
+ if (name[0] !== "#") return Object.hasOwn(NAMED_ENTITIES, name) ? NAMED_ENTITIES[name] : whole;
66
+ const code = name[1] === "x" || name[1] === "X" ? Number.parseInt(name.slice(2), 16) : Number(name.slice(1));
67
+ const valid = code > 0 && code <= 1114111 && (code < 55296 || code > 57343);
68
+ return valid ? String.fromCodePoint(code) : "\uFFFD";
69
+ });
70
+ }
71
+ function parseAttrs(source) {
72
+ const attrs = /* @__PURE__ */ Object.create(null);
73
+ for (const m of source.matchAll(ATTR_RE)) {
74
+ const name = m[1].toLowerCase();
75
+ if (!Object.hasOwn(attrs, name)) attrs[name] = decodeEntities(m[2] ?? m[3] ?? m[4] ?? "");
76
+ }
77
+ return attrs;
78
+ }
79
+ function parseBody(html) {
80
+ const root = { tag: "#root", attrs: /* @__PURE__ */ Object.create(null), children: [] };
81
+ const stack = [root];
82
+ const top = () => stack[stack.length - 1];
83
+ const closeTo = (tag) => {
84
+ for (let i = stack.length - 1; i > 0; i -= 1) {
85
+ if (stack[i].tag === tag) {
86
+ stack.length = i;
87
+ return;
88
+ }
89
+ }
90
+ };
91
+ const append = (node) => {
92
+ if (typeof node === "string" && (NO_TEXT_TAGS.has(top().tag) || top().tag !== "pre" && node === "")) return;
93
+ top().children.push(node);
94
+ };
95
+ const lower = html.toLowerCase();
96
+ TOKEN_RE.lastIndex = 0;
97
+ for (let m = TOKEN_RE.exec(html); m !== null; m = TOKEN_RE.exec(html)) {
98
+ const [token, closing, rawName, rawAttrs] = m;
99
+ if (rawName === void 0) {
100
+ if (!token.startsWith("<!--")) append(decodeEntities(token));
101
+ continue;
102
+ }
103
+ const tag = rawName.toLowerCase();
104
+ if (DISCARD_TAGS.has(tag)) {
105
+ if (!closing) {
106
+ const end = lower.indexOf(`</${tag}`, TOKEN_RE.lastIndex);
107
+ TOKEN_RE.lastIndex = end === -1 ? html.length : end;
108
+ }
109
+ continue;
110
+ }
111
+ if (!ALLOWED_TAGS.has(tag)) continue;
112
+ if (closing) {
113
+ closeTo(tag);
114
+ continue;
115
+ }
116
+ if (CLOSES_P.has(tag)) closeTo("p");
117
+ if (tag === "a" && stack.some((el2) => el2.tag === "a")) closeTo("a");
118
+ if (stack.length > MAX_DEPTH) continue;
119
+ if (tag === "tr" && top().tag === "table") {
120
+ const tbody = { tag: "tbody", attrs: /* @__PURE__ */ Object.create(null), children: [] };
121
+ append(tbody);
122
+ stack.push(tbody);
123
+ }
124
+ const el = { tag, attrs: parseAttrs(rawAttrs ?? ""), children: [] };
125
+ append(el);
126
+ if (!VOID_TAGS.has(tag)) stack.push(el);
127
+ }
128
+ return root.children;
129
+ }
130
+
3
131
  // src/react/class-names.ts
4
132
  function classNames(...names) {
5
133
  return names.filter((name) => typeof name === "string" && name !== "").join(" ");
6
134
  }
7
135
 
136
+ // src/react/safe-url.ts
137
+ function safeHttpUrl(url) {
138
+ if (typeof url !== "string" || !/^https?:\/\//i.test(url)) return null;
139
+ for (let i = 0; i < url.length; i += 1) {
140
+ const code = url.charCodeAt(i);
141
+ if (code <= 32 || code === 127) return null;
142
+ }
143
+ return url;
144
+ }
145
+ var ID_RE = /^nbk-[A-Za-z0-9\-_.:/]+$/;
146
+ var SAFE_VALUE = /^[A-Za-z0-9\-_.:/]+$/;
147
+ var IMAGE_PREFIX = "https://assets.st-note.com/";
148
+ var DIGITS = /^\d+$/;
149
+ var REL_TOKENS = /* @__PURE__ */ new Set(["nofollow", "noopener", "noreferrer", "sponsored"]);
150
+ var BODY_CLASS = {
151
+ p: "nbk-body__p",
152
+ h2: "nbk-body__h2",
153
+ h3: "nbk-body__h3",
154
+ h4: "nbk-body__h4",
155
+ a: "nbk-body__a",
156
+ ul: "nbk-body__ul",
157
+ ol: "nbk-body__ol",
158
+ blockquote: "nbk-body__blockquote",
159
+ figure: "nbk-body__figure",
160
+ figcaption: "nbk-body__figcaption",
161
+ img: "nbk-body__img",
162
+ pre: "nbk-body__pre",
163
+ code: "nbk-body__code",
164
+ hr: "nbk-body__hr",
165
+ table: "nbk-body__table",
166
+ th: "nbk-body__th",
167
+ td: "nbk-body__td"
168
+ };
169
+ function toNumber(value) {
170
+ return value !== void 0 && DIGITS.test(value) ? Number(value) : void 0;
171
+ }
172
+ function safeHref(value) {
173
+ if (value === void 0) return null;
174
+ if (!/^mailto:[^\s"<>]+$/i.test(value)) return safeHttpUrl(value);
175
+ for (let i = 0; i < value.length; i += 1) {
176
+ if (value.charCodeAt(i) < 32 || value.charCodeAt(i) === 127) return null;
177
+ }
178
+ return value;
179
+ }
180
+ function safeHttpsUrl(value) {
181
+ const url = safeHttpUrl(value);
182
+ return url !== null && /^https:\/\//i.test(url) ? url : null;
183
+ }
184
+ function safeImageUrl(value) {
185
+ const url = safeHttpsUrl(value);
186
+ return url?.toLowerCase().startsWith(IMAGE_PREFIX) ? url : null;
187
+ }
188
+ function collectIds(nodes, ids) {
189
+ for (const node of nodes) {
190
+ if (typeof node === "string") continue;
191
+ if (node.attrs.id !== void 0 && ID_RE.test(node.attrs.id)) ids.add(node.attrs.id);
192
+ collectIds(node.children, ids);
193
+ }
194
+ return ids;
195
+ }
196
+ function DefaultToc({ items, title, linkableIds }) {
197
+ if (items.length === 0) return null;
198
+ const ids = new Set(linkableIds);
199
+ const seen = /* @__PURE__ */ new Map();
200
+ const keyOf = (id) => {
201
+ const count = seen.get(id) ?? 0;
202
+ seen.set(id, count + 1);
203
+ return count === 0 ? id : `${id}:${count}`;
204
+ };
205
+ return /* @__PURE__ */ jsxs("nav", { className: "nbk-toc", children: [
206
+ /* @__PURE__ */ jsx("p", { className: "nbk-toc__title", children: title }),
207
+ /* @__PURE__ */ jsx("ul", { className: "nbk-toc__list", children: items.map((item) => /* @__PURE__ */ jsx(
208
+ "li",
209
+ {
210
+ className: item.level === 2 ? "nbk-toc__item nbk-toc__item--h2" : "nbk-toc__item nbk-toc__item--h3",
211
+ children: ids.has(item.id) ? /* @__PURE__ */ jsx("a", { className: "nbk-toc__link", href: `#${item.id}`, children: item.text }) : item.text
212
+ },
213
+ keyOf(item.id)
214
+ )) })
215
+ ] });
216
+ }
217
+ function renderChildren(nodes, ctx) {
218
+ return nodes.map((node, index) => renderNode(node, ctx, index));
219
+ }
220
+ function renderEmbed(el, ctx, key) {
221
+ const service = el.attrs["data-nbk-embed-service"];
222
+ const embedKey = el.attrs["data-nbk-embed-key"];
223
+ const found = ctx.article.embeds.find((item) => item.key === embedKey);
224
+ const embed = found !== void 0 && safeHttpsUrl(found.url) !== null && SAFE_VALUE.test(found.service) ? found : null;
225
+ const children = renderChildren(el.children, { ...ctx, inEmbed: true });
226
+ const { embed: Embed } = ctx.components;
227
+ if (Embed) {
228
+ return /* @__PURE__ */ jsx(Embed, { embed, service, children }, key);
229
+ }
230
+ const url = service === "note" ? safeHttpsUrl(embed?.url ?? el.attrs["data-nbk-embed-url"]) : null;
231
+ const { Link } = ctx;
232
+ return /* @__PURE__ */ jsx("figure", { className: "nbk-embed", "data-service": service, children: url === null ? children : /* @__PURE__ */ jsx(Link, { href: url, className: "nbk-embed__link", children: url }) }, key);
233
+ }
234
+ function renderNode(node, ctx, key) {
235
+ if (typeof node === "string") return node;
236
+ const { tag, attrs } = node;
237
+ const id = attrs.id !== void 0 && ID_RE.test(attrs.id) ? attrs.id : void 0;
238
+ if (tag === "div") {
239
+ if (!Object.hasOwn(attrs, "data-nbk-toc")) return renderChildren(node.children, ctx);
240
+ const Toc = ctx.components.toc ?? DefaultToc;
241
+ const linkableIds = ctx.article.toc.filter((item) => ctx.ids.has(item.id)).map((item) => item.id);
242
+ return /* @__PURE__ */ jsx(Toc, { items: ctx.article.toc, linkableIds, title: ctx.tocTitle }, key);
243
+ }
244
+ const service = attrs["data-nbk-embed-service"];
245
+ if (tag === "figure" && service !== void 0 && SAFE_VALUE.test(service)) return renderEmbed(node, ctx, key);
246
+ if (tag === "img") {
247
+ const src = safeImageUrl(attrs.src);
248
+ if (src === null) return null;
249
+ const props2 = { src, alt: attrs.alt ?? "" };
250
+ const width = toNumber(attrs.width);
251
+ const height = toNumber(attrs.height);
252
+ if (width !== void 0) props2.width = width;
253
+ if (height !== void 0) props2.height = height;
254
+ const { img: Img } = ctx.components;
255
+ return Img ? /* @__PURE__ */ jsx(Img, { ...props2 }, key) : /* @__PURE__ */ jsx(
256
+ "img",
257
+ {
258
+ className: BODY_CLASS.img,
259
+ src: props2.src,
260
+ alt: props2.alt,
261
+ width: props2.width,
262
+ height: props2.height,
263
+ loading: "lazy",
264
+ decoding: "async"
265
+ },
266
+ key
267
+ );
268
+ }
269
+ const children = renderChildren(node.children, ctx);
270
+ if (tag === "h2" || tag === "h3" || tag === "h4") {
271
+ const { heading: Heading } = ctx.components;
272
+ if (Heading) {
273
+ const level = Number(tag[1]);
274
+ return id === void 0 ? /* @__PURE__ */ jsx(Heading, { level, children }, key) : /* @__PURE__ */ jsx(Heading, { level, id, children }, key);
275
+ }
276
+ }
277
+ const props = {};
278
+ if (BODY_CLASS[tag] !== void 0) props.className = BODY_CLASS[tag];
279
+ if (id !== void 0) props.id = id;
280
+ if (tag === "a") {
281
+ if (ctx.inEmbed) props.className = "nbk-embed__link";
282
+ const href = safeHref(attrs.href);
283
+ if (href !== null) props.href = href;
284
+ if (attrs.target === "_blank") props.target = "_blank";
285
+ const rel = (attrs.rel ?? "").split(/\s+/).filter((token) => REL_TOKENS.has(token));
286
+ if (props.target !== void 0) {
287
+ rel.push(...["nofollow", "noopener", "noreferrer"].filter((token) => !rel.includes(token)));
288
+ }
289
+ if (rel.length > 0) props.rel = rel.join(" ");
290
+ } else if (tag === "th" || tag === "td") {
291
+ const colSpan = toNumber(attrs.colspan);
292
+ const rowSpan = toNumber(attrs.rowspan);
293
+ if (colSpan !== void 0) props.colSpan = colSpan;
294
+ if (rowSpan !== void 0) props.rowSpan = rowSpan;
295
+ }
296
+ const Tag = tag;
297
+ if (tag === "br" || tag === "hr") return /* @__PURE__ */ jsx(Tag, { ...props }, key);
298
+ return /* @__PURE__ */ jsx(Tag, { ...props, children }, key);
299
+ }
300
+ function NoteArticleBody({
301
+ article,
302
+ components = {},
303
+ readMoreHref,
304
+ linkComponent: Link = "a",
305
+ labels,
306
+ className
307
+ }) {
308
+ const nodes = parseBody(article.bodyHtml);
309
+ const ctx = {
310
+ article,
311
+ components,
312
+ Link,
313
+ tocTitle: labels?.toc ?? "\u76EE\u6B21",
314
+ ids: collectIds(nodes, /* @__PURE__ */ new Set())
315
+ };
316
+ const moreHref = article.isPartial ? readMoreHref ?? safeHttpUrl(article.url) : null;
317
+ return /* @__PURE__ */ jsxs("div", { className: classNames("nbk-body", className), children: [
318
+ renderChildren(nodes, ctx),
319
+ moreHref !== null && /* @__PURE__ */ jsx("p", { className: "nbk-body__more", children: /* @__PURE__ */ jsx(Link, { href: moreHref, className: "nbk-body__more-link", children: labels?.readMore ?? "\u7D9A\u304D\u306F note \u3067" }) })
320
+ ] });
321
+ }
322
+
8
323
  // src/react/format-date.ts
9
324
  var formatter = new Intl.DateTimeFormat("ja-JP", {
10
325
  timeZone: "Asia/Tokyo",
@@ -16,16 +331,6 @@ function formatNoteDate(iso) {
16
331
  const date = new Date(iso);
17
332
  return Number.isNaN(date.getTime()) ? iso : formatter.format(date);
18
333
  }
19
-
20
- // src/react/safe-url.ts
21
- function safeHttpUrl(url) {
22
- if (typeof url !== "string" || !/^https?:\/\//i.test(url)) return null;
23
- for (let i = 0; i < url.length; i += 1) {
24
- const code = url.charCodeAt(i);
25
- if (code <= 32 || code === 127) return null;
26
- }
27
- return url;
28
- }
29
334
  function NoteArticleCard({
30
335
  article,
31
336
  href,
@@ -96,4 +401,4 @@ function NotePagination({
96
401
  ] });
97
402
  }
98
403
 
99
- export { NoteArticleCard, NoteArticleList, NotePagination };
404
+ export { NoteArticleBody, NoteArticleCard, NoteArticleList, NotePagination };