Skip to content

Commit 6992c91

Browse files
authored
Merge pull request #409 from platanus/feat/add-markdown-input
Feat/add markdown input
2 parents 8c9c70a + 1c57802 commit 6992c91

File tree

10 files changed

+194
-2
lines changed

10 files changed

+194
-2
lines changed

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ PATH
1111
activeadmin_addons (1.9.0)
1212
active_material
1313
railties
14+
redcarpet
1415
require_all
1516
sassc
1617
sassc-rails
@@ -249,6 +250,7 @@ GEM
249250
rb-fsevent (0.10.3)
250251
rb-inotify (0.10.1)
251252
ffi (~> 1.0)
253+
redcarpet (3.5.1)
252254
regexp_parser (1.6.0)
253255
require_all (3.0.0)
254256
responders (3.0.0)

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ You can show `Array` or `Hash` values as html lists.
134134

135135
[Read more!](docs/list.md)
136136

137+
#### Markdown
138+
139+
You can render text as markdown.
140+
141+
<img src="./docs/images/markdown-row.png" height="250" />
142+
143+
[Read more!](docs/markdown.md)
144+
137145
### Inputs
138146

139147
#### Select2 Input

activeadmin_addons.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
2020

2121
s.add_dependency "active_material"
2222
s.add_dependency "railties"
23+
s.add_dependency "redcarpet"
2324
s.add_dependency "require_all"
2425
s.add_dependency "sassc"
2526
s.add_dependency "sassc-rails"

docs/images/markdown-row.png

73.6 KB
Loading

