markdown-rails 2.0.0.alpha8 → 2.0.0.alpha10

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: 4cc5654ddf4c2621a0f1f25c9de3cc177b1ca02d6a47c3c23550b426eb5c1f0f
4
- data.tar.gz: 672879f00b880bdc35f9bb79fad4ac0fdacfcf85fea5abb96ded0e773a77c90f
3
+ metadata.gz: 322bc825210d3a2bfc37e22665366325be25e77e438adb4b81fd3fb671151433
4
+ data.tar.gz: bd95381cdc14695bd1dbde902ad85d3b1724c91c4f04b41932f45bfcba0ea227
5
5
  SHA512:
6
- metadata.gz: 2de8d3c2ed3febe65b4fe72ce05e52eb98005f32cf319e6bf8b33e68e5ad27725325c0cdb400d70ae69e28db0242b137a9f76d061fb519a0acd746b5dd6af737
7
- data.tar.gz: 7fbd9f25952aeba219bce5726c0e37de27bec2d77f3187363abdc06e840a57f237b5ba9f00b916f40530e3fafafd076642c7cb8e9630cf92382b95e5fe68bccb
6
+ metadata.gz: 9c4c978ffdd52d865fc73c9636f51eef60ce84d902eaa9727100d52bfa4f831b67b842cbc05489e43c424dee36a6ff8efc8bdda6642fc72bf29313bbe1a54e67
7
+ data.tar.gz: ed00ba127ec4e455c7c492be6ffe50d1a87bdcc3a60fba6378fd85e821bce9d6a9ab79733177ccfd06186050825fc491bdfee91b357d311672681571ab9e766e
data/README.md CHANGED
@@ -58,45 +58,29 @@ Applications commonly need various markdown variants within one application. For
58
58
  # Restart the server to see changes made to this file.
59
59
 
60
60
  # Setup markdown stacks to work with different template handler in Rails.
61
- MarkdownRails.handle :md do
62
- ApplicationMarkdown.new
63
- end
64
-
65
- MarkdownRails.handle :crazymd do
66
- MyCrazyMarkdown.new
67
- end
61
+ MarkdownRails.handle :md, :markdown, with: :ApplicationMarkdown
62
+ MarkdownRails.handle :smd, with: :SafeMarkdown
68
63
  ```
69
64
 
70
65
  ### Enable Erb in Markdown
71
66
 
72
67
  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.
73
68
 
74
- To enable Erb, you can tell Rails to render all view files ending with `.markerb` using the `MarkdownRails::Handler::Erb` handler.
75
-
76
- ```ruby
77
- # ./config/initializers/markdown.rb
78
- MarkdownRails.handle :markerb, with: MarkdownRails::Handler::Erb do
79
- ApplicationMarkdown.new
80
- end
81
- ```
82
-
83
- You *could* change `:markerb` to `:md`, but I don't recommend it for all Markdown files or you'll run into problems if you have content like `<%= Time.current %>` inside of an `erb` codefence. You're better off having two configurations: one that handles Erb and another that doesn't, like this:
69
+ To enable Erb, you can tell Rails to render all view files ending with `.markerb` using the `ErbMarkdown` handler.
84
70
 
85
71
  ```ruby
86
72
  # ./config/initializers/markdown.rb
87
73
  # Restart the server to see changes made to this file.
88
74
 
89
75
  # Setup markdown stacks to work with different template handler in Rails.
