Skip to content

Commit b1b4698

Browse files
committed
minor API changes and additional docs
1 parent ad69f70 commit b1b4698

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

README.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ document. By default, it will assume that it is used in the header of your docum
44
the last heading if and only if it is numbered and the next heading is not the first on the current
55
page.
66

7-
By default hdyra assumes that you use `a4` page size, see the FAQ if you use different page size or
8-
margins.
7+
By default hydra also assumes that you use `a4` page size, see the FAQ if you use different page
8+
size or margins.
99

1010
## Example
1111
```typst
1212
#import "@preview/hydra:0.0.1": hydra
13-
#set page(header: hydra())
13+
#set page(header: #[hydra() #line(length: 100%)])
1414
#set heading(numbering: "1.1")
1515
#show heading.where(level: 1): it => pagebreak(weak: true) + it
1616
@@ -32,20 +32,41 @@ margins.
3232
![example2][example2]
3333

3434
## Non-default behavior
35+
Changing the default behavior can be done using its keyword arguments:
36+
```typst
37+
#let hydra(
38+
sel: heading, // the elements to consider
39+
getter: default.get-adjacent, // gets the neighboring elements according to sel
40+
prev-filter: default.prev-filter, // checks if the last element is valid
41+
next-filter: default.next-filter, // checks if the next element is valid
42+
display: default.display, // displays the last element
43+
resolve: default.resolve, // contains the glue code combining the other given args
44+
is-footer: false, // whether this is used from a footer
45+
) = {
46+
...
47+
}
48+
```
49+
These functions generally take a queried element and sometimes the current location, see the source
50+
for more info. The defaults assume only headings and fail if another element type is provided.
51+
52+
The `sel` argument can be an element function or selector, or either an array containing either
53+
of those and an addiitonal filter function. The additional filter function is applied before the
54+
adjacent arguments are selected from the result of the queries.
55+
3556
### Configuring filter and display
36-
By default it will hydra will display `[#numbering #body]` of the heading and this reject unnumbered
37-
ones. This filtering can be configured using `prev-filter` and `next-filter`, if those are changed
38-
to include unnumbered headings `display` be changed too.
57+
By default hydra will display `[#numbering #body]` of the heading and this reject unnumbered
58+
ones. This filtering can be configured using `prev-filter` and `next-filter`.
3959
```typst
40-
#set page(header: hydra(prev-filter: _ => true, display: h => h.body))
60+
#set page(header: hydra(prev-filter: (_, _) => true))
4161
```
4262

4363
Keep in mind that `next-filter` is also responsible for checking that the next heading is on the
4464
current page.
4565

4666
### In the footer
4767
To use the hydra functon in the footer of your doc, pass `is-footer: true` and place a
48-
`#metadata(()) <hydra>` somewhere in your header.
68+
`#metadata(()) <hydra>` somewhere in your header, or before your headings. Hydra will use the
69+
location of this label to search for the correct headings instead of searching from the footer.
4970

5071
```typst
5172
#set page(header: [#metadata(()) <hydra>], footer: hydra(is-footer: true))
@@ -62,7 +83,7 @@ specific levels of headings, pass the appropriate selector.
6283
#set page(header: hydra(sel: heading.where(level: 1)))
6384
6485
// only consider level 1 - 3
65-
#set page(header: hydra(sel: (heading, h => h.level <= 3)))
86+
#set page(header: hydra(sel: (heading, (h, _) => h.level <= 3)))
6687
6788
// consider also figures with this kind, must likely override all default functions other than
6889
// resolve, or resolve directly, see source

src/default.typ

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
loc = query(selector(<hydra>).before(loc), loc).last().location()
88
}
99

10-
let prev = query(sel.before(loc), loc).filter(filter)
11-
let next = query(sel.after(loc), loc).filter(filter)
10+
let prev = query(sel.before(loc), loc).filter(x => filter(x, loc))
11+
let next = query(sel.after(loc), loc).filter(x => filter(x, loc))
1212

1313
let prev = if prev != () { prev.last() }
1414
let next = if next != () { next.first() }
@@ -36,8 +36,10 @@
3636
#let display(element, loc) = {
3737
import "/src/util.typ": assert-element
3838
assert-element(element, heading)
39-
numbering(element.numbering, ..counter(heading).at(element.location()))
40-
[ ]
39+
if element.numbering != none {
40+
numbering(element.numbering, ..counter(heading).at(element.location()))
41+
[ ]
42+
}
4143
element.body
4244
}
4345

src/util.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// split a selector into a pair of selector and post filter
88
#let into-sel-filter-pair(sel) = {
99
if type(sel) in (selector, function) {
10-
(selector(sel), _ => true)
10+
(selector(sel), (_, _) => true)
1111
} else if (
1212
type(sel) == array and sel.len() == 2
1313
and type(sel.at(0)) in (selector, function) and type(sel.at(1)) == function

0 commit comments

Comments
 (0)