docs/markdown.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Markdown
2+
3+
## Markdown Column
4+
5+
Renders text as markdown in the index view.
6+
7+
You just need to use `markdown_column` method.
8+
9+
```ruby
10+
index do
11+
markdown_column :description
12+
end
13+
```
14+
15+
## Markdown Row
16+
17+
Renders text as markdown in the show view.
18+
19+
You just need to use `markdown_row` method.
20+
21+
```ruby
22+
show do
23+
attributes_table do
24+
markdown_row :description
25+
end
26+
end
27+
```
28+
29+
## Options
30+
31+
This builder uses [Redcarpet](https://github.com/vmg/redcarpet) to render the text. You can pass any Redcarpet options to it. Using the options `extensions` and `render_options` to modify existing [Redcarpet options](https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use).
32+
33+
```ruby
34+
index do
35+
markdown_column :description, extensions: { footnotes: true }
36+
end
37+
38+
index do
39+
markdown_column :description, render_options: { hard_wrap: true }
40+
end
41+
```
42+
43+
### Builder default options
44+
45+
The builder enables the following options by default to provide a plug-and-play experience:
46+
47+
#### Extensions
48+
``` json
49+
fenced_code_blocks: true
50+
no_intra_emphasis: true
51+
strikethrough: true
52+
superscript: true
53+
```
54+
55+
#### Render options
56+
57+
```json
58+
filter_html: true,
59+
hard_wrap: true
60+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'redcarpet'
2+
3+
module ActiveAdminAddons
4+
class MarkdownBuilder < CustomBuilder
5+
def render
6+
return if data.blank?
7+
8+
Redcarpet::Markdown.new(renderer, extensions).render(@data).html_safe
9+
end
10+
11+
private
12+
13+
def extensions_default
14+
{ fenced_code_blocks: true,
15+
no_intra_emphasis: true,
16+
strikethrough: true,
17+
superscript: true }
18+
end
19+
20+
def renderer_options_default
21+
{ filter_html: true, hard_wrap: true }
22+
end
23+
24+
def extensions
25+
@extensions ||= extensions_default.merge(options[:extensions] || {})
26+
end
27+
28+
def render_options
29+
@render_options ||= renderer_options_default.merge(options[:render_options] || {})
30+
end
31+
32+
def renderer
33+
@renderer ||= Redcarpet::Render::HTML.new(render_options)
34+
end
35+
end
36+
end
37+
38+
ActiveAdminAddons::MarkdownBuilder.create_view_methods

spec/dummy/app/admin/invoices.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ActiveAdmin.register Invoice do
22
permit_params :legal_date, :number, :paid, :state, :attachment, :photo, :category_id, :city_id,
3-
:amount, :color, :updated_at, :picture, :active, item_ids: [], other_item_ids: []
3+
:amount, :color, :updated_at, :picture, :active, :description, item_ids: [], other_item_ids: []
44

55
filter :id, as: :numeric_range_filter
66

@@ -21,6 +21,7 @@
2121
attachment_column :attachment
2222
number_column :amount, as: :currency, unit: "$", separator: ","
2323
toggle_bool_column :active
24+
markdown_column :description
2425
column :created_at
2526
actions
2627
end
@@ -40,6 +41,7 @@
4041
row :legal_date
4142
number_row("Monto", :amount, as: :human, &:amount)
4243
row :city
44+
markdown_row(:description)
4345
bool_row :active
4446
end
4547

@@ -85,6 +87,8 @@
8587

8688
f.input :attachment
8789

90+
f.input :description
91+
8892
f.input :legal_date
8993

9094
f.input :photo
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddDescriptionToInvoice < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :invoices, :description, :text
4+
end
5+
end

spec/dummy/db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2022_04_01_181023) do
13+
ActiveRecord::Schema.define(version: 2022_06_17_213244) do
1414

1515
create_table "active_admin_comments", force: :cascade do |t|
1616
t.string "namespace"
@@ -94,6 +94,7 @@
9494
t.boolean "active", default: true
9595
t.string "shipping_status"
9696
t.text "picture_data"
97+
t.text "description"
9798
t.index ["category_id"], name: "index_invoices_on_category_id"
9899
t.index ["city_id"], name: "index_invoices_on_city_id"
99100
end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
require 'rails_helper'
2+
3+
describe "Markdown Builder", type: :feature do
4+
context "shows text as markdown" do
5+
before do
6+
register_index(Invoice) do
7+
markdown_column :description
8+
end
9+
end
10+
11+
context "with markdown text" do
12+
before do
13+
create_invoice(description: "# Header **bold** *italic*")
14+
visit admin_invoices_path
15+
end
16+
17+
it "shows parsed markdown" do
18+
expect(page.html).to include("<h1>Header <strong>bold</strong> <em>italic</em></h1>")
19+
end
20+
end
21+
end
22+
23+
context "shows text as markdown with custom options" do
24+
before do
25+
register_index(Invoice) do
26+
markdown_column :description, extensions: { highlight: true }
27+
end
28+
end
29+
30+
context "with markdown text" do
31+
before do
32+
create_invoice(description: "This is ==highlighted==")
33+
visit admin_invoices_path
34+
end
35+
36+
it "shows parsed markdown" do
37+
expect(page.html).to include("This is <mark>highlighted</mark>")
38+
end
39+
end
40+
end
41+
42+
context "passing a block" do
43+
before do
44+
register_show(Invoice) do
45+
markdown_row(:description) do
46+
"# Header **bold** *italic*"
47+
end
48+
end
49+
50+
visit admin_invoice_path(create_invoice)
51+
end
52+
53+
it "shows parsed markdown" do
54+
expect(page.html).to include("<h1>Header <strong>bold</strong> <em>italic</em></h1>")
55+
end
56+
end
57+
58+
context "passing a block with custom options" do
59+
before do
60+
register_show(Invoice) do
61+
markdown_row(:description, extensions: { highlight: true }) do
62+
"This is ==highlighted=="
63+
end
64+
end
65+
66+
visit admin_invoice_path(create_invoice)
67+
end
68+
69+
it "shows parsed markdown" do
70+
expect(page.html).to include("This is <mark>highlighted</mark>")
71+
end
72+
end
73+
end

0 commit comments

Comments
 (0)