|
| 1 | +import * as params from '@params'; |
| 2 | + |
| 3 | +/** |
| 4 | + * |
| 5 | + */ |
| 6 | +async function getReposData() { |
| 7 | + for (repo of params.projects.repos) { |
| 8 | + await fetch(`https://api.github.com/repos/${params.projects.githubUsername}/${repo.url}`) |
| 9 | + .then(response => response.json()) |
| 10 | + .then(data => { |
| 11 | + repo.value = { |
| 12 | + name: data.name, |
| 13 | + description: data.description, |
| 14 | + url: data.html_url, |
| 15 | + stars: data.stargazers_count, |
| 16 | + forks: data.forks, |
| 17 | + }; |
| 18 | + }); |
| 19 | + }; |
| 20 | + |
| 21 | + return params.projects.repos; |
| 22 | +} |
| 23 | + |
| 24 | +$(document).ready(function() { |
| 25 | + const theme = localStorage.getItem('theme'); |
| 26 | + if (theme == 'dark') { |
| 27 | + $('html').attr('data-theme', 'dark'); |
| 28 | + } |
| 29 | + |
| 30 | + // Get lates blog posts. |
| 31 | + fetch(params.blog.sitemapUrl) |
| 32 | + .then(response => response.text()) |
| 33 | + .then(str => new window.DOMParser().parseFromString(str, 'text/xml')) |
| 34 | + .then(data => { |
| 35 | + const channel = data.childNodes[0].childNodes[0]; |
| 36 | + |
| 37 | + let posts = 0; |
| 38 | + for (let entity of channel.childNodes.values()) { |
| 39 | + if (posts > 3) { |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + if (entity.nodeName == 'item') { |
| 44 | + posts += 1; |
| 45 | + |
| 46 | + // Separate the text of the post from it's banner image. |
| 47 | + const postText = entity.getElementsByTagName("description")[0].textContent.replace('src="/', `src="${params.blog.url}/`); |
| 48 | + const regex = /<div class="featured-image">\n.+\n.+<\/div>/g; |
| 49 | + const postImage = postText.match(regex); |
| 50 | + const postDescription = postText.replace(regex, ''); |
| 51 | + let editedPostText = postDescription.split(' ', 60).join(' '); |
| 52 | + if (postDescription.split(' ').length > 60) { |
| 53 | + editedPostText += ' ...'; |
| 54 | + } |
| 55 | + |
| 56 | + $('.posts').append(` |
| 57 | + <div> |
| 58 | + <div class="uk-card uk-card-default"> |
| 59 | + <div class="uk-card-header"> |
| 60 | + <div class="uk-grid-small uk-flex-middle" uk-grid> |
| 61 | + <div class="uk-width-expand"> |
| 62 | + <h3 class="uk-card-title uk-margin-remove-bottom"><b>${entity.getElementsByTagName("title")[0].textContent}</b></h3> |
| 63 | + <p class="uk-text-meta uk-margin-remove-top"> |
| 64 | + <time datetime="2016-04-01T19:00">${entity.getElementsByTagName("pubDate")[0].textContent.split(':')[0].slice(0, -2)}</time> |
| 65 | + </p> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + <div class="uk-card-media-top"> |
| 70 | + ${postImage} |
| 71 | + </div> |
| 72 | + <div class="uk-card-body"> |
| 73 | + <p>${editedPostText}</p> |
| 74 | + </div> |
| 75 | + <div class="uk-card-footer"> |
| 76 | + <a href="${entity.getElementsByTagName("link")[0].textContent}" target="_blank" class="uk-button uk-button-text">Go to Post</a> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + `); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + // Get repos data from GitHub. |
| 86 | + getReposData() |
| 87 | + .then(repos => { |
| 88 | + repos.sort((a, b) => (a.value.stars < b.value.stars) ? 1 : -1); |
| 89 | + |
| 90 | + for (repo of repos) { |
| 91 | + $('.repos').append(` |
| 92 | + <div> |
| 93 | + <div class="uk-card uk-card-default"> |
| 94 | + <div class="uk-card-header"> |
| 95 | + <div class="uk-grid-small uk-flex-middle" uk-grid> |
| 96 | + <div class="uk-width-expand"> |
| 97 | + <h3 class="uk-card-title uk-margin-remove-bottom"><b>${repo.name}</b></h3> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + <div class="uk-card-media-top"> |
| 102 | + <a href="${repo.value.url}" target="_blank"> |
| 103 | + <img src="img/projects/${repo.url}.webp"> |
| 104 | + </a> |
| 105 | + </div> |
| 106 | + <div class="uk-card-body"> |
| 107 | + <p>${repo.value.description}</p> |
| 108 | + </div> |
| 109 | + <div class="uk-card-footer"> |
| 110 | + <span class="left"> |
| 111 | + <i class="fas fa-star"></i> |
| 112 | + ${repo.value.stars} |
| 113 | + </span> |
| 114 | + <span class="right"> |
| 115 | + <i class="fal fa-code-merge"></i> |
| 116 | + ${repo.value.forks} |
| 117 | + </span> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + `); |
| 122 | + }; |
| 123 | + }); |
| 124 | +}); |
0 commit comments