From af1d8ead09733456626c359b1d5a1408c57edd57 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Thu, 20 Jul 2023 12:36:11 -0700 Subject: [PATCH] `Journal`: `Entries` link to their `Keywords` - https://github.com/zinc-collective/convene/issues/1566 - https://github.com/zinc-collective/convene/issues/1662 --- app/furniture/journal/entry.rb | 4 ++++ app/furniture/journal/entry_component.rb | 15 +++++++++++++-- app/furniture/journal/keyword.rb | 6 +++++- spec/furniture/journal/entry_component_spec.rb | 1 + 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/furniture/journal/entry.rb b/app/furniture/journal/entry.rb index f75c5f84a..44747e6d3 100644 --- a/app/furniture/journal/entry.rb +++ b/app/furniture/journal/entry.rb @@ -45,6 +45,10 @@ def keywords=(keywords) super(keywords.uniq) end + def keywords + journal.keywords.search(*super) + end + def to_param slug end diff --git a/app/furniture/journal/entry_component.rb b/app/furniture/journal/entry_component.rb index 1afba665d..f0ce06c83 100644 --- a/app/furniture/journal/entry_component.rb +++ b/app/furniture/journal/entry_component.rb @@ -9,10 +9,21 @@ def initialize(*args, entry:, **kwargs) end def body_html - render_markdown(entry.body) + postprocess(render_markdown(entry.body)) end - def published_at + private def postprocess(text) + entry.keywords.map do |keyword| + search = keyword.canonical_with_aliases.sort.reverse.join("|") + text.gsub!(/\#(#{search})/i) do |match| + link_to(match, keyword.location) + end + end + + text + end + + private def published_at entry.published_at.to_fs(:long_ordinal) end diff --git a/app/furniture/journal/keyword.rb b/app/furniture/journal/keyword.rb index e0f8048cc..b207dede0 100644 --- a/app/furniture/journal/keyword.rb +++ b/app/furniture/journal/keyword.rb @@ -13,7 +13,11 @@ class Keyword < ApplicationRecord end) def entries - journal.entries.matching_keywords([canonical_keyword] + (aliases.presence || [])) + journal.entries.matching_keywords(canonical_with_aliases) + end + + def canonical_with_aliases + [canonical_keyword] + (aliases.presence || []) end def to_param diff --git a/spec/furniture/journal/entry_component_spec.rb b/spec/furniture/journal/entry_component_spec.rb index 3a12ec994..504575487 100644 --- a/spec/furniture/journal/entry_component_spec.rb +++ b/spec/furniture/journal/entry_component_spec.rb @@ -7,4 +7,5 @@ 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") } + it { is_expected.to have_link("#GoodTimes", href: polymorphic_path(entry.journal.keywords.find_by(canonical_keyword: "GoodTimes").location)) } end