Skip to content

Commit

Permalink
feat: poc generated image
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Aug 20, 2024
1 parent 1c56fa5 commit 1a30426
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ declare module '@theme/Blog/Components/Author/Socials' {
export default function BlogAuthorSocials(props: Props): JSX.Element;
}

declare module '@theme/Blog/Components/Author/GeneratedImage' {
export interface Props {
readonly name: string;
readonly link?: string;
readonly className?: string;
}

export default function GeneratedImage(props: Props): JSX.Element;
}

declare module '@theme/BlogListPaginator' {
import type {BlogPaginatedMetadata} from '@docusaurus/plugin-content-blog';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import clsx from 'clsx';
import Link, {type Props as LinkProps} from '@docusaurus/Link';
import type {Props} from '@theme/Blog/Components/Author/GeneratedImage';
import styles from './styles.module.css';

const hashString = (str: string) => {
let hash = 0;
for (let i = 0; i < str.length; i += 1) {
hash = (hash + str.charCodeAt(i) * (i + 1)) % 1000;
}
return hash;
};

function MaybeLink(props: LinkProps): JSX.Element {
if (props.href) {
return <Link {...props} />;
}
return <>{props.children}</>;
}

export default function GeneratedImage({
name,
link,
className,
}: Props): JSX.Element {
const gradientId = `grad-${link}`;

const hashName = hashString(name);
const primaryColor = `hsl(${hashName % 360}, 95%, 50%)`;
const secondaryColor = `hsl(${(hashName + 120) % 360}, 95%, 50%)`;

return (
<MaybeLink href={link} className="avatar__photo-link">
<svg
className={clsx('avatar__photo-link', styles.authorSvg, className)}
viewBox="0 0 72 72"
xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id={gradientId} x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stopColor={primaryColor} />
<stop offset="100%" stopColor={secondaryColor} />
</linearGradient>
</defs>
<circle cx="36" cy="36" r="36" fill={`url(#${gradientId})`} />
<text
x="50%"
y="50%"
textAnchor="middle"
alignmentBaseline="central"
fontSize="2rem"
fill="#fff">
{name![0]?.toLocaleUpperCase()}
</text>
</svg>
</MaybeLink>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

.authorSvg {
width: var(--ifm-avatar-photo-size);
height: var(--ifm-avatar-photo-size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Link, {type Props as LinkProps} from '@docusaurus/Link';
import AuthorSocials from '@theme/Blog/Components/Author/Socials';
import type {Props} from '@theme/Blog/Components/Author';
import Heading from '@theme/Heading';
import GeneratedImage from '@theme/Blog/Components/Author/GeneratedImage';
import styles from './styles.module.css';

function MaybeLink(props: LinkProps): JSX.Element {
Expand Down Expand Up @@ -65,14 +66,20 @@ export default function BlogAuthor({
className,
styles[`author-as-${as}`],
)}>
{imageURL && (
{imageURL ? (
<MaybeLink href={link} className="avatar__photo-link">
<img
className={clsx('avatar__photo', styles.authorImage)}
src={imageURL}
alt={name}
/>
</MaybeLink>
) : (
<GeneratedImage
name={name!}
link={link}
className={clsx(styles.authorImage)}
/>
)}

{(name || title) && (
Expand Down

0 comments on commit 1a30426

Please sign in to comment.