markdown-rails 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b123f6a0e803b1c36b5c6aac4080054d45a1171b70cab5449e7df9d579f288c
4
- data.tar.gz: b01e3956dd4b35a4d84043401daaf6c3be3a95ccda735226c6b5880d0e85ed08
3
+ metadata.gz: e552ca56be38b384f153880f11a5edb336f61b12e1e85448278b7e2f81723767
4
+ data.tar.gz: 9bbd303fb44492ffd820e0894144b51e387db88ed35197c3c4c76902a352118c
5
5
  SHA512:
6
- metadata.gz: bdd18e0d15ac48fb3c20a787aeac027b999d9b83b79e7dde76ea82b87dd0fc2d1de11ac6e150528b48ddab8f9c5026d2f843052acf1ec5be1bad8bf76391dc27
7
- data.tar.gz: 0eee23b19609af6466ccc7fcd58bbacb9c4aeac5cf4d24b31b4682cc2bfee6ca3e5c433054140319b4c7457886e97b25498b634078ae1aa99b2bafc6a630e74e
6
+ metadata.gz: 78566da895833c13a2e604ad9462d98be9e9c710a8cef12435be8e765204da9a5404a93a51824155b804de743646d54193fb9fd1af208ef986a3c9121dc9f5aa
7
+ data.tar.gz: 017a386e646f20a08364d75827b956d8a12b933b360ff86e4105180e92ee5cb4ca58ed05a14ae6db5b8569568ce2dad300558ffab866aa81df986d154b0a9091
data/README.md CHANGED
@@ -68,12 +68,10 @@ end
68
68
 
69
69
  Only enable Erb in Markdown if you trust the source of the file. Do not enable it for markdown provided by users or they will be able to execute arbitrary Ruby code.
70
70
 
71
- To enable Erb, you can tell Rails to render all view files ending with `.markerb` using the `MarkdownRails::Handler::Erb` handler.
72
-
73
71
  ```ruby
74
72
  # ./config/initializers/markdown.rb
75
- MarkdownRails.handle :markerb, with: MarkdownRails::Handler::Erb do
76
- ApplicationMarkdown.new
73
+ MarkdownRails.handle :markerb do
74
+ ErbMarkdown.new
77
75
  end
78
76
  ```
79
77
 
@@ -88,11 +86,13 @@ MarkdownRails.handle :md do
88
86
  ApplicationMarkdown.new
89
87
  end
90
88
 
91
- MarkdownRails.handle :markerb, with: MarkdownRails::Handler::Erb do
92
- ApplicationMarkdown.new
89
+ MarkdownRails.handle :markerb do
90
+ ErbMarkdown.new
93
91
  end
