Skip to content
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

Fixed TOC nesting with non-sequential headings #1059

Merged
merged 3 commits into from
Oct 25, 2023
Merged

Fixed TOC nesting with non-sequential headings #1059

merged 3 commits into from
Oct 25, 2023

Conversation

mossr
Copy link
Contributor

@mossr mossr commented Oct 24, 2023

When using \toc with a page that has nested headings that are not sequential, you currently get the following (which has h4 elements after h1 elements):

Before

franklin-toc-before

After

With this PR, I fix it so you skip adding empty nested lists to the TOC:

franklin-toc-after

Egregious MWE

Here's a MWE that's pretty egregious:

Before

franklin-toc-agg-before

After

franklin-toc-agg-after

MWE

Note, this with the following config.md

mintoclevel = 1
maxtoclevel = 7
@def title = "TOC Testing"

\toc

# H1

### H3

#### H4

## H2

## H2

##### H5

# H1

###### H6

## H2

## H2

#### H4

# H1

#### H4

#### H4

###### H6

#### H4

#### H4

@tlienart
Copy link
Owner

tlienart commented Oct 24, 2023

This is awesome, thanks a lot! code lgtm, will wait for CI to pass then do a patch release with this.

Ah it looks like the relevant test is failing, could you take a look?

@mossr
Copy link
Contributor Author

mossr commented Oct 24, 2023

Of course, I'll take a look

@mossr
Copy link
Contributor Author

mossr commented Oct 24, 2023

This test made me rethink how it should be handled. If we skip the indent entirely, then you may end up with headers that are actually nested under a top-level header but wouldn't appear as such in the TOC.

So I changed it so that the nesting is still added, but we hide the marker for the "skipped" sublists.

Test

old new
image image

Previous example

Notice now how all the h#'s line up properly.
image

@mossr
Copy link
Contributor Author

mossr commented Oct 24, 2023

Actually wait to merge this in. I think we can have the best of both worlds. Gimme a second

@mossr
Copy link
Contributor Author

mossr commented Oct 25, 2023

("a second == 4 hours later") I made the changes so both behaviors can co-exists. We only add empty nested items when appropriate. Here's what it would look like:

image

for something like

\toc
### Part of family 1: should not be nested `fd`

## Top-most ancestor of family 2
#### should be empty nested
### part of family 2

## Top-most ancestor of family 3
#### child1
#### child2
#### child3
#### child4

## Top-most ancestor of family 4
done.

I've updated the test to include this.

@tlienart
Copy link
Owner

Haha who would have thought that tocs are so interesting 😂 thanks a lot for all your work!

@tlienart tlienart merged commit 23bb45e into tlienart:master Oct 25, 2023
1 check passed
@mossr
Copy link
Contributor Author

mossr commented Oct 25, 2023

Of course, thank YOU for Franklin.jl! I love it so much that I redid my entire website using it

@yangcht
Copy link

yangcht commented Nov 6, 2023

Hej! I have been using \toc without problems but after recent updates. It starts to create errors like this. Do you have any idea how I can fix this? Or is this a bug? Thanks! @mossr

⋮ shutting down LiveServer… ✓
ERROR: ArgumentError: invalid Array dimensions
Stacktrace:
 [1] serve(fw::LiveServer.SimpleWatcher; host::String, port::Int64, dir::String, debug::Bool, verbose::Bool, coreloopfun::Franklin.var"#267#270"{…}, preprocess_request::typeof(identity), inject_browser_reload_script::Bool, launch_browser::Bool, allow_cors::Bool)
   @ LiveServer ~/.julia/packages/LiveServer/3Dvy0/src/server.jl:658
 [2] serve (repeats 2 times)
   @ ~/.julia/packages/LiveServer/3Dvy0/src/server.jl:578 [inlined]
 [3]
   @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/franklin.jl:140
 [4] serve()
   @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/franklin.jl:49
 [5] top-level scope
   @ REPL[3]:1

caused by: ArgumentError: invalid Array dimensions
Stacktrace:
  [1] Array
    @ Core ./boot.jl:475 [inlined]
  [2] Array
    @ Core ./boot.jl:484 [inlined]
  [3] fill
    @ Base ./array.jl:582 [inlined]
  [4] fill(v::Nothing, dims::Int64)
    @ Base ./array.jl:580
  [5] hfun_toc(params::Vector{String})
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/converter/html/functions.jl:244
  [6] top-level scope
    @ none:1
  [7] eval
    @ Core ./boot.jl:383 [inlined]
  [8] eval
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/Franklin.jl:1 [inlined]
  [9] convert_html_fblock(β::Franklin.HFun)
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/converter/html/functions.jl:18
 [10] process_html_qblocks(hs::String, qblocks::Vector{Franklin.AbstractBlock}, head::Int64, tail::Int64)
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/converter/html/html.jl:122
 [11] process_html_qblocks(hs::String, qblocks::Vector{Franklin.AbstractBlock})
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/converter/html/html.jl:102
 [12] convert_html(hs::String)
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/converter/html/html.jl:22
 [13] map
    @ Base ./tuple.jl:293 [inlined]
 [14] map
    @ Base ./tuple.jl:294 [inlined]
 [15] write_page(output_path::String, content::String; head::String, pgfoot::String, foot::String)
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/write_page.jl:108
 [16] write_page
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/write_page.jl:75 [inlined]
 [17] convert_and_write(root::String, file::String, head::String, pgfoot::String, foot::String, output_path::String)
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/write_page.jl:215
 [18] process_file_err(case::Symbol, fpair::Pair{String, String}, head::String, pgfoot::String, foot::String, t::Float64)
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/file_utils.jl:153
 [19] process_file(::Symbol, ::Pair{String, String}, ::String, ::Vararg{Any})
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/file_utils.jl:104
 [20] fd_loop(cycle_counter::Int64, ::LiveServer.SimpleWatcher, watched_files::@NamedTuple{…})
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/franklin.jl:408
 [21] #267
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/franklin.jl:136 [inlined]
 [22] serve(fw::LiveServer.SimpleWatcher; host::String, port::Int64, dir::String, debug::Bool, verbose::Bool, coreloopfun::Franklin.var"#267#270"{…}, preprocess_request::typeof(identity), inject_browser_reload_script::Bool, launch_browser::Bool, allow_cors::Bool)
    @ LiveServer ~/.julia/packages/LiveServer/3Dvy0/src/server.jl:648
 [23] serve (repeats 2 times)
    @ ~/.julia/packages/LiveServer/3Dvy0/src/server.jl:578 [inlined]
 [24]
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/franklin.jl:140
 [25] serve()
    @ Franklin ~/.julia/packages/Franklin/UskEZ/src/manager/franklin.jl:49
 [26] top-level scope
    @ REPL[3]:1
Some type information was truncated. Use `show(err)` to see complete types.

@tlienart tlienart mentioned this pull request Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants