From 41de505717bc8eee632546a38ba8ef471e283078 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Thu, 20 Jul 2023 11:47:39 -0700 Subject: [PATCH] Migrate Rendering to the EntryComponent so we have access to routes --- app/furniture/journal/entries/_entry.html.erb | 28 +---------------- app/furniture/journal/entry.rb | 16 ---------- .../journal/entry_component.html.erb | 27 +++++++++++++++++ app/furniture/journal/entry_component.rb | 30 +++++++++++++++++++ app/furniture/journal/renderer.rb | 4 +-- .../furniture/journal/entry_component_spec.rb | 10 +++++++ spec/furniture/journal/entry_spec.rb | 11 ------- 7 files changed, 69 insertions(+), 57 deletions(-) create mode 100644 app/furniture/journal/entry_component.html.erb create mode 100644 app/furniture/journal/entry_component.rb create mode 100644 spec/furniture/journal/entry_component_spec.rb diff --git a/app/furniture/journal/entries/_entry.html.erb b/app/furniture/journal/entries/_entry.html.erb index 4e30bd537..c773db5a0 100644 --- a/app/furniture/journal/entries/_entry.html.erb +++ b/app/furniture/journal/entries/_entry.html.erb @@ -1,27 +1 @@ -
-
-

<%= entry.headline %>

- <%- if entry.published?%> - Published on <%= link_to entry.published_at.to_fs(:long_ordinal), entry.location %> - <%- else %> - Unpublished. - <%- end %> -
- -
- <%= entry.to_html.html_safe %> -
- - -
+<%= render Journal::EntryComponent.new(entry: entry) %> diff --git a/app/furniture/journal/entry.rb b/app/furniture/journal/entry.rb index 7e51b67ae..f75c5f84a 100644 --- a/app/furniture/journal/entry.rb +++ b/app/furniture/journal/entry.rb @@ -3,7 +3,6 @@ class Entry < ApplicationRecord location(parent: :journal) self.table_name = "journal_entries" - include RendersMarkdown extend StripsNamespaceFromModelName scope :recent, -> { order("published_at DESC NULLS FIRST") } @@ -38,21 +37,6 @@ def published? published_at.present? end - def to_html - render_markdown(body) - end - - def self.renderer - @_renderer ||= Redcarpet::Markdown.new( - Renderer.new(filter_html: true, with_toc_data: true), - autolink: true, strikethrough: true, - no_intra_emphasis: true, - lax_spacing: true, - fenced_code_blocks: true, disable_indented_code_blocks: true, - tables: true, footnotes: true, superscript: true, quote: true - ) - end - def extract_keywords self.keywords = journal.keywords.extract_and_create_from!(body).pluck(:canonical_keyword) end diff --git a/app/furniture/journal/entry_component.html.erb b/app/furniture/journal/entry_component.html.erb new file mode 100644 index 000000000..02dc3f235 --- /dev/null +++ b/app/furniture/journal/entry_component.html.erb @@ -0,0 +1,27 @@ +
+
+

<%= entry.headline %>

+ <%- if entry.published?%> + Published on <%= link_to published_at, entry.location %> + <%- else %> + Unpublished. + <%- end %> +
+ +
+ <%== body_html %> +
+ + +
diff --git a/app/furniture/journal/entry_component.rb b/app/furniture/journal/entry_component.rb new file mode 100644 index 000000000..1afba665d --- /dev/null +++ b/app/furniture/journal/entry_component.rb @@ -0,0 +1,30 @@ +class Journal + class EntryComponent < ApplicationComponent + include RendersMarkdown + attr_accessor :entry + + def initialize(*args, entry:, **kwargs) + self.entry = entry + super(*args, **kwargs) + end + + def body_html + render_markdown(entry.body) + end + + def published_at + entry.published_at.to_fs(:long_ordinal) + end + + def self.renderer + @_renderer ||= Redcarpet::Markdown.new( + Renderer.new(filter_html: true, with_toc_data: true), + autolink: true, strikethrough: true, + no_intra_emphasis: true, + lax_spacing: true, + fenced_code_blocks: true, disable_indented_code_blocks: true, + tables: true, footnotes: true, superscript: true, quote: true + ) + end + end +end diff --git a/app/furniture/journal/renderer.rb b/app/furniture/journal/renderer.rb index 248ae12fc..8ac0d8888 100644 --- a/app/furniture/journal/renderer.rb +++ b/app/furniture/journal/renderer.rb @@ -6,9 +6,7 @@ def autolink(link, link_type) end def postprocess(doc) - doc - .gsub(/@([a-zA-Z\d]*)@(.*\.[a-zA-Z]*)/, '@\1@\2') - .gsub(/#(\w+)/, '{keyword_\1}') + doc.gsub(/@([a-zA-Z\d]*)@(.*\.[a-zA-Z]*)/, '@\1@\2') end end end diff --git a/spec/furniture/journal/entry_component_spec.rb b/spec/furniture/journal/entry_component_spec.rb new file mode 100644 index 000000000..3a12ec994 --- /dev/null +++ b/spec/furniture/journal/entry_component_spec.rb @@ -0,0 +1,10 @@ +require "rails_helper" + +RSpec.describe Journal::EntryComponent, type: :component do + subject(:output) { render_inline(component) } + + let(:component) { described_class.new(entry: entry) } + let(:entry) { create(:journal_entry, body: "https://www.google.com @zee@weirder.earth #GoodTimes") } + + it { is_expected.to have_link("https://www.google.com", href: "https://www.google.com") } +end diff --git a/spec/furniture/journal/entry_spec.rb b/spec/furniture/journal/entry_spec.rb index da84ea865..8e07f8b50 100644 --- a/spec/furniture/journal/entry_spec.rb +++ b/spec/furniture/journal/entry_spec.rb @@ -10,17 +10,6 @@ it { is_expected.to have_one(:room).through(:journal) } it { is_expected.to have_one(:space).through(:journal) } - describe "#to_html" do - subject(:to_html) { entry.to_html } - - context "when #body is 'https://www.google.com @zee@weirder.earth #GoodTimes'" do - let(:entry) { build(:journal_entry, body: "https://www.google.com @zee@weirder.earth #GoodTimes") } - - it { is_expected.to include('https://www.google.com') } - it { is_expected.to include("{keyword_GoodTimes}") } - end - end - describe "#save" do let(:entry) { create(:journal_entry, body: "#GoodTimes") } let(:journal) { entry.journal }