lib/govuk_markdown/renderer.rb in govuk_markdown-1.0.0 vs lib/govuk_markdown/renderer.rb in govuk_markdown-2.0.0b1

- old
+ new

@@ -1,7 +1,12 @@ module GovukMarkdown class Renderer < ::Redcarpet::Render::HTML + def initialize(govuk_options, options = {}) + @headings_start_with = govuk_options[:headings_start_with] + super options + end + def table(header, body) <<~HTML <table class='govuk-table'> <thead class='govuk-table__head'> #{header} @@ -28,20 +33,21 @@ </td> HTML end def header(text, header_level) - heading_size = case header_level - when 1 then "xl" - when 2 then "l" - when 3 then "m" - else "s" end + valid_header_sizes = %w[xl l m s].freeze - id_attribute = @options[:with_toc_data] ? " id=\"#{text.parameterize}\"" : "" + start_size = valid_header_sizes.include?(@headings_start_with) ? @headings_start_with : "xl" + start_size_index = valid_header_sizes.find_index(start_size) + + header_size = valid_header_sizes[start_size_index + header_level - 1] || "s" + + id_attribute = @options[:with_toc_data] ? " id=\"#{text.parameterize}\"" : "" <<~HTML - <h#{header_level}#{id_attribute} class="govuk-heading-#{heading_size}">#{text}</h#{header_level}> + <h#{header_level}#{id_attribute} class="govuk-heading-#{header_size}">#{text}</h#{header_level}> HTML end def paragraph(text) <<~HTML @@ -70,8 +76,16 @@ def hrule <<~HTML <hr class="govuk-section-break govuk-section-break--xl govuk-section-break--visible"> HTML + end + + def preprocess(document) + Preprocessor + .new(document) + .inject_inset_text + .inject_details + .output end end end