diff --git a/README.md b/README.md index 7091c31..af0b8cc 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,16 @@ Dates display in the selected language using UTC. Reading time is estimated at 200 words per minute, with a minimum of one minute. Set optional `youtubeId` to an 11-character YouTube video ID to show a player. +### Article contents + +Articles with at least one Markdown `##` or `###` heading automatically show an +index ("Indice" in Italian, "Contents" in English), with subsections nested under +their parent section. At desktop widths of 1100px and above, the index stays in a +sticky right sidebar; on smaller screens it appears in an expandable box before +the article body. The current section is highlighted while scrolling. Links work +without JavaScript, and articles without these headings keep the single-column +layout. No frontmatter setting or manually maintained list is needed. + ### Tags Use canonical English tag identifiers shared across both languages, for example diff --git a/src/components/TableOfContents.astro b/src/components/TableOfContents.astro new file mode 100644 index 0000000..9175514 --- /dev/null +++ b/src/components/TableOfContents.astro @@ -0,0 +1,85 @@ +--- +import type { MarkdownHeading } from 'astro'; + +interface Props { + headings: MarkdownHeading[]; + label: string; + showTitle?: boolean; +} + +const { headings, label, showTitle = false } = Astro.props; +const sections: { heading: MarkdownHeading; children: MarkdownHeading[] }[] = []; +for (const heading of headings) { + const parent = sections.at(-1); + if (heading.depth === 3 && parent?.heading.depth === 2) { + parent.children.push(heading); + } else { + sections.push({ heading, children: [] }); + } +} +--- + + + diff --git a/src/layouts/Base.astro b/src/layouts/Base.astro index 916a15a..e9bb9f8 100644 --- a/src/layouts/Base.astro +++ b/src/layouts/Base.astro @@ -9,8 +9,9 @@ interface Props { lang: Language; alternatives?: { lang: Language; url: string }[]; activeNav?: 'posts' | 'archive' | 'search' | 'tags' | 'about' | 'faq'; + wideArticle?: boolean; } -const { title, description, lang, alternatives = [], activeNav } = Astro.props; +const { title, description, lang, alternatives = [], activeNav, wideArticle = false } = Astro.props; const canonical = new URL(Astro.url.pathname, Astro.site); const labels = lang === 'it' ? { posts: 'Articoli', archive: 'Archivio', search: 'Cerca', tags: 'Tags', about: 'Chi siamo', faq: 'FAQ', nav: 'Navigazione principale', languages: 'Lingua', skip: 'Vai al contenuto', theme: 'Tema scuro' } @@ -75,7 +76,7 @@ const navigation = [ -
+