Skip to content

Commit 5a70aa6

Browse files
committed
Support rendering avatars for links to profiles in a contributor section
1 parent 20d0b8d commit 5a70aa6

File tree

1 file changed

+84
-7
lines changed

1 file changed

+84
-7
lines changed

markdown/index.html

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,34 @@
119119
margin-bottom: 1em;
120120
}
121121

122+
.avatar {
123+
border-radius: 50%;
124+
border-style: solid;
125+
border-color: rgba(255, 255, 255, 0.75);
126+
background-clip: padding-box;
127+
background-color: white;
128+
border-width: 2px;
129+
vertical-align: middle;
130+
transition: .2s;
131+
}
132+
133+
summary .avatar {
134+
width: 2em;
135+
}
136+
137+
li .avatar {
138+
width: 3.5em;
139+
}
140+
141+
summary .avatar:hover {
142+
transform: scale(3);
143+
}
144+
145+
li .avatar:hover,
146+
.avatar-hover {
147+
transform: scale(2);
148+
}
149+
122150
/*]]>*/
123151
</style>
124152
</head>
@@ -257,6 +285,53 @@ <h2>Table of Contents</h2>
257285
targetElement.innerHTML = heading + fileElements.join('');
258286
}
259287

288+
function generateAvatar(profileHref) {
289+
return `<a href="${profileHref}"><img class="avatar" src="${profileHref}.png"/></a>`;
290+
}
291+
292+
function generateContributorSection(a, logicalHref) {
293+
const li = a.parentElement;
294+
if (li.localName == 'li') {
295+
const ul = li.parentElement;
296+
if (ul?.localName == 'ul') {
297+
const details = ul.parentElement;
298+
if (details?.localName == 'details') {
299+
const avatar = toElements(`<span>${generateAvatar(logicalHref)}&nbsp;</span>`)[0];
300+
li.insertBefore(avatar, a);
301+
302+
const summary = details.querySelector('summary');
303+
if (summary.children.length == 0) {
304+
summary.innerHTML += '&nbsp;';
305+
}
306+
summary.innerHTML += generateAvatar(logicalHref);
307+
308+
a.onmouseenter = () => {
309+
avatar.querySelector('img').classList.add('avatar-hover')
310+
};
311+
a.onmouseleave = () => {
312+
avatar.querySelector('img').classList.remove('avatar-hover')
313+
};
314+
315+
316+
// We only need to process the overall contributor section once.
317+
if (ul.style.listStyleType != 'none') {
318+
ul.style.listStyleType = 'none';
319+
ul.style.paddingLeft = '1em';
320+
321+
summary.onclick = () => {
322+
setTimeout(() => {
323+
const avatars = summary.querySelectorAll('*');
324+
for (const avatar of avatars) {
325+
avatar.style.display = details.open ? 'none' : 'inline-block';
326+
}
327+
}, 10);
328+
};
329+
}
330+
}
331+
}
332+
}
333+
}
334+
260335
function generateMarkdown(logicalBaseURL, response) {
261336
if (response instanceof Array) {
262337
generateFileList(response);
@@ -314,11 +389,15 @@ <h2>Table of Contents</h2>
314389

315390
const logicalHref = new URL(href, logicalBaseURL);
316391
if (!logicalHref.pathname.endsWith('.md')) {
317-
const siteURL = toSiteURL(logicalHref);
318-
if (siteURL != null) {
319-
a.href = siteURL;
320-
} else if (!href.startsWith('http')) {
321-
a.href = new URL(`https://github.com/${org}/${repo}/blob/${branch}/${path}/../${href}`);
392+
if (/^https:\/\/github.com\/[^\/]+$/.exec(logicalHref.toString())) {
393+
generateContributorSection(a, logicalHref);
394+
} else {
395+
const siteURL = toSiteURL(logicalHref);
396+
if (siteURL != null) {
397+
a.href = siteURL;
398+
} else if (!href.startsWith('http')) {
399+
a.href = new URL(`https://github.com/${org}/${repo}/blob/${branch}/${path}/../${href}`);
400+
}
322401
}
323402
continue;
324403
}
@@ -382,12 +461,10 @@ <h2>Table of Contents</h2>
382461

383462
function generatBreadcrumbPath(crumbPath, segment) {
384463
const fullPath = `${org}/${repo}/${branch}${crumbPath}`;
385-
console.log(fullPath);
386464

387465
for (const matcher of specializedBreadcrumpbs) {
388466
const match = matcher.pattern.exec(fullPath);
389467
if (match != null) {
390-
console.log(match);
391468
const replacement = matcher.replacement;
392469
return replacement;
393470
}

0 commit comments

Comments
 (0)