|
| 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 | + const primaryAuthor = metadata.authors?.[0]; |
| 66 | + const profile = primaryAuthor?.key ? getAuthorProfile(primaryAuthor.key) : undefined; |
| 67 | + |
| 68 | + const authorAvatar = primaryAuthor?.imageURL || profile?.imageUrl; |
| 69 | + const authorName = primaryAuthor?.name || profile?.name; |
| 70 | + const githubHandle = primaryAuthor |
| 71 | + ? getGitHubHandle({ key: primaryAuthor.key, url: primaryAuthor.url }) |
| 72 | + : undefined; |
| 73 | + const githubUrl = primaryAuthor |
| 74 | + ? getGitHubUrl({ key: primaryAuthor.key, url: primaryAuthor.url, name: primaryAuthor.name }) |
| 75 | + : undefined; |
| 76 | + |
| 77 | + const roundedReadTime = Math.max(1, Math.ceil(metadata.readingTime || 0)); |
| 78 | + const readTimeText = `${roundedReadTime} min read`; |
| 79 | + |
| 80 | + const blogDate = metadata.date |
| 81 | + ? new Intl.DateTimeFormat(siteConfig.i18n?.defaultLocale || "en-US", { |
| 82 | + year: "numeric", |
| 83 | + month: "short", |
| 84 | + day: "numeric", |
| 85 | + }).format(new Date(metadata.date)) |
| 86 | + : undefined; |
| 87 | + |
| 88 | + const tags = (metadata.tags ?? []) |
| 89 | + .map((t) => (typeof t === "string" ? t : (t as { label?: string }).label ?? "")) |
| 90 | + .filter(Boolean); |
| 91 | + |
| 92 | + return ( |
| 93 | + <> |
| 94 | + {/* Render only the title — Info and Authors are replaced by our compact bar */} |
| 95 | + <BlogPostItemHeaderTitle /> |
| 96 | + <div className={styles.metaSection}> |
| 97 | + {/* Compact meta row: avatar · @handle · date · reading time */} |
| 98 | + <div className={styles.metaRow}> |
| 99 | + {(authorAvatar || authorName) && ( |
| 100 | + <div className={styles.authorPart}> |
| 101 | + {authorAvatar ? ( |
| 102 | + <img |
| 103 | + className={styles.avatar} |
| 104 | + src={authorAvatar} |
| 105 | + alt={authorName ? `${authorName} avatar` : "Author avatar"} |
| 106 | + loading="lazy" |
| 107 | + /> |
| 108 | + ) : ( |
| 109 | + <div className={styles.avatarFallback} aria-hidden="true"> |
| 110 | + {authorName?.charAt(0).toUpperCase()} |
| 111 | + </div> |
| 112 | + )} |
| 113 | + {githubHandle && |
| 114 | + (githubUrl ? ( |
| 115 | + <Link |
| 116 | + to={githubUrl} |
| 117 | + className={styles.handle} |
| 118 | + target="_blank" |
| 119 | + rel="noopener noreferrer" |
| 120 | + > |
| 121 | + {githubHandle} |
| 122 | + </Link> |
| 123 | + ) : ( |
| 124 | + <span className={styles.handle}>{githubHandle}</span> |
| 125 | + ))} |
| 126 | + </div> |
| 127 | + )} |
| 128 | + |
| 129 | + {blogDate && ( |
| 130 | + <> |
| 131 | + <span className={styles.sep} aria-hidden="true"> |
| 132 | + • |
| 133 | + </span> |
| 134 | + <span className={styles.date}>{blogDate}</span> |
| 135 | + </> |
| 136 | + )} |
| 137 | + |
| 138 | + <span className={styles.sep} aria-hidden="true"> |
| 139 | + • |
| 140 | + </span> |
| 141 | + <span className={styles.readTime}> |
| 142 | + <svg |
| 143 | + xmlns="http://www.w3.org/2000/svg" |
| 144 | + viewBox="0 0 24 24" |
| 145 | + width="13" |
| 146 | + height="13" |
| 147 | + fill="none" |
| 148 | + stroke="currentColor" |
| 149 | + strokeWidth="2" |
| 150 | + strokeLinecap="round" |
| 151 | + strokeLinejoin="round" |
| 152 | + aria-hidden="true" |
| 153 | + > |
| 154 | + <circle cx="12" cy="12" r="10" /> |
| 155 | + <polyline points="12 6 12 12 16 14" /> |
| 156 | + </svg> |
| 157 | + {readTimeText} |
| 158 | + </span> |
| 159 | + </div> |
| 160 | + |
| 161 | + {/* Colored tag pills */} |
| 162 | + {tags.length > 0 && ( |
| 163 | + <div className={styles.tagsRow} aria-label="Tags"> |
| 164 | + {tags.map((tag) => { |
| 165 | + const c = tagColor(tag); |
| 166 | + return ( |
| 167 | + <span |
| 168 | + key={tag} |
| 169 | + className={styles.tag} |
| 170 | + style={ |
| 171 | + { |
| 172 | + "--tag-dot": c.dot, |
| 173 | + "--tag-border": c.border, |
| 174 | + "--tag-bg": c.bg, |
| 175 | + "--tag-text": c.text, |
| 176 | + } as React.CSSProperties |
| 177 | + } |
| 178 | + > |
| 179 | + <span className={styles.tagDot} aria-hidden="true" /> |
| 180 | + {tag} |
| 181 | + </span> |
| 182 | + ); |
| 183 | + })} |
| 184 | + </div> |
| 185 | + )} |
| 186 | + </div> |
| 187 | + </> |
| 188 | + ); |
| 189 | +} |
0 commit comments