90
- MarkdownRails.handle :md do
91
- ApplicationMarkdown.new
92
- end
93
-
94
- MarkdownRails.handle :markerb, with: MarkdownRails::Handler::Erb do
95
- ApplicationMarkdown.new
96
- end
76
+ MarkdownRails.handle :md, :markdown, with: :ApplicationMarkdown
77
+ # This is a bad idea for a few reasons, but sometimes its needed
78
+ MarkdownRails.handle :markerb, with: :ErbMarkdown
97
79
  ```
98
80
 
99
- ## Customizing renderer
81
+ You *could* change `:markerb` to `:md`, but I don't recommend it for all Markdown files or you'll run into problems if you have content like `<%= Time.current %>` inside of an `erb` codefence. You're better off having two configurations: one that handles Erb and another that doesn't.
82
+
83
+ ## Customizing Markdown
100
84
 
101
85
  You might want to customize your Markdown handler to do things like syntax code highlighting, etc.
102
86
 
@@ -9,7 +9,7 @@ class ApplicationMarkdown < MarkdownRails::Renderer::Rails
9
9
  # Uncomment and run `bundle add rouge` for syntax highlighting
10
10
  # include MarkdownRails::Helper::Rouge
11
11
 
12
- # If you need access to ActionController::Base.helpers, you can delegate by uncommenting
12
+ # If you need access to methods from the view context, you can delegate by uncommenting
13
13
  # and adding to the list below. Several are already included for you in the `MarkdownRails::Renderer::Rails`,
14
14
  # but you can add more here.
15
15
  #
@@ -19,7 +19,7 @@ class ApplicationMarkdown < MarkdownRails::Renderer::Rails
19
19
  # :request,
20
20
  # :cache,
21
21
  # :turbo_frame_tag,
22
- # to: :helpers
22
+ # to: :view
23
23
 
24
24
  # These flags control features in the Redcarpet renderer, which you can read
25
25
  # about at https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
@@ -0,0 +1,10 @@
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
+ view.render inline: html, handler: :erb
9
+ end
10
+ end
@@ -1,13 +1,12 @@
1
1
  # Restart the server to see changes made to this file.
2
2
 
3
3
  # Setup markdown stacks to work with different template handler in Rails.
4
- MarkdownRails.handle :md, :markdown do
5
- ApplicationMarkdown.new
6
- end
4
+ MarkdownRails.handle :md, :markdown,
5
+ with: :ApplicationMarkdown
7
6
 
8
- # Don't use Erb for untrusted markdown content created by users; otherwise they
9
- # can execute arbitrary code on your server. This should only be used for input you
10
- # trust, like content files from your code repo.
11
- MarkdownRails.handle :markerb, with: MarkdownRails::Handler::Erb do
12
- ApplicationMarkdown.new
13
- end
7
+ # Ideally you can modify `ApplicationMarkdown` to suit your needs without
8
+ # needing ERB, but for some reason enabling ERB with markdown is insanely
9
+ # popular, so here it is if that's your jam.
10
+
11
+ # MarkdownRails.handle :markerb,
12
+ # with: :ErbMarkdown
@@ -15,8 +15,8 @@ module MarkdownRails
15
15
  ]
16
16
  end
17
17
 
18
- def renderer
19
- ::Redcarpet::Markdown.new(self.class, **features)
18
+ def markdown_renderer
19
+ @markdown_renderer ||= ::Redcarpet::Markdown.new(self, **features)
20
20
  end
21
21
 
22
22
  private
@@ -3,6 +3,18 @@ module MarkdownRails
3
3
  class Rails < Base
4
4
  include ::Rails.application.routes.url_helpers
5
5
 
6
+ def initialize(view:, **extensions)
7
+ super(extensions)
8
+ @view = view
9
+ end
10
+
11
+ # The Rails render sets the view, which we can use to delegate everything
12
+ # to from the renderer.
13
+ attr_reader :view
14
+
15
+ # Rendering from Markdown is actually outside of the view
16
+ # context, so we need to delegate render to the ApplicationController
17
+ # render method that can render outside of the view context.
6
18
  delegate \
7
19
  :asset_digest_path,
8
20
  :asset_path,
@@ -20,21 +32,17 @@ module MarkdownRails
20
32
  :video_url,
21
33
  :tag,
22
34
  :content_tag,
23
- :render,
24
35
  :request,
25
36
  :turbo_frame_tag,
26
37
  :controller,
27
38
  :raw,
28
- to: :helpers
39
+ :request,
40
+ :controller,
41
+ to: :view
29
42
 
30
43
  def image(link, title, alt)
31
44
  image_tag link, title: title, alt: alt
32
45
  end
33
-
34
- protected
35
- def helpers
36
- ::ActionController::Base.helpers
37
- end
38
46
  end
39
47
  end
40
48
  end
@@ -0,0 +1,15 @@
1
+ module MarkdownRails
2
+ # We cannot use MarkdownRails because it conflicts with RDiscount's Markdown class
3
+ class TemplateHandler
4
+ def initialize(klass)
5
+ @markdown = klass
6
+ end
7
+
8
+ def call(template, source = template.source)
9
+ %[
10
+ markdown = #{@markdown.name}.new(view: self);
11
+ markdown.markdown_renderer.render(#{source.inspect}).html_safe;
12
+ ]
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module MarkdownRails
2
- VERSION = "2.0.0.alpha8"
2
+ VERSION = "2.0.0.alpha10"
3
3
  end
@@ -2,21 +2,22 @@ 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
7
- end
5
+ def self.handle(*extensions, with:)
6
+ handler = TemplateHandler.new(with)
8
7
 
9
- module Handler
10
- autoload :Markdown, "markdown-rails/handler/markdown"
11
- autoload :Erb, "markdown-rails/handler/erb"
8
+ extensions.each do |extension|
9
+ ActionView::Template.register_template_handler extension, handler
10
+ end
12
11
  end
13
12
 
13
+ autoload :TemplateHandler, "markdown-rails/template_handler"
14
+
14
15
  module Renderer
15
- autoload :Base, "markdown-rails/renderer/base"
16
- autoload :Rails, "markdown-rails/renderer/rails"
16
+ autoload :Base, "markdown-rails/renderer/base"
17
+ autoload :Rails, "markdown-rails/renderer/rails"
17
18
  end
18
19
 
19
20
  module Helper
20
- autoload :Rouge, "markdown-rails/helper/rouge"
21
+ autoload :Rouge, "markdown-rails/helper/rouge"
21
22
  end
22
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.alpha8
4
+ version: 2.0.0.alpha10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-21 00:00:00.000000000 Z
11
+ date: 2023-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -53,15 +53,15 @@ files:
53
53
  - lib/generators/markdown_rails/install/USAGE
54
54
  - lib/generators/markdown_rails/install/install_generator.rb
55
55
  - lib/generators/markdown_rails/install/templates/app/markdown/application_markdown.rb
56
+ - lib/generators/markdown_rails/install/templates/app/markdown/erb_markdown.rb
56
57
  - lib/generators/markdown_rails/install/templates/config/initializers/markdown.rb
57
58
  - lib/markdown-rails.rb
58
59
  - lib/markdown-rails/engine.rb
59
- - lib/markdown-rails/handler/erb.rb
60
- - lib/markdown-rails/handler/markdown.rb
61
60
  - lib/markdown-rails/helper/rouge.rb
62
61
  - lib/markdown-rails/railtie.rb
63
62
  - lib/markdown-rails/renderer/base.rb
64
63
  - lib/markdown-rails/renderer/rails.rb
64
+ - lib/markdown-rails/template_handler.rb
65
65
  - lib/markdown-rails/version.rb
66
66
  - lib/tasks/markdown/rails_tasks.rake
67
67
  homepage: https://github.com/sitepress/markdown-rails
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  - !ruby/object:Gem::Version
88
88
  version: 1.3.1
89
89
  requirements: []
90
- rubygems_version: 3.3.20
90
+ rubygems_version: 3.4.1
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Markdown templates and partials in Rails.
@@ -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,37 +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
- def initialize(&block)
6
- @markdown = block
7
- end
8
-
9
- def call(template, source = template.source)
10
- renderer.render(source).inspect + '.html_safe'
11
- end
12
-
13
- def self.handle(*extensions, &block)
14
- Array(extensions).each do |extension|
15
- handler = new &block
16
- ActionView::Template.register_template_handler extension, handler
17
- end
18
- end
19
-
20
- private
21
-
22
- def markdown
23
- @cache = nil unless cache_enabled?
24
- @cache ||= @markdown.call
25
- end
26
-
27
- def renderer
28
- @renderer = nil unless cache_enabled?
29
- @renderer ||= markdown.renderer
30
- end
31
-
32
- def cache_enabled?
33
- ::Rails.configuration.cache_classes
34
- end
35
- end
36
- end
37
- end