Skip to content

Commit

Permalink
Simplify markdown preview
Browse files Browse the repository at this point in the history
No need to duplicate `helpers.emojify` and manually render markdown
if we can just render a partial.
  • Loading branch information
hennevogel committed Sep 23, 2024
1 parent 65c0fa5 commit b3893e3
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 25 deletions.
11 changes: 1 addition & 10 deletions app/controllers/markdown_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
class MarkdownController < ApplicationController
skip_before_action :authenticate_user!, only: [:preview]
respond_to :js

def preview
if params[:source]
markdown_source = params[:source].to_str.gsub(/(?<=^|\s):([\w+-]+):(?=\s|$)/) do |match|
%(![add-emoji](https://github.githubassets.com/images/icons/emoji/#{match.to_str.tr(':', '')}.png))
end
end
@rendered = MarkdownHelper.render markdown_source
respond_with @rendered
@markdown_source = helpers.emojify(params[:source])
end
end
4 changes: 0 additions & 4 deletions app/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
module MarkdownHelper
def self.render(markdown_source)
Haml::Filters::Markdown.new.render markdown_source
end

def mdpreview(markdown_source, lines: 3)
markdown_source.lines.grep_v(/\[comment\]/).grep(/\S/)[0..lines - 1].join
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/comments/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
.comment-form-body
.tab-content
.tab-pane.active.fade.in{ role: 'tab-pane', id: "markdown-source#{id}" }
= f.text_area :text, :placeholder => "Your comment. You can use markdown.", :class => 'form-control input-lg markdown-source-text', :required => "required"
= f.text_area :text, :placeholder => "Your comment. You can use markdown.", :class => 'form-control input-lg', :required => "required"
.tab-pane.fade{ role: 'tab-pane', id: "markdown-preview#{id}" }
.loading-spinner
= icon('fas', 'spinner pulse 3x')
Expand Down
2 changes: 1 addition & 1 deletion app/views/keywords/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

.form-group
= f.label('Description (maximum 255 characters)')
= f.text_area :description, maxlength: "255", rows: 5, id: 'keyword_description', class: 'form-control input-lg markdown-source-text'
= f.text_area :description, maxlength: "255", rows: 5, id: 'keyword_description', class: 'form-control input-lg'
.form-group
= f.label('Keyword Logo (Will be resized to 150x150 Pixels)')
= f.file_field :avatar
Expand Down
2 changes: 2 additions & 0 deletions app/views/markdown/_preview.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:markdown
#{markdown_source}
2 changes: 1 addition & 1 deletion app/views/markdown/preview.js.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$('#<%= params[:form_parent]%> .preview-contents').html("<%=j raw @rendered %>");
$('#<%= params[:form_parent]%> .preview-contents').html("<%= escape_javascript(render partial: 'preview', locals: { markdown_source: @markdown_source }) %>");
$('#<%= params[:form_parent]%> .loading-spinner').addClass('hidden');
$('#<%= params[:form_parent]%> .preview-contents').removeClass('hidden');
$('input[name="authenticity_token"]').val('<%= form_authenticity_token %>');
Expand Down
2 changes: 1 addition & 1 deletion app/views/projects/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#markdown-source.tab-pane.active.fade.in{ role: 'tab-pane' }
.form-group
= f.text_area :description, rows: 20, id: 'project_description',
class: 'form-control input-lg markdown-source-text'
class: 'form-control input-lg'
#markdown-preview.tab-pane.fade{ role: 'tab-pane' }
.loading-spinner
= icon('fas', 'spinner pulse 3x')
Expand Down
13 changes: 6 additions & 7 deletions spec/controllers/markdown_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
require 'rails_helper'

RSpec.describe MarkdownController, type: :controller do
describe 'GET #preview' do
it 'correctly assigns rendered html' do
source = '*italic*'

get :preview, xhr: true, params: { source: source }
render_views

expect(response).to be_successful
expect(assigns(:rendered)).to eq "<p><em>italic</em></p>\n"
describe 'GET #preview' do
it 'renders a markdown preview' do
sign_in create :user
get :preview, xhr: true, params: { source: '**hans**' }
expect(response.body).to include('$(\'# .preview-contents\').html("<p><strong>hans<\/strong><\/p>\n\n");')
end
end
end

0 comments on commit b3893e3

Please sign in to comment.