Skip to content

Commit bdeb402

Browse files
committed
add link to individual post
1 parent cb45461 commit bdeb402

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

index.html

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,32 @@
246246
return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
247247
}
248248

249+
async function renderSinglePost(dirName) {
250+
fileContent.innerHTML = '<div class="small">Loading single post...</div>';
251+
fileTitle.textContent = dirName;
252+
fileMeta.textContent = '';
253+
try {
254+
const postsPath = 'posts';
255+
const baseUrl = `https://api.github.com/repos/${owner}/${repo}/contents`;
256+
const dirUrl = `${baseUrl}/${encodeURIComponent(postsPath)}/${encodeURIComponent(dirName)}`;
257+
const dirContents = await apiFetch(dirUrl);
258+
const readmeFile = dirContents.find(f => /^readme(\.(md|markdown))?$/i.test(f.name));
259+
if (!readmeFile) {
260+
fileContent.innerHTML = '<div class="small">No README found for this post.</div>';
261+
return;
262+
}
263+
const fileData = await apiFetch(readmeFile.url);
264+
const decoded = decodeBase64ToUtf8(fileData.content || '');
265+
const html = marked.parse(decoded || '');
266+
fileContent.innerHTML = html;
267+
rewriteRelativeImageSrcs(fileContent, `${postsPath}/${dirName}`);
268+
fileContent.querySelectorAll('pre code').forEach(block => hljs.highlightElement(block));
269+
setStatus('');
270+
} catch (err) {
271+
fileContent.innerHTML = `<div class="small" style="color:#ffb4b4">Failed to load post: ${err.message}</div>`;
272+
}
273+
}
274+
249275
async function loadPosts(owner, repo) {
250276
if (!owner || !repo) {
251277
setStatus('Owner and repo required', true);
@@ -366,9 +392,17 @@
366392
header.style.alignItems = 'center';
367393
header.style.marginBottom = '6px';
368394

369-
const titleDiv = document.createElement('div');
395+
const titleDiv = document.createElement('a');
396+
titleDiv.href = '#';
397+
titleDiv.className = 'link';
370398
titleDiv.style.fontWeight = '700';
399+
titleDiv.style.textDecoration = 'none';
400+
titleDiv.style.cursor = 'pointer';
371401
titleDiv.textContent = dir.name;
402+
titleDiv.addEventListener('click', (e) => {
403+
e.preventDefault();
404+
renderSinglePost(dir.name);
405+
});
372406

373407
const badge = document.createElement('div');
374408
badge.className = 'small';

0 commit comments

Comments
 (0)