mark-don 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +88 -0
- data/lib/mark/don.rb +1 -0
- data/lib/mark_don/controller_helpers.rb +35 -0
- data/lib/mark_don/converter.rb +16 -0
- data/lib/mark_don/railtie.rb +40 -0
- data/lib/mark_don/request_handler.rb +20 -0
- data/lib/mark_don/version.rb +3 -0
- data/lib/mark_don.rb +3 -0
- data/mark-don.gemspec +25 -0
- metadata +122 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ec20e8729b15fd7da900cef5cf936a05d0ef8a9723c3c0eebf9da1e1d5871e83
|
|
4
|
+
data.tar.gz: 7df81eba5231a490d4c6ebf6997a37742f815020d66b8192ff3070023a8eda3c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1790cf9de91540a29f4406472daf3f79d3611e96d9cf5f400a1d3d56e9facb1073f2b017226f545bad4b0fc8a3e134a989dce05f5a4dea073d87801f9e261858
|
|
7
|
+
data.tar.gz: 6a4b37c896b08a148e2a1316ed80dca1f350941813fddcd08e13192645bee3925e9626bca6ec84b7e4dd22169eb6484f2818bd2567330a03b7b90a3a75a32e16
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Paul Lahana
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# mark-don
|
|
2
|
+
|
|
3
|
+
Serve any Rails HTML view as Markdown — no templates to write.
|
|
4
|
+
|
|
5
|
+
When a client requests `text/markdown` (via `Accept` header or `.md` extension), mark-don intercepts the normal HTML render, converts the output on the fly, and returns it with `Content-Type: text/markdown`. Your existing `.html.erb` views are reused as-is.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add to your Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'mark-don'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
No initializer needed. The gem hooks into Rails automatically via a Railtie.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### Per-action
|
|
20
|
+
|
|
21
|
+
Add `format.markdown` to any `respond_to` block:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
class RoomsController < ApplicationController
|
|
25
|
+
def show
|
|
26
|
+
@room = Room.find(params[:id])
|
|
27
|
+
|
|
28
|
+
respond_to do |format|
|
|
29
|
+
format.html
|
|
30
|
+
format.markdown
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
A request with `Accept: text/markdown` or the `.md` extension triggers the markdown response:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
GET /rooms/1.md
|
|
40
|
+
GET /rooms/1 # with Accept: text/markdown
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
> **Note:** the `.md` extension only works if your routes allow format suffixes. Rails disables them by default in newer apps. Either add `format: true` to the route, or use the `Accept` header instead.
|
|
44
|
+
|
|
45
|
+
### Controller macro
|
|
46
|
+
|
|
47
|
+
To enable markdown for multiple actions without repeating `format.markdown` everywhere:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
class RoomsController < ApplicationController
|
|
51
|
+
markdown_render # all actions
|
|
52
|
+
markdown_render only: :show
|
|
53
|
+
markdown_render except: :index
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Output
|
|
58
|
+
|
|
59
|
+
The gem converts the `<body>` content of the rendered HTML to GitHub Flavored Markdown using [reverse_markdown](https://github.com/xijo/reverse_markdown) under the hood. Only the body is converted — the layout's `<head>` and surrounding HTML are discarded. `<script>`, `<style>`, `<meta>`, and `<link>` tags are stripped. Inline styles and unknown elements are dropped; their text content is preserved.
|
|
60
|
+
|
|
61
|
+
**Input:**
|
|
62
|
+
```html
|
|
63
|
+
<h1>My Room</h1>
|
|
64
|
+
<p>This is a <strong>great</strong> room with a <a href="/view">nice view</a>.</p>
|
|
65
|
+
<ul>
|
|
66
|
+
<li>WiFi included</li>
|
|
67
|
+
<li>Breakfast at 8am</li>
|
|
68
|
+
</ul>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Output:**
|
|
72
|
+
```markdown
|
|
73
|
+
# My Room
|
|
74
|
+
|
|
75
|
+
This is a **great** room with a [nice view](/view).
|
|
76
|
+
|
|
77
|
+
- WiFi included
|
|
78
|
+
- Breakfast at 8am
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Requirements
|
|
82
|
+
|
|
83
|
+
- Ruby >= 3.0
|
|
84
|
+
- Rails >= 6.1
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT
|
data/lib/mark/don.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'mark_don'
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module MarkDon
|
|
2
|
+
module ControllerHelpers
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
extend ClassMethods
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
module ClassMethods
|
|
10
|
+
# Declares that this controller supports markdown responses.
|
|
11
|
+
# With no arguments, this is a semantic label only — the global around_action
|
|
12
|
+
# already handles all markdown requests.
|
|
13
|
+
# Pass `only:` or `except:` to restrict markdown conversion to specific actions.
|
|
14
|
+
def markdown_render(only: nil, except: nil)
|
|
15
|
+
return unless only || except
|
|
16
|
+
|
|
17
|
+
skip_around_action :mark_don_handle_markdown_request
|
|
18
|
+
around_action(only: only, except: except) do |controller, action|
|
|
19
|
+
if MarkDon::MARKDOWN_FORMAT_SYMBOLS.include?(controller.request.format.symbol)
|
|
20
|
+
controller.request.format = :html
|
|
21
|
+
action.call
|
|
22
|
+
if controller.performed? && controller.response.successful?
|
|
23
|
+
html = Array(controller.response_body).join
|
|
24
|
+
markdown = MarkDon::Converter.convert(html)
|
|
25
|
+
controller.response_body = markdown
|
|
26
|
+
controller.response.content_type = Mime[:markdown].to_s
|
|
27
|
+
end
|
|
28
|
+
else
|
|
29
|
+
action.call
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'reverse_markdown'
|
|
2
|
+
require 'nokogiri'
|
|
3
|
+
|
|
4
|
+
module MarkDon
|
|
5
|
+
class Converter
|
|
6
|
+
STRIP_TAGS = %w[script style meta link].freeze
|
|
7
|
+
|
|
8
|
+
def self.convert(html)
|
|
9
|
+
doc = Nokogiri::HTML(html)
|
|
10
|
+
STRIP_TAGS.each { |tag| doc.css(tag).remove }
|
|
11
|
+
|
|
12
|
+
body = doc.at('body') || doc
|
|
13
|
+
ReverseMarkdown.convert(body.inner_html, unknown_tags: :bypass, github_flavored: true).strip
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require 'rails'
|
|
2
|
+
require 'mark_don/request_handler'
|
|
3
|
+
require 'mark_don/controller_helpers'
|
|
4
|
+
|
|
5
|
+
module MarkDon
|
|
6
|
+
class Railtie < Rails::Railtie
|
|
7
|
+
# Rails 8.1+ ships "text/markdown" as :md already.
|
|
8
|
+
# We skip re-registration if it's present, and explicitly define format.markdown
|
|
9
|
+
# on the Collector so that respond_to blocks can use it without hitting
|
|
10
|
+
# method_missing (which recurses infinitely when the MIME symbol is :md but
|
|
11
|
+
# the called method is :markdown).
|
|
12
|
+
initializer 'mark_don.mime_type' do
|
|
13
|
+
Mime::Type.register 'text/markdown', :markdown unless Mime[:md] || Mime[:markdown]
|
|
14
|
+
|
|
15
|
+
unless ActionController::MimeResponds::Collector.method_defined?(:markdown)
|
|
16
|
+
ActionController::MimeResponds::Collector.class_eval do
|
|
17
|
+
def markdown(...)
|
|
18
|
+
custom(Mime[:md] || Mime[:markdown], ...)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Intercepts any markdown request before respond_to runs. Switches the format to
|
|
25
|
+
# :html so the action renders its HTML view normally, then converts the response.
|
|
26
|
+
# Checks both :md (Rails 8.1+ symbol) and :markdown (custom registration).
|
|
27
|
+
initializer 'mark_don.around_action' do
|
|
28
|
+
ActiveSupport.on_load(:action_controller) do
|
|
29
|
+
include MarkDon::RequestHandler
|
|
30
|
+
around_action :mark_don_handle_markdown_request
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
initializer 'mark_don.controller_helpers' do
|
|
35
|
+
ActiveSupport.on_load(:action_controller) do
|
|
36
|
+
include MarkDon::ControllerHelpers
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module MarkDon
|
|
2
|
+
MARKDOWN_FORMAT_SYMBOLS = %i[markdown md].freeze
|
|
3
|
+
|
|
4
|
+
module RequestHandler
|
|
5
|
+
def mark_don_handle_markdown_request(&action)
|
|
6
|
+
if MarkDon::MARKDOWN_FORMAT_SYMBOLS.include?(request.format.symbol)
|
|
7
|
+
request.format = :html
|
|
8
|
+
action.call
|
|
9
|
+
if performed? && response.successful?
|
|
10
|
+
html = Array(response_body).join
|
|
11
|
+
markdown = MarkDon::Converter.convert(html)
|
|
12
|
+
self.response_body = markdown
|
|
13
|
+
response.content_type = 'text/markdown'
|
|
14
|
+
end
|
|
15
|
+
else
|
|
16
|
+
action.call
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
data/lib/mark_don.rb
ADDED
data/mark-don.gemspec
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require_relative 'lib/mark_don/version'
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = 'mark-don'
|
|
5
|
+
spec.version = MarkDon::VERSION
|
|
6
|
+
spec.authors = ['Paul Lahana']
|
|
7
|
+
spec.email = ['paul.lahana@gmail.com']
|
|
8
|
+
|
|
9
|
+
spec.summary = 'Render Rails HTML views as Markdown'
|
|
10
|
+
spec.description = 'A Rails gem that converts HTML views to Markdown on-the-fly via format.markdown in respond_to blocks.'
|
|
11
|
+
spec.homepage = 'https://github.com/paultursuru/mark-don'
|
|
12
|
+
spec.license = 'MIT'
|
|
13
|
+
|
|
14
|
+
spec.required_ruby_version = '>= 3.0'
|
|
15
|
+
|
|
16
|
+
spec.files = Dir['lib/**/*', '*.gemspec', 'Gemfile', 'README.md', 'LICENSE']
|
|
17
|
+
spec.require_paths = ['lib']
|
|
18
|
+
|
|
19
|
+
spec.add_dependency 'rails', '>= 6.1'
|
|
20
|
+
spec.add_dependency 'reverse_markdown', '~> 2.1'
|
|
21
|
+
spec.add_dependency 'nokogiri', '>= 1.13'
|
|
22
|
+
|
|
23
|
+
spec.add_development_dependency 'rspec-rails', '~> 6.0'
|
|
24
|
+
spec.add_development_dependency 'capybara', '~> 3.0'
|
|
25
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mark-don
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Paul Lahana
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rails
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '6.1'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '6.1'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: reverse_markdown
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '2.1'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '2.1'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: nokogiri
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '1.13'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '1.13'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: rspec-rails
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '6.0'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '6.0'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: capybara
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - "~>"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '3.0'
|
|
75
|
+
type: :development
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - "~>"
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '3.0'
|
|
82
|
+
description: A Rails gem that converts HTML views to Markdown on-the-fly via format.markdown
|
|
83
|
+
in respond_to blocks.
|
|
84
|
+
email:
|
|
85
|
+
- paul.lahana@gmail.com
|
|
86
|
+
executables: []
|
|
87
|
+
extensions: []
|
|
88
|
+
extra_rdoc_files: []
|
|
89
|
+
files:
|
|
90
|
+
- Gemfile
|
|
91
|
+
- LICENSE
|
|
92
|
+
- README.md
|
|
93
|
+
- lib/mark/don.rb
|
|
94
|
+
- lib/mark_don.rb
|
|
95
|
+
- lib/mark_don/controller_helpers.rb
|
|
96
|
+
- lib/mark_don/converter.rb
|
|
97
|
+
- lib/mark_don/railtie.rb
|
|
98
|
+
- lib/mark_don/request_handler.rb
|
|
99
|
+
- lib/mark_don/version.rb
|
|
100
|
+
- mark-don.gemspec
|
|
101
|
+
homepage: https://github.com/paultursuru/mark-don
|
|
102
|
+
licenses:
|
|
103
|
+
- MIT
|
|
104
|
+
metadata: {}
|
|
105
|
+
rdoc_options: []
|
|
106
|
+
require_paths:
|
|
107
|
+
- lib
|
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
|
+
requirements:
|
|
110
|
+
- - ">="
|
|
111
|
+
- !ruby/object:Gem::Version
|
|
112
|
+
version: '3.0'
|
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - ">="
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
requirements: []
|
|
119
|
+
rubygems_version: 3.7.2
|
|
120
|
+
specification_version: 4
|
|
121
|
+
summary: Render Rails HTML views as Markdown
|
|
122
|
+
test_files: []
|