Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🥔✨ Journal: Entries link to their Keywords #1684

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other name suggestions my brain comes up with:

  • replace_keywords_with_link
  • keywords_to_link
  • linkify_keywords

☺️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like linkify_keywords! Wil try to do that.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super helpful! ❤️

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 @[email protected] #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