From eec330e214d44cb2421a0e5e81cdc47dab59377c Mon Sep 17 00:00:00 2001 From: Andrew Curtis <80860310+meisZWFLZ@users.noreply.github.com> Date: Thu, 11 Jul 2024 01:57:31 -0700 Subject: [PATCH 1/2] feat: :sparkles: Sort entries by date --- internals.typ | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internals.typ b/internals.typ index e503096..d5e1baf 100644 --- a/internals.typ +++ b/internals.typ @@ -27,7 +27,7 @@ #let print-entries(theme: (:)) = { let print-helper(section, state) = { locate(loc => { - for entry in state.final(loc) [ + for entry in state.final(loc).sorted(key: entry=>entry.ctx.date) [ #let entry-func = fallback-to-default(section + "-entry", theme) #let body = [] + entry.body #entry-func(body, ctx: entry.ctx) From 454c78810777ebb126e0eac9548fe39ab80b4ac9 Mon Sep 17 00:00:00 2001 From: meisZWFLZ Date: Thu, 11 Jul 2024 04:56:38 -0700 Subject: [PATCH 2/2] feat: :sparkles: sort toc, add links to toc, and fix page numbers --- entries.typ | 16 ++++++++-------- internals.typ | 5 +++-- themes/radial/components/toc.typ | 5 +++-- utils.typ | 12 ++++++++++-- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/entries.typ b/entries.typ index 7750c05..8db5d2a 100644 --- a/entries.typ +++ b/entries.typ @@ -18,23 +18,23 @@ witness: "", body, ) = { - let (state, entry-label) = if section == "frontmatter" { - (globals.frontmatter-entries, label("notebook-frontmatter")) + let (state, entry-type) = if section == "frontmatter" { + (globals.frontmatter-entries, "notebook-frontmatter") } else if section == "body" { - (globals.entries, label("notebook-body")) + (globals.entries, "notebook-body") } else if section == "appendix" { - (globals.appendix-entries, label("notebook-appendix")) + (globals.appendix-entries, "notebook-appendix") } else { panic("No valid entry type selected") } state.update( entries => { + // let unique-label = label(entry-type + ":" + str(entries.len())) + let entry-type-label = label(entry-type) // Inject the proper labels and settings changes into the user's entry body - let final-body = if entries.len() == 0 { - [#counter(page).update(1)] // Correctly set the page number for each section - } + [ - #metadata(none) #entry-label + let final-body = [ + #metadata(none) #entry-type-label #counter(footnote).update(0) ] + body // Place a label on blank content to the table of contents can find each entry diff --git a/internals.typ b/internals.typ index e503096..f0c667c 100644 --- a/internals.typ +++ b/internals.typ @@ -1,5 +1,6 @@ #import "./globals.typ" #import "./themes/themes.typ" +#import "/utils.typ" #let fallback-to-default(key, theme) = { let component = theme.at(key, default: none) @@ -27,9 +28,9 @@ #let print-entries(theme: (:)) = { let print-helper(section, state) = { locate(loc => { - for entry in state.final(loc) [ + for (index, entry) in utils.sort_entries(state.final(loc)).enumerate() [ #let entry-func = fallback-to-default(section + "-entry", theme) - #let body = [] + entry.body + #let body = if index == 0 [#counter(page).update(1)] else [] + entry.body #entry-func(body, ctx: entry.ctx) ] }) diff --git a/themes/radial/components/toc.typ b/themes/radial/components/toc.typ index fc12f04..eefd7d8 100644 --- a/themes/radial/components/toc.typ +++ b/themes/radial/components/toc.typ @@ -22,8 +22,9 @@ #h(5pt) #entry.title #box(width: 1fr, line(length: 100%, stroke: (dash: "dotted"))) - #entry.page-number - ],) + #link((page: entry.global-page-number,x:0mm,y:0mm))[#entry.page-number] + ] + ,) }) linebreak() diff --git a/utils.typ b/utils.typ index 72d1516..d12919a 100644 --- a/utils.typ +++ b/utils.typ @@ -1,5 +1,12 @@ #import "/globals.typ" +#let sort_entries(entries) = { + return entries.map(entry=>{ + if entry.ctx.date == none {entry.ctx.insert("date", datetime(year: 0, day: 1, month: 1))} + return entry + }).sorted(key: entry=>entry.ctx.date) +} + // TODO: document what ctx provides to the callback /// Utility function to help themes implement a table of contents. /// @@ -34,10 +41,11 @@ let result = () - for (index, entry) in state.final(loc).enumerate() { - let page-number = counter(page).at(markers.at(index).location()).at(0) + for (entry,marker) in sort_entries(state.final(loc)).zip(markers) { + let page-number = counter(page).at(marker.location()).at(0) let ctx = entry.ctx ctx.page-number = page-number + ctx.global-page-number = marker.location().page() result.push(ctx) } return result