Piano notes book, powered by Astro and React.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

49 lignes
1.2 KiB

  1. ---
  2. import { readdir, readFile } from 'node:fs/promises';
  3. import Default from '../content/layouts/Default.astro';
  4. type Frontmatter = Record<string, unknown>;
  5. interface PageProps {
  6. frontmatter: Frontmatter;
  7. }
  8. const title = 'Table of Contents'
  9. const allPages = await readdir('src/content/chapters');
  10. const pages = allPages.filter((p) => (
  11. !p.startsWith('index.')
  12. && !p.endsWith('.astro')
  13. && p.endsWith('.mdx')
  14. ));
  15. const pagesContentImported = await Promise.all(
  16. pages.map(async (p) => {
  17. const fileBuffer = await readFile(`src/content/chapters/${p}`);
  18. const file = fileBuffer.toString('utf-8');
  19. const [, frontmatterRaw] = file.split('---');
  20. const frontmatterLines = (frontmatterRaw?.split('\n') ?? []) as string[];
  21. const frontmatter = Object.fromEntries(
  22. frontmatterLines.map(l => l.split(':').map((s) => s.trim()))
  23. ) as Frontmatter;
  24. return [
  25. p.replace('.mdx', '.html'),
  26. {
  27. frontmatter,
  28. },
  29. ] as [string, PageProps];
  30. })
  31. ) as [string, PageProps][];
  32. ---
  33. <Default title={title}>
  34. <ol>
  35. {pagesContentImported.map(([p, f]) => (
  36. <li>
  37. <a href={`chapters/${p.replace('.mdx', '')}`}>
  38. {f.frontmatter.title}
  39. </a>
  40. </li>
  41. ))}
  42. </ol>
  43. </Default>