Skip to content

Commit 89ea06c

Browse files
committed
added a (not perfect) markdown preview when editing a page body in the admin dashboard
1 parent 9e2996c commit 89ea06c

File tree

6 files changed

+30
-3
lines changed

6 files changed

+30
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
$(document).on('rails_admin.dom_ready', function() {
2+
if($('.markdown-input').length && $('.markdown-output').length){
3+
const input = $('.markdown-input')[0]
4+
const output = $('.markdown-output')[0]
5+
output.innerHTML = marked.parse(input.value)
6+
input.addEventListener('input', function(a,b) {
7+
output.innerHTML = marked.parse(input.value)
8+
})
9+
}
10+
});

app/controllers/pages_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ class PagesController < ApplicationController
22
before_action :load_game
33

44
def show
5-
@page = @game.pages.find_by path: params[:id]
5+
@page = @game.pages.find_by path: params[:id].downcase
66
end
77
end

app/models/page.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
class Page < ApplicationRecord
22
belongs_to :game
33

4-
validates :path, presence: true, uniqueness: { scope: :game_id }, :format => { with: /\A[a-zA-Z\-]*\z/ , :message => 'path can only consist of letters and dashes' }
4+
validates :path, presence: true, uniqueness: { scope: :game_id, case_sensitive: false }, :format => { with: /\A[a-zA-Z\-]*\z/ , :message => 'path can only consist of letters and dashes' }
5+
before_save :downcase_path
6+
7+
def preview
8+
self.body
9+
end
10+
11+
def downcase_path
12+
self.path.downcase!
13+
end
514
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
%textarea{class: 'form-control markdown-input', rows: '10', cols: '64', name: 'page[body]', id: 'page_body'}=form.object.body
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%div{class: 'markdown-output', rows: 10, cols: 64, disabled: true}=form.object.body
2+
= javascript_include_tag "https://cdn.jsdelivr.net/npm/marked/marked.min.js"

config/initializers/rails_admin.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,12 @@
597597
config.model 'Page' do
598598
edit do
599599
field :title
600-
field :body
600+
field :body do
601+
partial "markdown_input"
602+
end
603+
field :preview do
604+
partial "markdown_output"
605+
end
601606
field :path do
602607
label 'mitrestemctf.org/game/pages/'
603608
end

0 commit comments

Comments
 (0)