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 diff --git a/spec/furniture/journal/entry_spec.rb b/spec/furniture/journal/entry_spec.rb index 8e07f8b50..7780ea722 100644 --- a/spec/furniture/journal/entry_spec.rb +++ b/spec/furniture/journal/entry_spec.rb @@ -25,7 +25,7 @@ expect(journal.keywords.where(canonical_keyword: "GoodTimes")).to exist expect(journal.keywords.where(canonical_keyword: "HardCider")).to exist expect(journal.keywords.where(canonical_keyword: "BadApples")).not_to exist - expect(entry.reload.keywords).to eq(["GoodTimes", "HardCider", "BadApple"]) + expect(entry.reload.keywords).to contain_exactly(good_times, journal.keywords.find_by(canonical_keyword: "HardCider"), bad_apple) end end end