-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Introduce `MarkdownRenderer` class (inspired by https://railsinspire.com/samples/16/files/64) - Syntax highlight code blocks in markdown - Allow `UI::CodeBlock` to be rendered in any language
- Loading branch information
Showing
8 changed files
with
88 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
<pre class="rouge border border-gray-300 rounded overflow-scroll"> | ||
<%== formatter.format(lexer.lex(content)) %> | ||
</pre> | ||
<pre id="<%= id %>" class="rouge border border-gray-300 rounded overflow-scroll px-4 py-2"><%== formatter.format(lexer.lex(content)) %></pre> | ||
|
||
<style type="text/css"> | ||
<%= Rouge::Themes::Github.render(scope: 'pre.rouge') %> | ||
<%= Rouge::Themes::Github.render(scope: "pre##{id}.rouge") %> | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module MarkdownHelper | ||
def render_markdown(content) | ||
MarkdownRenderer.new(sanitize(content)).render | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
class MarkdownRenderer | ||
class DefaultRenderer < Redcarpet::Render::HTML | ||
include Redcarpet::Render::SmartyPants | ||
|
||
def block_code(code, language) | ||
ApplicationController.render(UI::CodeBlock.new(language:).with_content(code), layout: false) | ||
end | ||
end | ||
|
||
def initialize(content) | ||
@content = content | ||
end | ||
|
||
def render | ||
return "" if content.blank? | ||
|
||
renderer.render(content) | ||
end | ||
|
||
private | ||
|
||
attr_reader :content | ||
|
||
def renderer | ||
Redcarpet::Markdown.new( | ||
DefaultRenderer.new(**markdown_settings), | ||
renderer_settings, | ||
) | ||
end | ||
|
||
def markdown_settings | ||
{ | ||
hard_wrap: true, | ||
no_images: false, | ||
no_links: false, | ||
with_toc_data: false, | ||
} | ||
end | ||
|
||
def renderer_settings | ||
{ | ||
autolink: true, | ||
disable_indented_code_blocks: true, | ||
fenced_code_blocks: true, | ||
footnotes: true, | ||
gh_blockcode: true, | ||
highlight: true, | ||
lax_spacing: true, | ||
no_intra_emphasis: true, | ||
strikethrough: true, | ||
superscript: true, | ||
tables: true, | ||
underline: true, | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters