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 *= "
  • " - elseif lvl > curlvl + else # Open additional sublists for each level added - if make_empty - # hide marker for the empty nested sublists - for i = fill(nothing, skip) - inner *= "
    1. " - end - end - for i = fill(nothing, lvl - curlvl - skip) + for i = fill(nothing, skip - curskip) inner *= "
      1. " + open_lists += 1 end end inner *= html_ahref_key(rs, h[1]) - curlvl = lvl + ancestor_offset_relative_to_min[li] + curlvl = lvl + curskip = skip # At this point, number of sublists (
        1. ) open equals curlvl end # Close remaining lists, as if going down to the base level - for i = fill(nothing, curlvl - baselvl + curskip + sum(ancestor_offset_relative_to_min)) + for i = fill(nothing, open_lists) inner *= "
        " end toc = "
        " * inner * "
        " end - """ $(SIGNATURES) diff --git a/test/converter/md/markdown2.jl b/test/converter/md/markdown2.jl index 06e1c4378..7492bc88c 100644 --- a/test/converter/md/markdown2.jl +++ b/test/converter/md/markdown2.jl @@ -55,12 +55,8 @@ end
      2. Top-most ancestor of family 2
          -
        1. -
            -
          1. - should be empty nested -
          2. -
          +
        2. + should be empty nested
        3. part of family 2 @@ -142,3 +138,98 @@ end

          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""" +
          +
            +
          1. + H1 +
              +
            1. + H3 +
                +
              1. H4
              2. +
              +
            2. +
            3. H2
            4. +
            5. + H2 +
                +
              1. H5
              2. +
              +
            6. +
            +
          2. +
          3. + H1 +
              +
            1. H3
            2. +
            3. H2
            4. +
            5. + H2 +
                +
              1. H4
              2. +
              +
            6. +
            +
          4. +
          5. + H1 +
              +
            1. H4
            2. +
            3. + H4 +
                +
              1. H6
              2. +
              +
            4. +
            5. H4
            6. +
            7. H4
            8. +
            +
          6. +
          +
          +

          H1

          +

          H3

          +

          H4

          +

          H2

          +

          H2

          +
          H5
          +

          H1

          +

          H3

          +

          H2

          +

          H2

          +

          H4

          +

          H1

          +

          H4

          +

          H4

          +
          H6
          +

          H4

          +

          H4

          + """) +end