Skip to content

Commit d05cede

Browse files
committed
Implement plaintext description method in Event model and update ApiController to use it for event descriptions
1 parent e5ff1b8 commit d05cede

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

app/controllers/api_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def events
2929
e.dtstart = Icalendar::Values::DateTime.new(ev.start_at.utc, tzid: 'UTC')
3030
e.dtend = Icalendar::Values::DateTime.new(ev.end_at.utc, tzid: 'UTC')
3131
e.summary = ev.name
32-
e.description = "Register: #{ev.url}\n\n#{ev.description}"
32+
e.description = "Register: #{ev.url}\n\n#{ev.description_plaintext}"
3333
e.location = 'Newspeak House, 133 Bethnal Green Road, London, E2 7DG, UK'
3434
e.url = ev.url
3535
e.organizer = Icalendar::Values::CalAddress.new("mailto:#{ev.organiser_email}", cn: ev.organiser_name)

app/models/event.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,24 @@ def formatted_date_details
2424
"#{start_date}#{start_time}#{end_time}#{location}"
2525
end
2626
end
27+
28+
def description_plaintext
29+
text = description.to_s.dup
30+
# Convert Markdown links and images to "text (url)"
31+
text.gsub!(/!\[([^\]]*)\]\(([^\)]+)\)/, '\\1 (\\2)')
32+
text.gsub!(/\[([^\]]+)\]\(([^\)]+)\)/, '\\1 (\\2)')
33+
# Remove emphasis/strong/code markers
34+
text.gsub!(/[*_]{1,3}([^*_]+)[*_]{1,3}/, '\\1')
35+
text.gsub!(/`{1,3}([^`]+)`{1,3}/m, '\\1')
36+
# Strip headings and blockquotes markers
37+
text.gsub!(/^\s{0,3}#+\s*/, '')
38+
text.gsub!(/^>\s?/, '')
39+
# Normalize list markers
40+
text.gsub!(/^\s*[-*+]\s+/, '- ')
41+
text.gsub!(/^\s*\d+\.\s+/, '- ')
42+
# Collapse excessive blank lines
43+
text.gsub!(/\r\n?/, "\n")
44+
text.gsub!(/\n{3,}/, "\n\n")
45+
text.strip
46+
end
2747
end

0 commit comments

Comments
 (0)