diff --git a/src/converter/html/functions.jl b/src/converter/html/functions.jl index 723142df7..c465e5e9e 100644 --- a/src/converter/html/functions.jl +++ b/src/converter/html/functions.jl @@ -170,84 +170,56 @@ function hfun_toc(params::Vector{String})::String inner = "" headers = filter(p -> min ≤ p.second[3] ≤ max, PAGE_HEADERS) isempty(headers) && return "" - levels = [h[3] for (rs,h) ∈ headers] - # find index of the top-most ancestor of each family - top_lvl_ancestor_idx = Vector{Int}(undef, length(levels)) - for (i, lvl) in enumerate(levels) - if i == 1 - top_lvl_ancestor_idx[i] = 1 + levels = [h[3] for (rs,h) ∈ headers] + baselvl = minimum(h[3] for h in values(headers)) + curlvl = baselvl + + new_levels = [1] # first level should be left-most aligned (i.e., start at 1) + curlvl = 1 + for i in 2:length(levels) + if levels[i-1] < levels[i] + curlvl += 1 else - a = i - for ai in reverse(1:i-1) - if lvl > levels[ai] - a = ai - break - end - end - top_lvl_ancestor_idx[i] = a + curlvl = levels[i] == baselvl ? 1 : 2 end + push!(new_levels, curlvl) end - - # get all the levels of the members in a family - families = map(top_lvl_idx->levels[findall(top_lvl_ancestor_idx .== top_lvl_idx)], top_lvl_ancestor_idx) - - # indicate the smallest level (that which will be the most left aligned) - top_most_lvl = minimum(levels[top_lvl_ancestor_idx]) - - # get offset of level relative to the left-most level (i.e., the min.) - ancestor_offset_relative_to_min = map(member->top_most_lvl - member[1], families) - - # indicators if any members of the family should be nested with empty bullets - make_empty_nest = map(fam_diff->any(fam_diff .< 0), diff.(families)) - - baselvl = minimum(h[3] for h in values(headers)) - 1 - curlvl = first(families)[1] - 1 + open_lists = 0 curskip = 0 for (li, (rs, h)) ∈ enumerate(headers) lvl = h[3] - sep = abs(lvl - curlvl) - skipped_lvl = sep ∉ [0, 1] - make_empty = make_empty_nest[li] - skip = skipped_lvl ? sep - 1 : 0 - if skip != 0 - curskip = skip - end - if lvl ≤ curlvl + skip = new_levels[li] + if skip ≤ curskip # Close previous list item inner *= "" # Close additional sublists for each level eliminated - close_length = make_empty ? curlvl - lvl : curlvl - lvl - skip - for i = fill(nothing, close_length) + for i = fill(nothing, curskip - skip) inner *= "" + open_lists -= 1 end # Reopen for this list item inner *= "
done.
""") end + +@testset "TOC-3" begin + fs() + s = raw""" + @def fd_rpath = "pages/ff/aa.md" + @def mintoclevel = 1 + @def maxtoclevel = 7 + \toc + # H1 + ### H3 + #### H4 + ## H2 + ## H2 + ##### H5 + # H1 + ### H3 + ## H2 + ## H2 + #### H4 + # H1 + #### H4 + #### H4 + ###### H6 + #### H4 + #### H4 + """ |> seval + @test isapproxstr(s, raw""" +