-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/docusaurus-theme-classic/src/theme/Blog/Components/Author/GeneratedImage/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
...ocusaurus-theme-classic/src/theme/Blog/Components/Author/GeneratedImage/styles.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters