Attempt to improve the markdown digest

This commit is contained in:
echarp 2017-06-06 23:08:10 +02:00
parent 2d9107997f
commit 1aa7aa936b
4 changed files with 14 additions and 3 deletions

View File

@ -0,0 +1,8 @@
# Helper for the digest/markdown views
module DigestHelper
# Important mechanism, to ensure the digest will not have the same footnote
# link multiple times
def renumber_footnotes(id, description)
description.gsub(/\[(\d)*\]/, "[#{id}_\\1]")
end
end

View File

@ -56,6 +56,8 @@ module EventsHelper
item.class.human_attribute_name(label).rjust(12) + " #{value}"
end
# Using kramdown, let's parse the html and render it as markdown text
# No idea why, but also needs to remove extraneous quote encoding :(
def to_markdown(description, line_width = -1)
desc = sanitize description,
tags: %w[p br h1 h2 h3 h4 table tr th td ul ol li a strong
@ -64,8 +66,9 @@ module EventsHelper
result = Kramdown::Document.new(desc, input: :html, line_width: line_width)
.to_kramdown
# Remove extraneous line feeds
return if result.nil? || result.blank?
result.gsub(/\n\n+/, '
').tr('\\', '').html_safe
').gsub(/(\w)\\'/, '\1\'')
end
end

View File

@ -8,7 +8,7 @@
- @events.sort_by(&:start_time).each do |event|
[#{event.region.region.try(:code) || event.region.try(:code)} #{event.city}] [#{event.title}](#{event_url event}) - #{display_date event}
\-----
= to_markdown event.description, -1
= renumber_footnotes event.id, to_markdown(event.description)
\
* #{[event.place_name, event.full_address].compact.join ', '}
- if event.url.present?

View File

@ -15,5 +15,5 @@
= display_attr @event, :repeat
= display_attr @event, :rule, t(@event.rule, scope: 'activerecord.attributes.event.rule_values')
\
= to_markdown @event.description, 78
= raw to_markdown @event.description, 78
==============================================================================