`): the escaped name, a staff badge, ``, the text through * `commentTextHtml` (plain text; only `https:` URLs become links, `rel="ugc nofollow noopener * noreferrer"`), "in reply to " for replies to replies, and a "Report" disclosure with a * small plain form per comment. A removed top-level comment with approved replies reads * "This comment was removed". * - Open threads get the plain HTML form (`method=post action=/__cloudhal/comments`, a honeypot, * hidden challenge fields the module fills) and the module tag; closed ones "Comments are * closed"; hidden and off threads render nothing. * - The outcome notes (`#comment-pending`, `#comment-invalid`, …) are hidden until the fragment * names them (`:target`), so a JavaScript-less post's `303` lands on the cached page and still * explains itself. * * No third-party resource, no inline script; the only ``; function authorName(c: Comment, t: CommentStrings): string { return c.author.kind === "erased" ? t.anonymous : c.author.name; } function formatDate(iso: string, locale: string): string { try { return new Intl.DateTimeFormat(locale, { dateStyle: "long", timeZone: "UTC" }).format( new Date(iso), ); } catch { return iso.slice(0, 10); } } function reportForm(c: Comment, t: CommentStrings): string { const id = `report-${c.id}`; const options = Object.entries(t.reasons) .map(([value, label]) => `${e(label)}`) .join(""); return `${e(t.report)}${e(t.reportReason)} ${options}${e(t.reportDetails)} ${e(t.reportEmail)} ${e(t.website)}${e(t.sendReport)}`; } function commentHtml( c: Comment, view: CommentsView, t: CommentStrings, names: ReadonlyMap, top: boolean, inner = "", ): string { const name = authorName(c, t); const badge = c.author.kind === "staff" ? ` ${e(c.author.badge === "author" ? t.author : t.team)}` : ""; const answered = c.inReplyToId ? names.get(c.inReplyToId) : undefined; const context = c.inReplyToId && answered !== undefined ? ` ${e(t.inReplyTo(answered))}` : ""; const reply = view.state === "open" ? `${e(t.reply)} ` : ""; return `${e(name)}${badge} ${e(formatDate(c.createdAt, view.locale))}${context}${c.body ? commentTextHtml(c.body.text) : ""}${inner}`; } function itemHtml( item: RenderItem, view: CommentsView, t: CommentStrings, names: ReadonlyMap, ): string { const replies = item.replies.length ? `${item.replies.map((r) => commentHtml(r, view, t, names, false)).join("")}` : ""; if ("comment" in item) return commentHtml(item.comment, view, t, names, true, replies); return `${e(t.removed)}${replies}`; } function formHtml(view: CommentsView, t: CommentStrings): string { const noScript = view.allowWithoutScript ? "" : `${e(t.scriptRequired)}`; return `${e(t.formTitle)}${noScript}${e(t.name)} ${e(t.nameHint)}${e(t.email)} ${e(t.emailHint)}${e(t.comment)} ${e(t.website)}${e(t.moderation)}${e(t.submit)} ${e(t.cancelReply)}`; } /** * The section's HTML for a view, or `""` when nothing is shown (hidden and off threads, or no * view). Previews get a placeholder and never show comments. */ export function commentsHtml(view: CommentsView | undefined, strings?: CommentStrings): string { if (!view) return ""; const t = strings ?? commentStrings(view.locale); if (view.state === "preview") return `${e(t.preview)}`; if (view.state !== "open" && view.state !== "closed") return ""; const page = view.page; const items = page?.items ?? []; const names = new Map(); for (const item of items) for (const c of "comment" in item ? [item.comment, ...item.replies] : item.replies) names.set(c.id, authorName(c, t)); const open = view.state === "open"; const notes = Object.entries(t.notes) .map( ([id, text]) => `${e(text)}`, ) .join(""); const attrs = [ `class="cloudhal-comments"`, `id="comments"`, `aria-labelledby="comments-title"`, `data-cloudhal-comments`, `data-state="${view.state}"`, ...(open ? [ `data-protection="${e(view.protection)}"`, ...(view.protection === "turnstile" && view.turnstileSiteKey ? [`data-sitekey="${e(view.turnstileSiteKey)}"`] : []), `data-sending="${e(t.sending)}"`, `data-pending-label="${e(t.awaiting)}"`, `data-reply-label="${e(t.replyingTo)}"`, ] : []), ].join(" "); const list = `${items.map((i) => itemHtml(i, view, t, names)).join("")}`; const more = page && page.page < page.pageCount ? `${e(t.more)}` : ""; const tail = open ? `${formHtml(view, t)}${view.script ? `` : ""}` : `${e(t.closed)}`; return `${STYLE}${e(t.heading(page?.total ?? 0))}${notes}${list}${more}${tail}`; }
${e(name)}${badge} ${e(formatDate(c.createdAt, view.locale))}${context}${c.body ? commentTextHtml(c.body.text) : ""}