Skip to content

Commit 9fb4180

Browse files
committed
added the media library and tweaked markdown
1 parent 5f007d9 commit 9fb4180

File tree

8 files changed

+165
-68
lines changed

8 files changed

+165
-68
lines changed

docs/.vitepress/config.js

+6
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ const config = {
232232
{text: "TailwindCSS integration", link: "/3.0/tailwindcss-integration.html"},
233233
],
234234
},
235+
{
236+
text: "Media Library",
237+
items: [
238+
{text: "Overview", link: "/3.0/media-library.html"},
239+
]
240+
},
235241
{
236242
text: "Performance",
237243
items: [

docs/.vitepress/getFiles.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const getFiles = (directory, version) => {
2424
if (text === 'Easy mde') {
2525
text = 'Easy MDE'
2626
}
27+
if (text === 'Tip tap') {
28+
text = 'Tip Tap'
29+
}
2730
const link = `/${version}/${directory}/${path.replace('.md', '.html')}`
2831
return {text, link}
2932
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Option name="`always_show`">
2+
3+
By default, the content of the field is not visible on the `Show` view; instead, it's hidden under a `Show Content` link that, when clicked, displays the content. You can set it to display the content by setting `always_show` to `true`.
4+
5+
<!-- @include: ./../default_boolean_false.md-->
6+
</Option>

docs/3.0/fields/markdown.md

+32-58
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,66 @@
11
---
2-
version: '3.17'
2+
version: '3.17.0'
33
license: community
4+
betaStatus: Beta
45
---
56

67
# Markdown
78

89
:::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.
1013
:::
1114

1215
This field is inspired by the wonderful GitHub editor we all love and use.
1316

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.
1619

1720
```ruby
1821
field :body, as: :markdown
1922
```
2023

2124
:::warning
22-
Please add these gems to your `Gemfile`.
25+
Please ensure you have these gems in your `Gemfile`.
2326

2427
```ruby
25-
gem "avo-markdown_field"
26-
gem "redcarpet"
28+
gem "marksmith"
2729
```
2830
:::
2931

30-
## Customize the parser & renderer
32+
## Customize the renderer
3133

3234
There are two places where we parse the markdown into the HTML you see.
3335

3436
1. In the controller
3537
2. In the <Show /> field component
3638

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.
4240

4341
```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+
)
5463
end
5564
end
5665
end
5766
```
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-
```

docs/3.0/fields/rhino.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
version: '3.17.0'
3+
license: community
4+
---
5+
6+
# Rhino
7+
8+
The wonderful [Rhino Editor](https://rhino-editor.vercel.app/) built by [Konnor Rogers](https://www.konnorrogers.com/) is available and fully integrated with Avo.
9+
10+
```ruby
11+
field :body, as: :rhino
12+
```
13+
14+
Rhino is based on [TipTap](https://tiptap.dev/) which is a powerful and flexible editor.
15+
16+
The Rhino field supports [ActiveStorage](https://guides.rubyonrails.org/active_storage_overview.html) file attachments and seamlessly integrates with the [Media Library](./media-library).
17+
18+
## Options
19+
20+
<!-- @include: ./../common/field_options/always_show.md-->

docs/3.0/fields/tip_tap.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
---
2-
version: '3.5.0'
3-
license: community
2+
betaStatus: Deprecated
43
---
54

6-
# TipTap
5+
# Tip Tap
76

8-
WIP
7+
The `TipTap` field is deprecated in favor of the [Rhino](./rhino) field.
8+
9+
The Rhino field is a fork of the TipTap editor with some additional features and improvements.
10+
11+
The Rhino field is fully integrated with Avo and provides a seamless experience for managing rich text content using the [ActiveStorage](https://guides.rubyonrails.org/active_storage_overview.html) integration and the [Media Library](./media-library).

docs/3.0/fields/trix.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ field :body, as: :trix
1212

1313
The `Trix` field renders a [WYSIWYG Editor](https://trix-editor.org/) and can be associated with a `string` or `text` column in the database. The value stored in the database will be the editor's resulting `HTML` content.
1414

15+
It supports [ActiveStorage](https://guides.rubyonrails.org/active_storage_overview.html) file attachments and seamlessly integrates with the [Media Library](./media-library).
16+
1517
<Image src="/assets/img/fields/trix.jpg" width="877" height="193" alt="Trix field" />
1618

1719
Trix field is hidden from the `Index` view.
1820

1921
## Options
2022

21-
<Option name="`always_show`">
22-
23-
By default, the content of the `Trix` field is not visible on the `Show` view; instead, it's hidden under a `Show Content` link that, when clicked, displays the content. You can set Markdown to display the content by setting `always_show` to `true`.
24-
25-
<!-- @include: ./../common/default_boolean_false.md-->
26-
</Option>
23+
<!-- @include: ./../common/field_options/always_show.md-->
2724

2825
<Option name="`attachments_disabled`">
2926

docs/3.0/media-library.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
version: "3.17.0"
3+
betaStatus: Alpha
4+
---
5+
6+
# Media Library
7+
8+
If you run an asset-intensive, having a place to view all those asses would be great. It's becoming easier with Avo and it's Media Library feature.
9+
10+
The Media Library has two goals in mind.
11+
12+
1. Browse and manage all your assets
13+
2. Use it to inject assets in all three of Avo's rich text editors ([trix]('./fields/trix'), [rhino]('./fields/rhino'), and [markdown]('./fields/markdown')).
14+
15+
:::warning
16+
The Media Library feature is still in alpha and future releases might contain breaking changes so keep an eye out for the upgrade guide.
17+
18+
This is just the initial version and we'll be adding more features as we progress and get more feedback on usage.
19+
:::
20+
21+
## How to enable it
22+
23+
The Media Library feature is disabled by default (until we release the stable version). To enable it, you need to do the following:
24+
25+
```ruby{4}
26+
# config/initializers/avo.rb
27+
if defined?(Avo::MediaLibrary)
28+
Avo::MediaLibrary.configure do |config|
29+
config.enabled = true
30+
end
31+
end
32+
```
33+
34+
This is the killswitch of the whole feature.
35+
When disabled, the Media Library will not be available to anyone. It will hide the menu item, block the all the routes, and hide media the library icons from the editors.
36+
37+
## Hide menu item
38+
39+
You can hide the menu item from the sidebar by setting the `visible` option to `false`.
40+
41+
```ruby
42+
# config/initializers/avo.rb
43+
if defined?(Avo::MediaLibrary)
44+
Avo::MediaLibrary.configure do |config|
45+
config.visible = false
46+
end
47+
end
48+
```
49+
50+
You may also use a [block](./execution-context) to conditionally show the menu item. You'll have access to the `Avo::Current` object and you can use it to show the menu item based on the current user.
51+
52+
```ruby
53+
# config/initializers/avo.rb
54+
if defined?(Avo::MediaLibrary)
55+
Avo::MediaLibrary.configure do |config|
56+
config.visible = -> { Avo::Current.user.is_developer? }
57+
end
58+
end
59+
```
60+
61+
This will hide the menu item from the sidebar if the current user is not a developer.
62+
63+
## Add it to the menu editor
64+
65+
The Media Library is a menu item in the sidebar. You can add it to the menu editor by using the `media_library` helper.
66+
67+
```ruby
68+
# config/initializers/avo.rb
69+
Avo.configure do |config|
70+
config.main_menu = lambda {
71+
link_to 'Media Library', avo.media_library_index_path
72+
}
73+
end
74+
```
75+
76+
## Use it with the rich text editors
77+
78+
The Media Library will seamlessly integrate with all the rich text editors.
79+
80+
```ruby
81+
field :body, as: :trix
82+
field :body, as: :rhino
83+
field :body, as: :markdown
84+
```
85+
86+
The editors will each have a button to open the Media Library modal.
87+
Once open, after the user selects the asset, it will be injected into the editor.
88+

0 commit comments

Comments
 (0)