Skip to content

Instantly share code, notes, and snippets.

@steinbrueckri
Created March 12, 2020 13:49
Show Gist options
  • Save steinbrueckri/99e12dcaec2e2423825c763fc9b40864 to your computer and use it in GitHub Desktop.
Save steinbrueckri/99e12dcaec2e2423825c763fc9b40864 to your computer and use it in GitHub Desktop.
const isDev = ['localhost', '127.0.0.1'].includes(location.hostname);
const dom = {
select: document.querySelector.bind(document),
slectAll: document.querySelectorAll.bind(document)
};
const injectScript = (source, callback) => {
const script = document.createElement('script');
script.src = source;
script.addEventListener('load', callback);
document.head.appendChild(script);
};
const insertHypenationHintsForCamelCase = string => string.replace(/([a-z])([A-Z])/g, '$1\u00AD$2');
(async () => {
const username = 'sindresorhus';
const email = 'sindresorhus@gmail.com';
const response = await fetch(`https://api.github.com/users/${username}/events/public`);
const json = await response.json();
let latestCommit;
const latestPushEvent = json.find(event => {
if (event.type !== 'PushEvent') {
return false;
}
latestCommit = event.payload.commits.reverse().find(commit => commit.author.email === email);
return Boolean(latestCommit);
});
if (!latestCommit) {
dom.select('#latest-commit').textContent = 'No commit';
return;
}
const {
repo,
created_at: createdAt
} = latestPushEvent;
const repoUrl = `https://github.com/${repo.name}`;
const commitTitleElement = dom.select('#latest-commit .commit-title');
commitTitleElement.href = `${repoUrl}/commit/${latestCommit.sha}`;
const commitMessageLines = latestCommit.message.trim().split('\n');
commitTitleElement.title = commitMessageLines.slice(1).join('\n').trim();
commitTitleElement.textContent = commitMessageLines[0].trim();
const commitDateElement = dom.select('#latest-commit .commit-date');
commitDateElement.textContent = timeago().format(createdAt);
const repoTitleElement = dom.select('#latest-commit .repo-title');
repoTitleElement.href = repoUrl;
repoTitleElement.textContent = repo.name.replace(new RegExp(`^${username}/`), '');
})();
(async () => {
const textColorFromBackgroundColor = color => {
if (color.length < 5) {
color += color.slice(1);
}
return parseInt(color.replace('#', '0x'), 16) > (0xFFFFFF / 2) ? '#333' : '#fff';
};
const url = isDev ? 'http://localhost:3000' : 'https://gh-latest-repos.now.sh';
const json = await (await fetch(url)).json();
const template = dom.select('#latest-repos-template');
const container = dom.select('#latest-repos');
for (const repo of json.reverse()) {
if (!repo.description) {
continue;
}
const content = template.cloneNode(true).content;
const a = content.querySelector('.latest-repos-title');
a.href = repo.url;
a.textContent = insertHypenationHintsForCamelCase(repo.name);
if (repo.primaryLanguage) {
const lang = content.querySelector('.latest-repos-language');
lang.textContent = repo.primaryLanguage.name;
lang.style.color = textColorFromBackgroundColor(repo.primaryLanguage.color);
lang.style.backgroundColor = repo.primaryLanguage.color;
const query = `user:sindresorhus language:${repo.primaryLanguage.name.toLowerCase()}`;
const url = new URL('https://github.com/search');
url.searchParams.append('q', query);
lang.href = url;
}
content.querySelector('.latest-repos-description').textContent = repo.description;
container.appendChild(document.importNode(content, true));
}
dom.select('#projects').style.opacity = 1;
})();
dom.select('#unicorn-btn').addEventListener('click', event => {
event.preventDefault();
injectScript('cornify.js', () => {
cornify();
cornify();
setInterval(cornify, 1000);
});
}, {
once: true
}); {
function hideScrollHint() {
if (window.scrollY > 100) {
document.removeEventListener('scroll', hideScrollHint);
dom.select('#scroll-hint').classList.add('hidden');
}
}
document.addEventListener('scroll', hideScrollHint, {
passive: true
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment