diff --git a/src/components/content/PostDetail.tsx b/src/components/content/PostDetail.tsx index cd651d7..d059ce8 100644 --- a/src/components/content/PostDetail.tsx +++ b/src/components/content/PostDetail.tsx @@ -8,7 +8,7 @@ import { ease, fontFamily, fontSize, radius, spacing, theme } from "~/layout.tsx import type { Post, TextSegment } from "~/content/post.ts"; import { type Profile } from "~/services/post.ts"; import { Bluesky, ExternalLink } from "~/components/ui/Icon.tsx"; -import { formatPostDate } from "~/util/formatting.ts"; +import { flattenThread, formatPostDate } from "~/util/formatting.ts"; const Styled = css(` :scope { @@ -240,16 +240,6 @@ function renderSegments(segments: TextSegment[]) { }); } -function flattenThread(post: Post, isRoot: boolean = true): Array { - const flat = [{ ...post, isRoot }]; - if (post.replies && post.replies.length > 0) { - for (const reply of post.replies) { - flat.push(...flattenThread(reply, false)); - } - } - return flat; -} - export default function PostDetail({ post, profile }: { post: Post; profile: Profile }) { const items = flattenThread(post); diff --git a/src/components/content/TimelinePost.tsx b/src/components/content/TimelinePost.tsx index 0b47bbc..a9424d9 100644 --- a/src/components/content/TimelinePost.tsx +++ b/src/components/content/TimelinePost.tsx @@ -8,7 +8,7 @@ import { ease, fontFamily, fontSize, radius, spacing, theme } from "~/layout.tsx import type { Post, TextSegment } from "~/content/post.ts"; import { type Profile } from "~/services/post.ts"; import { Bluesky } from "~/components/ui/Icon.tsx"; -import { formatPostDate } from "~/util/formatting.ts"; +import { flattenThread, formatPostDate } from "~/util/formatting.ts"; const Styled = css(` :scope { @@ -236,6 +236,7 @@ function truncateSegments(segments: TextSegment[]): TextSegment[] { export default function TimelinePost({ post, profile }: { post: Post; profile: Profile }) { const hasReplies = post.replies && post.replies.length > 0; + const items = flattenThread(post); return ( @@ -261,7 +262,7 @@ export default function TimelinePost({ post, profile }: { post: Post; profile: P {hasReplies && (
- {post.replies?.length ?? 0} more {(post.replies?.length ?? 0) === 1 ? "skeet" : "more skeets"}{" "} + {items.length - 1} more {(items.length - 1) === 1 ? "skeet" : "more skeets"}{" "} in this thread
)} diff --git a/src/layout.tsx b/src/layout.tsx index 5dbdaaf..6ad895f 100644 --- a/src/layout.tsx +++ b/src/layout.tsx @@ -118,10 +118,23 @@ const styles = /* css */ ` `; interface CollectResult { - head: JsxNode[]; + head: Map; body: JsxNode[]; } +function getHeadKey(node: JsxNode): string | null { + if (typeof node !== "object" || node === null || !("tag" in node)) return null; + const { tag, props } = node as JsxElement; + + if (tag === "title") return "__title__"; + if (tag === "meta") { + if (props.property) return `meta::property::${props.property}`; + if (props.name) return `meta::name::${props.name}`; + if (props.charset) return "meta::charset"; + } + return null; +} + function collect(node: JsxNode, result: CollectResult): void { if (node == null || node === false || node === true) return; @@ -134,9 +147,19 @@ function collect(node: JsxNode, result: CollectResult): void { } const { tag, props } = node as JsxElement; - if (tag === "head") { - return void result.head.push(props.children); + const tempBody: JsxNode[] = []; + collect(props.children, { head: new Map(), body: tempBody }); + + for (const child of tempBody) { + const key = getHeadKey(child); + if (key) { + result.head.set(key, child); + } else { + result.head.set(`__raw__${Math.random()}`, child); + } + } + return; } if (typeof tag === "function") { @@ -148,17 +171,29 @@ function collect(node: JsxNode, result: CollectResult): void { return void result.body.push(node); } - const child: CollectResult = { head: [], body: [] }; + const child: CollectResult = { head: new Map(), body: [] }; collect(props.children, child); - if (child.head.length) { - result.head.push(...child.head); + for (const [key, value] of child.head) { + result.head.set(key, value); } result.body.push(jsx(tag, { ...props, children: child.body as any })); } export function Layout({ scope, children, class: className, selected }: LayoutProps) { - const result: CollectResult = { head: [], body: [] }; + const result: CollectResult = { head: new Map(), body: [] }; + + const globalHeadTags = [ + jsx("meta", { property: "og:site_name", content: "kyu.re" }), + jsx("meta", { property: "og:type", content: "profile" }), + jsx("meta", { property: "og:description", content: "one of the girls of all time" }), + jsx("meta", { property: "og:image", content: "https://kyu.re/~.png" }), + jsx("meta", { property: "og:image:type", content: "image/png" }), + jsx("meta", { property: "og:title", content: "júlia lívia" }), + jsx("meta", { name: "theme-color", content: tokens.theme.accentDim }), + ]; + + collect(jsx("head", { children: globalHeadTags }), result); collect(children, result); return "" + ( @@ -167,18 +202,11 @@ export function Layout({ scope, children, class: className, selected }: LayoutPr - - - - - - - + {[...result.head.values()]} - {result.head} diff --git a/src/util/formatting.ts b/src/util/formatting.ts index 69e27c9..da527ae 100644 --- a/src/util/formatting.ts +++ b/src/util/formatting.ts @@ -1,3 +1,15 @@ +import { Post } from "~/content/post.ts"; + +export function flattenThread(post: Post, isRoot: boolean = true): Array { + const flat = [{ ...post, isRoot }]; + if (post.replies && post.replies.length > 0) { + for (const reply of post.replies) { + flat.push(...flattenThread(reply, false)); + } + } + return flat; +} + export function formatPostDate(date: Date): string { const now = Date.now(); const diff = now - date.getTime();