Browse Source

Event's description has their eventual empty paragraphs removed.

Plus remove a strange {::} markdown artifact
remotes/origin/master
echarp 3 years ago
parent
commit
803511cde2
  1. 14
      app/helpers/events_helper.rb
  2. 5
      app/models/event.rb
  3. 8
      test/helpers/events_helper_test.rb

14
app/helpers/events_helper.rb

@ -63,10 +63,18 @@ module EventsHelper
tags: %w[p br h1 h2 h3 h4 table tr th td ul ol li a strong
b em i sub sup],
attributes: %w[href]
Kramdown::Document.new(desc, input: :html, line_width: line_width)
Kramdown::Document.new(spaces(desc), input: :html, line_width: line_width)
.to_kramdown
.remove(/ +$/) # Remove extraneous line feeds
.gsub(/\\([\"'])/, '\1') # Remove slash before quotes
.gsub(/(\n\n)\n+/, '\1') # Fewer line feeds
.remove(/{::}/) # Markdown artefact
end
private
# Cleaner html elements, to correct things like <em> test</em>
def spaces(desc)
desc.gsub(/<(strong|em)> /, ' <\1>')
.gsub(%r{ </(strong|em)>}, '</\1> ')
.gsub(/ ([,.])/, '\1')
end
end

5
app/models/event.rb

@ -95,6 +95,11 @@ class Event < ApplicationRecord
tag_list.map { |tag| "##{tag.tr('-', '_').camelize :lower}" }
end
def description
# Remove empty paragraphs, which are added by tinymce
self[:description]&.remove '<p> </p>'
end
def to_s
"#{start_time.to_date} #{city}: #{title} #{hashtags.join(' ')}"
end

8
test/helpers/events_helper_test.rb

@ -9,4 +9,12 @@ class EventsHelperTest < ActionView::TestCase
assert_equal '', to_markdown('
')
end
test 'HTML elements with a space inside' do
assert_equal '**hello world**', to_markdown('<strong>hello world </strong>')
assert_equal '**h**,', to_markdown('<strong> h</strong>,')
assert_equal '**h**,', to_markdown('<strong>h </strong>,')
assert_equal '*ho***h**,', to_markdown('<em>ho</em><strong>h </strong>,')
assert_equal '*ho* **h**,', to_markdown('<em>ho</em><strong> h</strong>,')
end
end

Loading…
Cancel
Save