Sha256: 15fc4d09014baf3f596aefffa220541b0e6a9cf5fd6a5a1b4d6f59a591f6a1c5

Contents?: true

Size: 782 Bytes

Versions: 10

Compression:

Stored size: 782 Bytes

Contents

require "redcarpet"

module Lookbook
  class MarkdownRenderer < Service
    attr_reader :text, :opts

    def initialize(text, opts = {})
      @text = text
      @opts = opts.to_h
    end

    def call
      clean_text = ActionViewAnnotationsStripper.call(text)
      md = Redcarpet::Markdown.new(LookbookMarkdownRenderer, opts)
      md.render(clean_text).html_safe
    end

    class LookbookMarkdownRenderer < Redcarpet::Render::HTML
      def block_code(code, language = "ruby")
        line_numbers = language.to_s.end_with? "-numbered"
        ApplicationController.render(Lookbook::Code::Component.new(**{
          source: code,
          language: language.to_s.chomp("-numbered"),
          line_numbers: line_numbers
        }), layout: nil)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lookbook-1.4.5 lib/lookbook/services/markdown_renderer.rb
lookbook-1.4.4 lib/lookbook/services/markdown_renderer.rb
lookbook-1.4.3 lib/lookbook/services/markdown_renderer.rb
lookbook-1.4.2 lib/lookbook/services/markdown_renderer.rb
lookbook-1.4.1 lib/lookbook/services/markdown_renderer.rb
lookbook-1.4.0 lib/lookbook/services/markdown_renderer.rb
lookbook-1.3.4 lib/lookbook/services/markdown_renderer.rb
lookbook-1.3.3 lib/lookbook/services/markdown_renderer.rb
lookbook-1.3.1 lib/lookbook/services/markdown_renderer.rb
lookbook-1.3.0 lib/lookbook/services/markdown_renderer.rb