Skip to content

Commit

Permalink
🥔✨ Journal: Entries link to their Keywords (#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
zspencer authored Jul 20, 2023
1 parent a435d21 commit 5071b58
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/furniture/journal/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def keywords=(keywords)
super(keywords.uniq)
end

def keywords
journal.keywords.search(*super)
end

def to_param
slug
end
Expand Down
23 changes: 21 additions & 2 deletions app/furniture/journal/entry_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,29 @@ 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|
replace_keyword(text, keyword)
end

text
end

private def replace_keyword(text, keyword)
text.gsub!(keywords_regex(keyword)) do |match|
link_to(match, keyword.location)
end
end

# We sort the keywords longest to shortest because regex matches groups left-to-right
private def keywords_regex(keyword)
/\#(#{keyword.canonical_with_aliases.sort.reverse.join("|")})/i
end

private def published_at
entry.published_at.to_fs(:long_ordinal)
end

Expand Down
6 changes: 5 additions & 1 deletion app/furniture/journal/keyword.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/furniture/journal/entry_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/furniture/journal/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5071b58

Please sign in to comment.