· 3 min read · updated
An Awesome list with filters in the URL
Awesome lists are YAML files in content/awesome/, one per list: categories, then links with a title, url, description and tags. A list is data, not prose, so…
What we did
- Awesome lists are YAML files in
content/awesome/, one per list: categories, then links with a title, url, description and tags. A list is data, not prose, so no markdown. The file name is the URL:modern-css.yaml→/awesome/modern-css. src/lib/awesome/awesome.tsloads them at build time (import.meta.globwith?rawand theyamlpackage, likecontent.ts). A link that isn'thttps://or a tag that isn't kebab-case fails the build.checkTagsis now exported fromcontent.tsfor this.LinkFilter(filter.svelte.ts) holds the search text and the picked tags as$state, and the matching links and their count as$derived. It turns itself into URL params and reads them back.AwesomeList.svelteshows a search box, tag toggle buttons (aria-pressed), a live count (role="status") and a "Clear filters" button. The filter goes into the URL (?q=grid&tag=docs), so a filtered view can be shared./awesomelists the lists. The first one, Modern CSS, has 25 links taken from the docs this Devlog already cites.
What I learned
- Runes work in classes, but only in
.svelteand.svelte.tsfiles. A class keeps the state, what's derived from it and the ways to change it in one place, and a component gets all of it as one object. $derivedis lazy:resultsis recomputed only when it's read and something it uses has changed.- The class gets the categories as a function (
() => list.categories), not the array. A plain array would be read once, when the class is created. {#key list.slug}makes a fresh component when the list changes. Otherwise moving from one list to another would keep the old filter.- A function binding,
bind:value={() => get, (v) => set}, runs my code right where the value changes. I use it to update the URL after the filter, in that order. - The filters only render once
onMounthas run. Without JS they'd be controls that do nothing, and the prerendered page already has every link. - The descriptions are our own data, shown as text with
{…}, which Svelte escapes. Only HTML through{@html}would need sanitizing. page.url.searchParamsis read-only.read()asks only forPick<URLSearchParams, 'get' | 'getAll'>, the two methods it uses, so the read-only params fit.
Problems & fixes
- "Clear filters" hides itself once nothing is filtered, and focus fell to
<body>. Clearing now focuses the search box. - An unknown
?tag=from a link would have hidden every link behind a button that doesn't exist.read()keeps only the list's own tags.
Links
- Svelte:
$statein classes - Svelte:
$derived - Svelte: function bindings
- Svelte:
{#key} - MDN:
<search> - MDN: aria-pressed
Next
- Review the Modern CSS list (descriptions, what's missing), then open the Phase 8 PR.