Skip to content

Commit 699c528

Browse files
committed
feat: migrated notes list to static generation instead of dynamic preact component
1 parent f4bb9e2 commit 699c528

File tree

7 files changed

+84
-68
lines changed

7 files changed

+84
-68
lines changed

site/package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"devDependencies": {
3131
"@types/mapbox-gl": "^3.1.0",
32+
"@types/markdown-it": "^14.1.1",
3233
"prettier": "^3.2.5",
3334
"prettier-plugin-astro": "^0.13.0"
3435
}

site/src/components/Note.astro

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
import markdownit from "markdown-it";
3+
4+
const { item } = Astro.props;
5+
6+
const md = markdownit({
7+
highlight: (str: string, lang: string) => {
8+
console.log(lang);
9+
return (
10+
'<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;"><code style="padding-left: 8px;">' +
11+
md.utils.escapeHtml(str) +
12+
"</code></pre>"
13+
);
14+
},
15+
});
16+
17+
const getFormattedDate = (date: string) => {
18+
const d = new Date(date);
19+
const browserLang = Astro.currentLocale;
20+
return `${d.toLocaleDateString(browserLang)} ${d.toLocaleTimeString(browserLang, { timeStyle: "short" })}`;
21+
};
22+
---
23+
24+
<li class="note-list-item">
25+
<article>
26+
<a href={`#${item.id}`} target="_blank">
27+
<h3 id={item.id}>{item.title}</h3>
28+
</a>
29+
<p class="note-list-item--time">{getFormattedDate(item.date_created)}</p>
30+
<p set:html={md.render(item.text)} />
31+
<hr />
32+
</article>
33+
</li>
34+
35+
<script is:inline define:vars={{ id: item.id }}>
36+
/**
37+
* Extract the hash link from URL and check if it matches the item id
38+
*/
39+
const applyActiveClass = () => {
40+
const hash = location.hash.replace("#", "");
41+
return hash && parseInt(hash) === id ? "note-list-item--active" : "";
42+
};
43+
44+
const hashClass = applyActiveClass();
45+
if (!hashClass) return;
46+
const listElement = document.getElementById(id).closest("li");
47+
listElement.classList.add(hashClass);
48+
</script>

site/src/components/NoteList.astro

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
import { fetchNotes } from "../functions/notes.func";
3+
import Note from "./Note.astro";
4+
5+
const items = await fetchNotes();
6+
---
7+
8+
<ol class="note-list">
9+
{items.map((item: any) => <Note item={item} />)}
10+
</ol>

site/src/components/dynamic/Note.jsx

Lines changed: 0 additions & 40 deletions
This file was deleted.

site/src/components/dynamic/NoteList.jsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

site/src/pages/notes.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
import Layout from "../layouts/Layout.astro";
3-
import NoteList from "../components/dynamic/NoteList";
3+
import NoteList from "../components/NoteList.astro";
44
import { Image } from "astro:assets";
55
66
import rssIcon from "../resources/rss.svg";
@@ -22,7 +22,7 @@ import rssIcon from "../resources/rss.svg";
2222
</h1>
2323
<p>Everything that pops in my mind and isn't a full blog post.</p>
2424
<ol class="note-list">
25-
<NoteList client:visible />
25+
<NoteList />
2626
</ol>
2727
</main>
2828
</Layout>

0 commit comments

Comments
 (0)