Skip to content

Commit

Permalink
Stable sort
Browse files Browse the repository at this point in the history
try to avoid left menu jumping around
  • Loading branch information
heathd committed Oct 23, 2024
1 parent d888afa commit 8b7e5ba
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions lib/govuk_tech_docs/table_of_contents/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ def single_page_table_of_contents(html, url: "", max_level: nil)
output
end

# Items with a weight appear first, in weight order
# if items have equal weight, they stay in the order they appeared (stable sort)
# Items without a weight appear after, sorted stably in the order that they are
# present in the resource list
def sort_resources_stably(resources)
resources
.each.with_index
.sort_by { |r, index| [r.data.weight ? 0 : 1, r.data.weight || 0, index] }
.map(&:first)
.to_a
end

def select_top_level_html_files(resources)
resources
.select { |r| r.path.end_with?(".html") && (r.parent.nil? || r.parent.url == "/") }
end

def multi_page_table_of_contents(resources, current_page, config, current_page_html = nil)
# Only parse top level html files
# Sorted by weight frontmatter
resources = resources
.select { |r| r.path.end_with?(".html") && (r.parent.nil? || r.parent.url == "/") }
.sort_by { |r| [r.data.weight ? 0 : 1, r.data.weight || 0] }
resources = sort_resources_stably(
select_top_level_html_files(resources),
)

render_page_tree(resources, current_page, config, current_page_html)
end
Expand All @@ -40,9 +55,7 @@ def list_items_from_headings(html, url: "", max_level: nil)
end

def render_page_tree(resources, current_page, config, current_page_html)
# Sort by weight frontmatter
resources = resources
.sort_by { |r| [r.data.weight ? 0 : 1, r.data.weight || 0] }
resources = sort_resources_stably(resources)

output = "<ul>\n"
resources.each do |resource|
Expand Down

0 comments on commit 8b7e5ba

Please sign in to comment.