-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (74 loc) · 2.94 KB
/
script.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Fetch from GitHub and render content
async function fetchAndRenderContent(gitHubFolderPath, containerId, renderFunction) {
const container = document.getElementById(containerId);
const response = await fetch(`https://api.github.com/repos/machinetown44/Dev-Journey/contents/${gitHubFolderPath}`);
const files = await response.json();
renderFunction(files, container);
}
// Render Markdown CV
async function renderMarkdownCV(files, container) {
const markdownFile = files.find(file => file.name.endsWith('.md'));
const response = await fetch(markdownFile.download_url);
const text = await response.text();
// Convert Markdown to HTML and add to container
container.innerHTML = converter.makeHtml(text);
}
// Render media files
async function renderMedia(files, container) {
const mediaFiles = files.filter(file => file.name.endsWith('.mp4') || file.name.endsWith('.gif')).reverse();
mediaFiles.forEach(async file => {
const mediaName = file.name;
const descriptionName = mediaName.split('.')[0] + '.md';
const descriptionFile = files.find(f => f.name === descriptionName);
if (descriptionFile) {
const response = await fetch(descriptionFile.download_url);
const descriptionText = await response.text();
renderMediaBlock(descriptionText, file, container);
} else {
renderMediaBlock(null, file, container);
}
});
}
// Intersection Observer for animation
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('slide-in-right');
}
});
});
function renderMediaBlock(descriptionText, file, container) {
const mediaItem = document.createElement('div');
mediaItem.className = 'media-item';
observer.observe(mediaItem); // Observe this element
const separator = document.createElement('hr');
const description = document.createElement('p');
const mediaElement = file.name.endsWith('.mp4') ? document.createElement('video') : document.createElement('img');
mediaElement.crossOrigin = "anonymous";
if (descriptionText) {
description.style.flex = "1";
mediaElement.style.flex = "2";
const descriptionHtml = converter.makeHtml(descriptionText);
description.innerHTML = descriptionHtml;
mediaItem.appendChild(description);
} else {
// Center if there is no description
mediaItem.style.justifyContent = "center";
mediaElement.style.clipPath = "none";
}
mediaElement.src = file.download_url;
if (file.name.endsWith('.mp4')) {
mediaElement.controls = true;
}
mediaItem.appendChild(mediaElement);
container.appendChild(mediaItem);
container.appendChild(separator);
}
const converter = new showdown.Converter();
// Initialization function
window.addEventListener('load', () => {
// Fetch and render CV
fetchAndRenderContent('.', 'cv-container', renderMarkdownCV);
// Fetch and render media
fetchAndRenderContent('media', 'media-container', renderMedia);
});