refactor: bookmarks becomes a content collection with auto-id
This commit is contained in:
parent
23a267a242
commit
1d8ef601bc
6 changed files with 33 additions and 57 deletions
|
|
@ -2,18 +2,11 @@
|
|||
import { getCollection } from 'astro:content';
|
||||
import Layout from '../layouts/Layout.astro';
|
||||
import yaml from 'js-yaml';
|
||||
import bookmarksRaw from '../content/bookmarks.yaml?raw';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { getApprovedEntries, type GuestbookEntry } from '../lib/db';
|
||||
import { getGitDate } from '../utils';
|
||||
|
||||
interface Bookmark {
|
||||
date: string;
|
||||
title: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
interface TxtFile {
|
||||
name: string;
|
||||
date: Date;
|
||||
|
|
@ -50,8 +43,9 @@ for (const category of sortedCategories) {
|
|||
});
|
||||
}
|
||||
|
||||
const bookmarks = (yaml.load(bookmarksRaw) as Bookmark[])
|
||||
.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
||||
const bookmarksCollection = await getCollection('bookmarks');
|
||||
const bookmarks = bookmarksCollection
|
||||
.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
|
||||
|
||||
// Auto-discover txt files from public/txt/
|
||||
const txtDir = path.join(process.cwd(), 'public/txt');
|
||||
|
|
@ -89,11 +83,6 @@ function formatDate(date: Date): string {
|
|||
return `${d}/${m}/${y}`;
|
||||
}
|
||||
|
||||
function formatBookmarkDate(dateStr: string): string {
|
||||
const date = new Date(dateStr);
|
||||
return formatDate(date);
|
||||
}
|
||||
|
||||
function extractDomain(url: string): string {
|
||||
try {
|
||||
const parsed = new URL(url);
|
||||
|
|
@ -129,7 +118,7 @@ function extractDomain(url: string): string {
|
|||
<details open>
|
||||
<summary>bookmarks</summary>
|
||||
<pre set:html={[
|
||||
...bookmarks.slice(0, 10).map(b => `<span class="muted">${formatBookmarkDate(b.date)}</span> <a href="${b.url}">${b.title}</a> <span class="muted">(${extractDomain(b.url)})</span>`),
|
||||
...bookmarks.slice(0, 10).map(b => `<span class="muted">${formatDate(b.data.date)}</span> <a href="${b.data.url}">${b.data.title}</a> <span class="muted">(${extractDomain(b.data.url)})</span>`),
|
||||
...(bookmarks.length > 10 ? [`<a href="/bookmarks/">+${bookmarks.length - 10} more</a>`] : [])
|
||||
].join('\n')} />
|
||||
</details>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue