markdown-rails 2.0.0.alpha10 → 2.0.1

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: 322bc825210d3a2bfc37e22665366325be25e77e438adb4b81fd3fb671151433
4
- data.tar.gz: bd95381cdc14695bd1dbde902ad85d3b1724c91c4f04b41932f45bfcba0ea227
3
+ metadata.gz: 9b123f6a0e803b1c36b5c6aac4080054d45a1171b70cab5449e7df9d579f288c
4
+ data.tar.gz: b01e3956dd4b35a4d84043401daaf6c3be3a95ccda735226c6b5880d0e85ed08
5
5
  SHA512:
6
- metadata.gz: 9c4c978ffdd52d865fc73c9636f51eef60ce84d902eaa9727100d52bfa4f831b67b842cbc05489e43c424dee36a6ff8efc8bdda6642fc72bf29313bbe1a54e67
7
- data.tar.gz: ed00ba127ec4e455c7c492be6ffe50d1a87bdcc3a60fba6378fd85e821bce9d6a9ab79733177ccfd06186050825fc491bdfee91b357d311672681571ab9e766e
6
+ metadata.gz: bdd18e0d15ac48fb3c20a787aeac027b999d9b83b79e7dde76ea82b87dd0fc2d1de11ac6e150528b48ddab8f9c5026d2f843052acf1ec5be1bad8bf76391dc27
7
+ data.tar.gz: 0eee23b19609af6466ccc7fcd58bbacb9c4aeac5cf4d24b31b4682cc2bfee6ca3e5c433054140319b4c7457886e97b25498b634078ae1aa99b2bafc6a630e74e
data/README.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # markdown-rails
2
2
 
3
- > **Note**
4
- > **Everything below is implemented in 2.0.0.alpha.** . You can start using this by adding to `gem "markdown-rails", version: "2.0.0.alpha"` to your Gemfile or by looking at it being used in the https://github.com/bradgessler/view-playground repo.
5
-
6
3
  This gem allows you to write static Rails views and partials using the [Markdown](http://daringfireball.net/projects/markdown/syntax) syntax. No more editing prose in HTML!
7
4
 
8
5
  It is a comprehensive Markdown library for Rails that addresses the following pain points:
@@ -58,29 +55,45 @@ Applications commonly need various markdown variants within one application. For
58
55
  # Restart the server to see changes made to this file.
59
56
 
60
57
  # Setup markdown stacks to work with different template handler in Rails.
61
- MarkdownRails.handle :md, :markdown, with: :ApplicationMarkdown
62
- MarkdownRails.handle :smd, with: :SafeMarkdown
58
+ MarkdownRails.handle :md do
59
+ ApplicationMarkdown.new
60
+ end
61
+
62
+ MarkdownRails.handle :crazymd do
63
+ MyCrazyMarkdown.new
64
+ end
63
65
  ```
64
66
 
65
67
  ### Enable Erb in Markdown
66
68
 
67
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.
68
70
 
69
- To enable Erb, you can tell Rails to render all view files ending with `.markerb` using the `ErbMarkdown` handler.
71
+ To enable Erb, you can tell Rails to render all view files ending with `.markerb` using the `MarkdownRails::Handler::Erb` handler.
72
+
73
+ ```ruby
74
+ # ./config/initializers/markdown.rb
75
+ MarkdownRails.handle :markerb, with: MarkdownRails::Handler::Erb do
76
+ ApplicationMarkdown.new
77
+ end
78
+ ```
79
+
80
+ 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:
70
81
 
71
82
  ```ruby
72
83
  # ./config/initializers/markdown.rb
73
84
  # Restart the server to see changes made to this file.
74
85
 
75
86
  # Setup markdown stacks to work with different template handler in Rails.
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
79
- ```
87
+ MarkdownRails.handle :md do
88
+ ApplicationMarkdown.new
89
+ end
80
90
 
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.
91
+ MarkdownRails.handle :markerb, with: MarkdownRails::Handler::Erb do
92
+ ApplicationMarkdown.new
93
+ end
94
+ ```
82
95
 
83
- ## Customizing Markdown
96
+ ## Customizing renderer
84
97
 
85
98
  You might want to customize your Markdown handler to do things like syntax code highlighting, etc.
86
99
 
@@ -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 methods from the view context, you can delegate by uncommenting
12
+ # If you need access to ActionController::Base.helpers, 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: :view
22
+ # to: :helpers
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
@@ -1,12 +1,13 @@
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,
5
- with: :ApplicationMarkdown
4
+ MarkdownRails.handle :md, :markdown do
5
+ ApplicationMarkdown.new
6
+ end
6
7
 
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
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
@@ -1,4 +1,10 @@
1
1
  module MarkdownRails
2
2
  class Engine < ::Rails::Engine
3
+ # Check if the `.md` extension has been registered after the
4
+ # config/initializer files are processed. This makes the `:md`
5
+ # extension work if the user forgot to install the initializers.
6
+ config.before_initialize do
7
+ MarkdownRails::Handler::Markdown.register_default_handler
8
+ end
3
9
  end
4
10
  end
@@ -0,0 +1,22 @@
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
@@ -0,0 +1,45 @@
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
@@ -15,8 +15,8 @@ module MarkdownRails
15
15
  ]
