|
1 | 1 | ---
|
2 |
| -version: '3.17' |
| 2 | +version: '3.17.0' |
3 | 3 | license: community
|
| 4 | +betaStatus: Beta |
4 | 5 | ---
|
5 | 6 |
|
6 | 7 | # Markdown
|
7 | 8 |
|
8 | 9 | :::info
|
9 |
| -In Avo 3.17 we renamed the `markdown` field `easy_mde` and introduced this custom one. |
| 10 | +In Avo 3.17 we renamed the `markdown` field `easy_mde` and introduced this custom one based on the [Marksmith editor](https://github.com/avo-hq/marksmith). |
| 11 | + |
| 12 | +Please read the docs on the repo for more information on how it works. |
10 | 13 | :::
|
11 | 14 |
|
12 | 15 | This field is inspired by the wonderful GitHub editor we all love and use.
|
13 | 16 |
|
14 |
| -It supports applying styles to the markup and dropping files in the editor. The file will be taken over by Rails and persisted using Active Storage. |
15 |
| - |
| 17 | +It supports applying styles to the markup, dropping files in the editor, and using the [Media Library](./media-library). |
| 18 | +The uploaded files will be taken over by Rails and persisted using Active Storage. |
16 | 19 |
|
17 | 20 | ```ruby
|
18 | 21 | field :body, as: :markdown
|
19 | 22 | ```
|
20 | 23 |
|
21 | 24 | :::warning
|
22 |
| -Please add these gems to your `Gemfile`. |
| 25 | +Please ensure you have these gems in your `Gemfile`. |
23 | 26 |
|
24 | 27 | ```ruby
|
25 |
| -gem "avo-markdown_field" |
26 |
| -gem "redcarpet" |
| 28 | +gem "marksmith" |
27 | 29 | ```
|
28 | 30 | :::
|
29 | 31 |
|
30 |
| -## Customize the parser & renderer |
| 32 | +## Customize the renderer |
31 | 33 |
|
32 | 34 | There are two places where we parse the markdown into the HTML you see.
|
33 | 35 |
|
34 | 36 | 1. In the controller
|
35 | 37 | 2. In the <Show /> field component
|
36 | 38 |
|
37 |
| -You may customize the renderer by overriding the implementation. |
38 |
| - |
39 |
| -### Everywhere |
40 |
| - |
41 |
| -Both parsers inherit from the same parser from the field. So you can override in the field and it will be visible everywhere. |
| 39 | +You may customize the renderer by overriding the model. |
42 | 40 |
|
43 | 41 | ```ruby
|
44 |
| -# config/initializers/avo.rb |
45 |
| - |
46 |
| -module Avo |
47 |
| - module Fields |
48 |
| - class MarkdownField < BaseField |
49 |
| - def self.parser |
50 |
| - # update this to your liking |
51 |
| - renderer = ::Redcarpet::Render::HTML.new(hard_wrap: true) |
52 |
| - ::Redcarpet::Markdown.new(renderer, lax_spacing: true, fenced_code_blocks: true, hard_wrap: true) |
53 |
| - end |
| 42 | +# app/models/marksmith/renderer.rb |
| 43 | + |
| 44 | +require "redcarpet" |
| 45 | + |
| 46 | +module Marksmith |
| 47 | + class Renderer |
| 48 | + def renderer |
| 49 | + ::Redcarpet::Markdown.new( |
| 50 | + ::Redcarpet::Render::HTML, |
| 51 | + tables: true, |
| 52 | + lax_spacing: true, |
| 53 | + fenced_code_blocks: true, |
| 54 | + space_after_headers: true, |
| 55 | + hard_wrap: true, |
| 56 | + autolink: true, |
| 57 | + strikethrough: true, |
| 58 | + underline: true, |
| 59 | + highlight: true, |
| 60 | + quote: true, |
| 61 | + with_toc_data: true |
| 62 | + ) |
54 | 63 | end
|
55 | 64 | end
|
56 | 65 | end
|
57 | 66 | ```
|
58 |
| - |
59 |
| -### In the controller |
60 |
| - |
61 |
| -```ruby |
62 |
| -module Avo |
63 |
| - class MarkdownPreviewsController < ApplicationController |
64 |
| - def create |
65 |
| - # fetch the resource to get some information off of it |
66 |
| - resource_class = Avo.resource_manager.get_resource_by_name(params[:resource_class]) |
67 |
| - # initialize it |
68 |
| - resource = resource_class.new view: :edit |
69 |
| - # run field detection |
70 |
| - resource.detect_fields |
71 |
| - # fetch the field |
72 |
| - field = resource.get_field(params[:field_id]) |
73 |
| - # render the result |
74 |
| - @result = field.parser.render(params[:body]) |
75 |
| - end |
76 |
| - end |
77 |
| -end |
78 |
| -``` |
79 |
| - |
80 |
| -### In the `ShowFieldComponent` |
81 |
| - |
82 |
| -```ruby |
83 |
| -# app/components/avo/fields/markdown_field/show_component.rb |
84 |
| - |
85 |
| -class Avo::Fields::MarkdownField::ShowComponent < Avo::Fields::ShowComponent |
86 |
| - def parsed_body |
87 |
| - renderer = Redcarpet::Render::HTML.new(hard_wrap: true) |
88 |
| - parser = Redcarpet::Markdown.new(renderer, lax_spacing: true, fenced_code_blocks: true, hard_wrap: true) |
89 |
| - parser.render(@field.value) |
90 |
| - end |
91 |
| -end |
92 |
| -``` |
0 commit comments