Sha256: 2d83cef56c8cf5d7bee5c257ff2495bbef36c4959d53896eeaf785842b9b40d1

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

require "redcarpet"

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

    def initialize(text, opts = {})
      @text = text
      @opts = Lookbook.config.markdown_options.merge(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

      def postprocess(full_document)
        full_document&.gsub!("<p><lookbook", "<lookbook")
        full_document&.gsub!("</lookbook-embed></p>", "</lookbook-embed>")
        full_document
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lookbook-2.0.0.beta.2 lib/lookbook/services/markdown_renderer.rb
lookbook-2.0.0.beta.1 lib/lookbook/services/markdown_renderer.rb
lookbook-2.0.0.beta.0 lib/lookbook/services/markdown_renderer.rb