Skip to content

feat: ✨ Sort entries by date #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions entries.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions internals.typ
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
]
})
Expand Down
5 changes: 3 additions & 2 deletions themes/radial/components/toc.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 10 additions & 2 deletions utils.typ
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -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
Expand Down
Loading