Sha256: 9df12cb83f86fbb5949f3930cb9b774bf86a4aec909354ade1755e0f29c58465

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 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)
          puts "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)
        after_h1_strategy = @options[:after_h1_strategy]
        h1_callback = @options[:h1_callback]
        src_title = el.options[:raw_text]
        after_h1_html = after_h1_strategy.call(binding)

        level = output_header_level(el.options[:level])
        if level == 1
          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

2 entries across 2 versions & 1 rubygems

Version Path
bhook-0.1.1 lib/bhook/converter/html.rb
bhook-0.1.0 lib/bhook/converter/html.rb