|
| 1 | +import React from "react"; |
| 2 | +import Link from "@docusaurus/Link"; |
| 3 | +import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; |
| 4 | +import { useBlogPost } from "@docusaurus/plugin-content-blog/client"; |
| 5 | +import BlogPostItemHeaderOriginal from "@theme-original/BlogPostItem/Header"; |
| 6 | +import BlogPostItemHeaderTitle from "@theme/BlogPostItem/Header/Title"; |
| 7 | +import type BlogPostItemHeaderType from "@theme/BlogPostItem/Header"; |
| 8 | +import type { WrapperProps } from "@docusaurus/types"; |
| 9 | +import { getAuthorProfile } from "../../../utils/authors"; |
| 10 | + |
| 11 | +import styles from "./styles.module.css"; |
| 12 | + |
| 13 | +type Props = WrapperProps<typeof BlogPostItemHeaderType>; |
| 14 | + |
| 15 | +const TAG_COLORS = [ |
| 16 | + { dot: "#f59e0b", border: "#fde68a", bg: "#fffbeb", text: "#92400e" }, |
| 17 | + { dot: "#6366f1", border: "#c7d2fe", bg: "#eef2ff", text: "#3730a3" }, |
| 18 | + { dot: "#ec4899", border: "#fbcfe8", bg: "#fdf2f8", text: "#9d174d" }, |
| 19 | + { dot: "#10b981", border: "#a7f3d0", bg: "#ecfdf5", text: "#065f46" }, |
| 20 | + { dot: "#f97316", border: "#fed7aa", bg: "#fff7ed", text: "#9a3412" }, |
| 21 | + { dot: "#8b5cf6", border: "#ddd6fe", bg: "#f5f3ff", text: "#5b21b6" }, |
| 22 | + { dot: "#14b8a6", border: "#99f6e4", bg: "#f0fdfa", text: "#134e4a" }, |
| 23 | + { dot: "#ef4444", border: "#fecaca", bg: "#fef2f2", text: "#991b1b" }, |
| 24 | +]; |
| 25 | + |
| 26 | +function tagColor(label: string) { |
| 27 | + let hash = 0; |
| 28 | + for (let i = 0; i < label.length; i++) { |
| 29 | + hash = (hash * 31 + label.charCodeAt(i)) & 0xffff; |
| 30 | + } |
| 31 | + return TAG_COLORS[hash % TAG_COLORS.length]; |
| 32 | +} |
| 33 | + |
| 34 | +function getGitHubHandle(author: { key?: string; url?: string }): string | undefined { |
| 35 | + if (author.url && /github\.com\//i.test(author.url)) { |
| 36 | + const matched = author.url.match(/github\.com\/([^/?#]+)/i); |
| 37 | + if (matched?.[1]) { |
| 38 | + return `@${matched[1]}`; |
| 39 | + } |
| 40 | + } |
| 41 | + if (author.key) { |
| 42 | + return `@${author.key}`; |
| 43 | + } |
| 44 | + return undefined; |
| 45 | +} |
| 46 | + |
| 47 | +function getGitHubUrl(author: { key?: string; url?: string; name?: string }): string | undefined { |
| 48 | + if (author.key) { |
| 49 | + return getAuthorProfile(author.key).githubUrl; |
| 50 | + } |
| 51 | + if (author.url && /github\.com\//i.test(author.url)) { |
| 52 | + return author.url.startsWith("http") ? author.url : `https://${author.url}`; |
| 53 | + } |
| 54 | + return undefined; |
| 55 | +} |
| 56 | + |
| 57 | +export default function BlogPostItemHeaderWrapper(props: Props): JSX.Element { |
| 58 | + const { siteConfig } = useDocusaurusContext(); |
| 59 | + const { metadata, isBlogPostPage } = useBlogPost(); |
| 60 | + |
| 61 | + if (!isBlogPostPage) { |
| 62 | + return <BlogPostItemHeaderOriginal {...props} />; |
| 63 | + } |
| 64 | + |
| 65 | + // Build display data for ALL authors (not just the first one) |
| 66 | + const authors = (metadata.authors ?? []).map((author) => { |
| 67 | + const profile = author.key ? getAuthorProfile(author.key) : undefined; |
| 68 | + return { |
| 69 | + avatar: author.imageURL || profile?.imageUrl, |
| 70 | + name: author.name || profile?.name, |
| 71 | + handle: getGitHubHandle({ key: author.key, url: author.url }), |
| 72 | + url: getGitHubUrl({ key: author.key, url: author.url, name: author.name }), |
| 73 | + }; |
| 74 | + }); |
| 75 | + |
| 76 | + const roundedReadTime = Math.max(1, Math.ceil(metadata.readingTime || 0)); |
| 77 | + const readTimeText = `${roundedReadTime} min read`; |
| 78 | + |
| 79 | + const blogDate = metadata.date |
| 80 | + ? new Intl.DateTimeFormat(siteConfig.i18n?.defaultLocale || "en-US", { |
| 81 | + year: "numeric", |
| 82 | + month: "short", |
| 83 | + day: "numeric", |
| 84 | + }).format(new Date(metadata.date)) |
| 85 | + : undefined; |
| 86 | + |
| 87 | + const tags = (metadata.tags ?? []) |
| 88 | + .map((t) => (typeof t === "string" ? t : (t as { label?: string }).label ?? "")) |
| 89 | + .filter(Boolean); |
| 90 | + |
| 91 | + return ( |
| 92 | + <> |
| 93 | + {/* Render only the title — Info and Authors are replaced by our compact bar */} |
| 94 | + <BlogPostItemHeaderTitle /> |
| 95 | + <div className={styles.metaSection}> |
| 96 | + {/* Compact meta row: avatars · @handles · date · reading time */} |
| 97 | + <div className={styles.metaRow}> |
| 98 | + {authors.length > 0 && ( |
| 99 | + <div className={styles.authorsPart}> |
| 100 | + {authors.map((author, idx) => ( |
| 101 | + <React.Fragment key={author.handle ?? `${author.name ?? ""}-${idx}`}> |
| 102 | + {idx > 0 && ( |
| 103 | + <span className={styles.authorSep} aria-hidden="true"> |
| 104 | + & |
| 105 | + </span> |
| 106 | + )} |
| 107 | + <div className={styles.authorPart}> |
| 108 | + {author.avatar ? ( |
| 109 | + <img |
| 110 | + className={styles.avatar} |
| 111 | + src={author.avatar} |
| 112 | + alt={author.name ? `${author.name} avatar` : "Author avatar"} |
| 113 | + loading="lazy" |
| 114 | + /> |
| 115 | + ) : ( |
| 116 | + <div className={styles.avatarFallback} aria-hidden="true"> |
| 117 | + {author.name?.charAt(0).toUpperCase()} |
| 118 | + </div> |
| 119 | + )} |
| 120 | + {author.handle && |
| 121 | + (author.url ? ( |
| 122 | + <Link |
| 123 | + to={author.url} |
| 124 | + className={styles.handle} |
| 125 | + target="_blank" |
| 126 | + rel="noopener noreferrer" |
| 127 | + > |
| 128 | + {author.handle} |
| 129 | + </Link> |
| 130 | + ) : ( |
| 131 | + <span className={styles.handle}>{author.handle}</span> |
| 132 | + ))} |
| 133 | + </div> |
| 134 | + </React.Fragment> |
| 135 | + ))} |
| 136 | + </div> |
| 137 | + )} |
| 138 | + |
| 139 | + {blogDate && ( |
| 140 | + <> |
| 141 | + <span className={styles.sep} aria-hidden="true"> |
| 142 | + • |
| 143 | + </span> |
| 144 | + <span className={styles.date}>{blogDate}</span> |
| 145 | + </> |
| 146 | + )} |
| 147 | + |
| 148 | + <span className={styles.sep} aria-hidden="true"> |
| 149 | + • |
| 150 | + </span> |
| 151 | + <span className={styles.readTime}> |
| 152 | + <svg |
| 153 | + xmlns="http://www.w3.org/2000/svg" |
| 154 | + viewBox="0 0 24 24" |
| 155 | + width="13" |
| 156 | + height="13" |
| 157 | + fill="none" |
| 158 | + stroke="currentColor" |
| 159 | + strokeWidth="2" |
| 160 | + strokeLinecap="round" |
| 161 | + strokeLinejoin="round" |
| 162 | + aria-hidden="true" |
| 163 | + > |
| 164 | + <circle cx="12" cy="12" r="10" /> |
| 165 | + <polyline points="12 6 12 12 16 14" /> |
| 166 | + </svg> |
| 167 | + {readTimeText} |
| 168 | + </span> |
| 169 | + </div> |
| 170 | + |
| 171 | + {/* Colored tag pills */} |
| 172 | + {tags.length > 0 && ( |
| 173 | + <div className={styles.tagsRow} aria-label="Tags"> |
| 174 | + {tags.map((tag) => { |
| 175 | + const c = tagColor(tag); |
| 176 | + return ( |
| 177 | + <span |
| 178 | + key={tag} |
| 179 | + className={styles.tag} |
| 180 | + style={ |
| 181 | + { |
| 182 | + "--tag-dot": c.dot, |
| 183 | + "--tag-border": c.border, |
| 184 | + "--tag-bg": c.bg, |
| 185 | + "--tag-text": c.text, |
| 186 | + } as React.CSSProperties |
| 187 | + } |
| 188 | + > |
| 189 | + <span className={styles.tagDot} aria-hidden="true" /> |
| 190 | + {tag} |
| 191 | + </span> |
| 192 | + ); |
| 193 | + })} |
| 194 | + </div> |
| 195 | + )} |
| 196 | + </div> |
| 197 | + </> |
| 198 | + ); |
| 199 | +} |
0 commit comments