16
16
  end
17
17
 
18
- def markdown_renderer
19
- @markdown_renderer ||= ::Redcarpet::Markdown.new(self, **features)
18
+ def renderer
19
+ ::Redcarpet::Markdown.new(self.class, **features)
20
20
  end
21
21
 
22
22
  private
@@ -3,18 +3,14 @@ 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
6
  # Rendering from Markdown is actually outside of the view
16
7
  # context, so we need to delegate render to the ApplicationController
17
8
  # render method that can render outside of the view context.
9
+ delegate \
10
+ :helpers,
11
+ :render,
12
+ to: :base_controller
13
+
18
14
  delegate \
19
15
  :asset_digest_path,
20
16
  :asset_path,
@@ -36,13 +32,16 @@ module MarkdownRails
36
32
  :turbo_frame_tag,
37
33
  :controller,
38
34
  :raw,
39
- :request,
40
- :controller,
41
- to: :view
35
+ to: :helpers
42
36
 
43
37
  def image(link, title, alt)
44
38
  image_tag link, title: title, alt: alt
45
39
  end
40
+
41
+ protected
42
+ def base_controller
43
+ ::ApplicationController
44
+ end
46
45
  end
47
46
  end
48
47
  end
@@ -1,3 +1,3 @@
1
1
  module MarkdownRails
2
- VERSION = "2.0.0.alpha10"
2
+ VERSION = "2.0.1"
3
3
  end
@@ -2,22 +2,21 @@ require "markdown-rails/version"
2
2
  require "markdown-rails/engine"
3
3
 
4
4
  module MarkdownRails
5
- def self.handle(*extensions, with:)
6
- handler = TemplateHandler.new(with)
7
-
8
- extensions.each do |extension|
9
- ActionView::Template.register_template_handler extension, handler
10
- end
5
+ def self.handle(*extensions, with: Handler::Markdown, &block)
6
+ with.handle *extensions, &block
11
7
  end
12
8
 
13
- autoload :TemplateHandler, "markdown-rails/template_handler"
9
+ module Handler
10
+ autoload :Markdown, "markdown-rails/handler/markdown"
11
+ autoload :Erb, "markdown-rails/handler/erb"
12
+ end
14
13
 
15
14
  module Renderer
16
- autoload :Base, "markdown-rails/renderer/base"
17
- autoload :Rails, "markdown-rails/renderer/rails"
15
+ autoload :Base, "markdown-rails/renderer/base"
16
+ autoload :Rails, "markdown-rails/renderer/rails"
18
17
  end
19
18
 
20
19
  module Helper
21
- autoload :Rouge, "markdown-rails/helper/rouge"
20
+ autoload :Rouge, "markdown-rails/helper/rouge"
22
21
  end
23
22
  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.alpha10
4
+ version: 2.0.1
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-01-05 00:00:00.000000000 Z
11
+ date: 2023-10-12 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
57
56
  - lib/generators/markdown_rails/install/templates/config/initializers/markdown.rb
58
57
  - lib/markdown-rails.rb
59
58
  - lib/markdown-rails/engine.rb
59
+ - lib/markdown-rails/handler/erb.rb
60
+ - lib/markdown-rails/handler/markdown.rb
60
61
  - lib/markdown-rails/helper/rouge.rb
61
62
  - lib/markdown-rails/railtie.rb
62
63
  - lib/markdown-rails/renderer/base.rb
63
64
  - 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
@@ -83,11 +83,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
83
  version: '0'
84
84
  required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  requirements:
86
- - - ">"
86
+ - - ">="
87
87
  - !ruby/object:Gem::Version
88
- version: 1.3.1
88
+ version: '0'
89
89
  requirements: []
90
- rubygems_version: 3.4.1
90
+ rubygems_version: 3.4.6
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Markdown templates and partials in Rails.
@@ -1,10 +0,0 @@
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,15 +0,0 @@
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