Skip to content

Trying code blocks in markdown #1

Description

@tarzan

This is markdown underline: underlined

This is ==highlighted==

This is a "quote"

def method
  user.destroy
end
def method
  user.destroy
end
\int_{a}^{b} x^2 \,dx
# frozen_string_literal: true

module R::MarkdownHelper
  def self.markdown(text)
    return "" unless text

    options = {
      escape_html: true
    }

    renderer = R::Markdown::ChapterRenderer.new(options)
    markdown = Redcarpet::Markdown.new(renderer, default_markdown_extensions)

    markdown.render(text).html_safe
  end

  def self.excerpt(md, truncate_at = 400, truncate_options = {})
    truncate_options.reverse_update({ separator: /\s/ })

    markdown = Redcarpet::Markdown.new(R::Markdown::ExcerptRenderer, default_markdown_extensions)

    markdown.render(md).truncate(truncate_at, truncate_options)
  end

  def self.plain_markdown(text)
    return "" unless text

    extensions = {
      no_intra_emphasis: true,
      autolink: true,
      superscript: false,
      disable_indented_code_blocks: true,
      fenced_code_blocks: false,
      strikethrough: true,
      lax_spacing: true
    }

    options = { filter_html: true, no_images: true }

    renderer = Redcarpet::Render::HTML.new(options)
    markdown = Redcarpet::Markdown.new(renderer, extensions)
    markdown.render(text).html_safe
  end

  def self.plain_markdown_with_newlines(text)
    return "" unless text

    extensions = {
      no_intra_emphasis: true,
      autolink: true,
      superscript: false,
      disable_indented_code_blocks: true,
      fenced_code_blocks: false,
      strikethrough: true,
      lax_spacing: true
    }

    options = { filter_html: true, hard_wrap: true, no_images: true }

    renderer = Redcarpet::Render::HTML.new(options)
    markdown = Redcarpet::Markdown.new(renderer, extensions)
    markdown.render(text).html_safe
  end

  private

  def self.default_markdown_extensions
    {
      autolink: true,
      disable_indented_code_blocks: true,
      fenced_code_blocks: true,
      lax_spacing: true,
      no_intra_emphasis: true,
      strikethrough: true,
      superscript: true,
      tables: true,
      underline: true,
      highlight: true,
      quote: true
    }
  end
end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions