Sha256: 3dd7e2175261dbbd8e9561278dfcfadfed3048ce08610e55c49c6d33d01ef1d0

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# typed: strict
# frozen_string_literal: true

module Bhook
  module Converter
    class Html < ::Kramdown::Converter::Html
      extend T::Sig

      sig { params(root: Kramdown::Element, options: T::Hash[Symbol, T.untyped]).void }
      def initialize(root, options)
        @options = T.let({}, T::Hash[Symbol, T.untyped])
        super(root, options)
      end

      sig { params(el: Kramdown::Element, indent: Integer).returns(String) }
      def convert_a(el, indent)
        href_path = el.attr['href']
        if valid_relative_md_link?(href_path)
          L.debug "Found link: #{href_path}"
          el.attr['href'].gsub!(/\.md/, '.html')
        end
        super(el, indent)
      end

      sig { params(el: Kramdown::Element, indent: Integer).returns(String) }
      def convert_header(el, indent)
        header = super(el, indent)
        src_title = el.options[:raw_text]
        after_h1_html = @options[:after_h1_strategy].call(binding)

        level = output_header_level(el.options[:level])
        if level == 1
          @options[:h1_callback].call(src_title)
          "#{header}#{' ' * indent}#{after_h1_html}"
        else
          header
        end
      end

      private

      sig { params(href_path: String).returns(T::Boolean) }
      def valid_relative_md_link?(href_path)
        (href_path.end_with?('.md') || href_path.include?('.md#')) &&
          Pathname.new(href_path).relative?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bhook-0.2.0 lib/bhook/converter/html.rb