diff options
| author | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2024-06-05 09:38:10 +0100 |
|---|---|---|
| committer | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2024-06-05 09:38:10 +0100 |
| commit | f977ee338c7eff240c26c793d7340de4a7b5788b (patch) | |
| tree | e5803001ec972c176e7a87884455ff2dcad7c6c7 /website/src/components/LinksFeed.astro | |
| parent | b5a434c9596f6792f8f574c5641d4cbe1dd5c289 (diff) | |
Adds Links page and fixes blog feed
Diffstat (limited to 'website/src/components/LinksFeed.astro')
| -rw-r--r-- | website/src/components/LinksFeed.astro | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/website/src/components/LinksFeed.astro b/website/src/components/LinksFeed.astro new file mode 100644 index 0000000..a428395 --- /dev/null +++ b/website/src/components/LinksFeed.astro @@ -0,0 +1,77 @@ +--- +import FormattedDate from '../components/FormattedDate.astro'; +import LINKS from '../data/links.ts'; + +export interface Props { + headingLevel?: 1 | 2 | 3 | 4 | 5 | 6, + hideAuthor?: boolean, +}; + +const { headingLevel = 2, hideAuthor = false } = Astro.props; + +const headingElem = `h${headingLevel}`; +const subHeadingElem = `h${headingLevel + 1}` + +const distinctYears: string[] = LINKS + .map(link => link.isoDateAdded.slice(0,4)) + .reduce<string[]>((acc, curr) => acc.includes(curr) ? acc : [...acc, curr], []) + .sort((a, b) => Number.parseInt(b) - Number.parseInt(a)); + +function matchesYear(year: string) { + return (link: (typeof LINKS)[number]) => link.isoDateAdded.slice(0,4) === year; +} + +function sortByDateAddedDescending(link1: (typeof LINKS)[number], link2: (typeof LINKS)[number]) { + const year1 = Number.parseInt(link1.isoDateAdded.slice(0, 4)); + const year2 = Number.parseInt(link2.isoDateAdded.slice(0, 4)); + const month1 = Number.parseInt(link1.isoDateAdded.slice(5, 7)); + const month2 = Number.parseInt(link2.isoDateAdded.slice(5, 7)); + const day1 = Number.parseInt(link1.isoDateAdded.slice(8, 10)); + const day2 = Number.parseInt(link2.isoDateAdded.slice(8, 10)); + + if (year1 !== year2) { + return year2 - year1; + } else if (month1 !== month2) { + return month2 - month1; + } else { + return day2 - day1; + } +} + +const canonicalLinksUrl = new URL('links', Astro.site) +--- + +<section class="h-feed"> + <Fragment set:html={` + <${headingElem} class="p-name"> + My links + </${headingElem}> + `} /> + + <aside> + <p hidden={hideAuthor}> + These links are collected by <a class="p-author h-card" href="/">Joe Carstairs</a> + </p> + + <p hidden> + <a class="u-url" href={canonicalLinksUrl}>Permalink</a> + </p> + </aside> + + { distinctYears.map(year => ( + <Fragment set:html={` + <${subHeadingElem}> + ${year} + </${subHeadingElem}> + `} /> + <ul> + { LINKS.filter(matchesYear(year)).sort(sortByDateAddedDescending).map(link => ( + <li class="h-entry e-content"> + <a class="u-url p-name" href={link.href}>{link.title}</a>. + {link.description} + Added: <FormattedDate date={link.isoDateAdded} /> + </li> + )) } + </ul> + )) } +</section> |
