-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAuthor.tsx
76 lines (65 loc) · 2.15 KB
/
Author.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import style from "./styles/author.scss"
import { FullSlug, _stripSlashes, joinSegments, pathToRoot } from "../util/path"
import { QuartzComponentConstructor, QuartzComponentProps } from "./types"
import { URL } from "url"
import { hostname } from "os"
import { range } from "d3"
import { JSXInternal } from "preact/src/jsx"
const profilePhotos: Record<string, string> = {
"Xinyang YU": "https://github.com/xy-241.png",
"Yufan Zhu": "https://github.com/Yufannnn.png",
"default": "https://imgflip.com/s/meme/Doge.jpg",
}
function createAuthorElement(author: string, link: string) {
author = author.trim()
link = link.trim()
var knownAuthor = (author in profilePhotos);
const image_element = <img src={knownAuthor ? profilePhotos[author] : profilePhotos["default"]} alt="" />;
return(
<div class="authorWLink">
<a href={link} >
{image_element}
<span>{author}</span>
</a>
</div>
)
}
function cleanTooManyAuthors(authorsElements:JSXInternal.Element[], maxShown: number= 3) {
if (authorsElements.length <= maxShown) {
return authorsElements
}
var shownElements = authorsElements.slice(0, maxShown)
var hiddenAuthors = (
<div class="hiddenAuthors"> <div class="hiddenAuthorsContainer">{authorsElements.slice(maxShown, authorsElements.length)}</div></div>
)
return [...shownElements, hiddenAuthors]
}
export default (() => {
function Author({fileData}: QuartzComponentProps) {
const authors = fileData.frontmatter?.["Author"] ?? [""];
const authorLinks = fileData.frontmatter?.["Author Profile"] ?? [""];
var authorsElements = []
if (authors) {
// var message = "🖋"
for (var i of range(authors.length)) {
var link = ""
if (i < authorLinks?.length) {
link = authorLinks[i]
}
authorsElements.push(createAuthorElement(authors[i], link))
}
return (
<div class="author">
{/* <p>{message} </p> */}
{cleanTooManyAuthors(authorsElements)}
</div>
)
}
else {
return (<p></p>)
}
}
Author.css = style
return Author
}
) satisfies QuartzComponentConstructor