94
92
  ```
95
93
 
94
+ The same technique can be used for preprocessing Markdown with other handlers, like liquid.
95
+
96
96
  ## Customizing renderer
97
97
 
98
98
  You might want to customize your Markdown handler to do things like syntax code highlighting, etc.
@@ -6,7 +6,7 @@ class ApplicationMarkdown < MarkdownRails::Renderer::Rails
6
6
  # and feel smarter. Read the docs at https://github.com/vmg/redcarpet#also-now-our-pants-are-much-smarter
7
7
  include Redcarpet::Render::SmartyPants
8
8
 
9
- # Uncomment and run `bundle add rouge` for syntax highlighting
9
+ # Run `bundle add rouge` and uncomment the include below for syntax highlighting
10
10
  # include MarkdownRails::Helper::Rouge
11
11
 
12
12
  # If you need access to ActionController::Base.helpers, you can delegate by uncommenting
@@ -28,16 +28,6 @@ class ApplicationMarkdown < MarkdownRails::Renderer::Rails
28
28
  [:fenced_code_blocks]
29
29
  end
30
30
 
31
- # These methods are called as the Markdown document is parsed. Block-level calls are
32
- # documented at https://github.com/vmg/redcarpet#block-level-calls and span level calls
33
- # are documented at https://github.com/vmg/redcarpet#block-level-calls so feel free to
34
- # add more as you see fit.
35
- def block_code(code, language)
36
- content_tag :pre, class: language do
37
- code # You could implement syntax highlighting here with Rouge.
38
- end
39
- end
40
-
41
31
  # Example of how you might override the images to show embeds, like a YouTube video.
42
32
  def image(link, title, alt)
43
33
  url = URI(link)
@@ -0,0 +1,11 @@
1
+ # DANGER! This parses Erb, which means arbitrary Ruby can be run. Make sure
2
+ # you trust the source of your markdown and that its not user input.
3
+
4
+ class ErbMarkdown < ApplicationMarkdown
5
+ # Enables Erb to render for the entire doc before the markdown is rendered.
6
+ # This works great, except when you have an `erb` code fence.
7
+ def preprocess(html)
8
+ # Read more about this render call at https://guides.rubyonrails.org/layouts_and_rendering.html
9
+ render inline: html, handler: :erb
10
+ end
11
+ end
@@ -8,6 +8,6 @@ end
8
8
  # Don't use Erb for untrusted markdown content created by users; otherwise they
9
9
  # can execute arbitrary code on your server. This should only be used for input you
10
10
  # trust, like content files from your code repo.
11
- MarkdownRails.handle :markerb, with: MarkdownRails::Handler::Erb do
12
- ApplicationMarkdown.new
11
+ MarkdownRails.handle :markerb do
12
+ ErbMarkdown.new
13
13
  end
@@ -4,7 +4,7 @@ module MarkdownRails
4
4
  # config/initializer files are processed. This makes the `:md`
5
5
  # extension work if the user forgot to install the initializers.
6
6
  config.before_initialize do
7
- MarkdownRails::Handler::Markdown.register_default_handler
7
+ MarkdownRails::Handler.register_default
8
8
  end
9
9
  end
10
10
  end
@@ -0,0 +1,43 @@
1
+ module MarkdownRails
2
+ # We cannot use MarkdownRails because it conflicts with RDiscount's Markdown class
3
+ class Handler
4
+ DEFAULT_EXTENSION = :md
5
+
6
+ def initialize(&block)
7
+ @markdown = block
8
+ end
9
+
10
+ def call(template, source = template.source)
11
+ renderer.render(source).inspect + '.html_safe'
12
+ end
13
+
14
+ def self.handle(*extensions, &block)
15
+ Array(extensions).each do |extension|
16
+ handler = new &block
17
+ ActionView::Template.register_template_handler extension, handler
18
+ end
19
+ end
20
+
21
+ # Registers a default `.md` handler for Rails templates. This might be
22
+ # replaced by a handler in the `config/initializers/markdown.rb` file.
23
+ def self.register_default
24
+ handle(DEFAULT_EXTENSION) { MarkdownRails::Renderer::Rails.new }
25
+ end
26
+
27
+ private
28
+
29
+ def markdown
30
+ @cache = nil unless cache_enabled?
31
+ @cache ||= @markdown.call
32
+ end
33
+
34
+ def renderer
35
+ @renderer = nil unless cache_enabled?
36
+ @renderer ||= markdown.renderer
37
+ end
38
+
39
+ def cache_enabled?
40
+ ::Rails.configuration.cache_classes
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module MarkdownRails
2
- VERSION = "2.0.1"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -2,14 +2,11 @@ require "markdown-rails/version"
2
2
  require "markdown-rails/engine"
3
3
 
4
4
  module MarkdownRails
5
- def self.handle(*extensions, with: Handler::Markdown, &block)
6
- with.handle *extensions, &block
5
+ def self.handle(*extensions, &block)
6
+ Handler.handle(*extensions, &block)
7
7
  end
8
8
 
9
- module Handler
10
- autoload :Markdown, "markdown-rails/handler/markdown"
11
- autoload :Erb, "markdown-rails/handler/erb"
12
- end
9
+ autoload :Handler, "markdown-rails/handler"
13
10
 
14
11
  module Renderer
15
12
  autoload :Base, "markdown-rails/renderer/base"
metadata CHANGED
@@ -1,17 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-12 00:00:00.000000000 Z
11
+ date: 2023-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionview
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 6.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 6.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
15
43
  requirement: !ruby/object:Gem::Requirement
16
44
  requirements:
17
45
  - - ">="
@@ -53,11 +81,11 @@ files:
53
81
  - lib/generators/markdown_rails/install/USAGE
54
82
  - lib/generators/markdown_rails/install/install_generator.rb
55
83
  - lib/generators/markdown_rails/install/templates/app/markdown/application_markdown.rb
84
+ - lib/generators/markdown_rails/install/templates/app/markdown/erb_markdown.rb
56
85
  - lib/generators/markdown_rails/install/templates/config/initializers/markdown.rb
57
86
  - lib/markdown-rails.rb
58
87
  - lib/markdown-rails/engine.rb
59
- - lib/markdown-rails/handler/erb.rb
60
- - lib/markdown-rails/handler/markdown.rb
88
+ - lib/markdown-rails/handler.rb
61
89
  - lib/markdown-rails/helper/rouge.rb
62
90
  - lib/markdown-rails/railtie.rb
63
91
  - lib/markdown-rails/renderer/base.rb
@@ -1,22 +0,0 @@
1
- module MarkdownRails
2
- module Handler
3
- class Erb < Markdown
4
- def call(template, source = template.source)
5
- compiled_source = compile_erb template, source
6
- # TODO: This won't properly handle initializer blocks. Somehow
7
- # I need to pass a reference to the block that's passed in.
8
- "#{markdown.class.name}.new.renderer.render(begin;#{compiled_source};end).html_safe"
9
- end
10
-
11
- private
12
-
13
- def compile_erb(template, source)
14
- erb.call(template, source)
15
- end
16
-
17
- def erb
18
- ActionView::Template.registered_template_handler(:erb)
19
- end
20
- end
21
- end
22
- end
@@ -1,45 +0,0 @@
1
- module MarkdownRails
2
- module Handler
3
- # We cannot use MarkdownRails because it conflicts with RDiscount's Markdown class
4
- class Markdown
5
- DEFAULT_EXTENSION = :md
6
-
7
- def initialize(&block)
8
- @markdown = block
9
- end
10
-
11
- def call(template, source = template.source)
12
- renderer.render(source).inspect + '.html_safe'
13
- end
14
-
15
- def self.handle(*extensions, &block)
16
- Array(extensions).each do |extension|
17
- handler = new &block
18
- ActionView::Template.register_template_handler extension, handler
19
- end
20
- end
21
-
22
- # Registers a default `.md` handler for Rails templates. This might be
23
- # replaced by a handler in the `config/initializers/markdown.rb` file.
24
- def self.register_default_handler
25
- handle(DEFAULT_EXTENSION) { MarkdownRails::Renderer::Rails.new }
26
- end
27
-
28
- private
29
-
30
- def markdown
31
- @cache = nil unless cache_enabled?
32
- @cache ||= @markdown.call
33
- end
34
-
35
- def renderer
36
- @renderer = nil unless cache_enabled?
37
- @renderer ||= markdown.renderer
38
- end
39
-
40
- def cache_enabled?
41
- ::Rails.configuration.cache_classes
42
- end
43
- end
44
- end
45
- end