-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Journal: Entries with @handle@example.com link to https://example.com…
…/@handle (#998) * Journal: Entries with @handle@example.com link to https://example.com/@handle https://github.com/zinc-collective/convene/issues/898 There's probably a better way to do this that takes into account that handles may not use the same format on every fediverse instance; but we'll cross that bridge when we come to it.
- Loading branch information
Showing
4 changed files
with
82 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,62 @@ | ||
class Journal::Entry < ApplicationRecord | ||
self.table_name = "journal_entries" | ||
include RendersMarkdown | ||
extend StripsNamespaceFromModelName | ||
class Journal | ||
class Entry < ApplicationRecord | ||
self.table_name = "journal_entries" | ||
include RendersMarkdown | ||
extend StripsNamespaceFromModelName | ||
|
||
scope :recent, -> { order("published_at DESC NULLS FIRST") } | ||
scope :recent, -> { order("published_at DESC NULLS FIRST") } | ||
|
||
attribute :headline, :string | ||
attribute :body, :string | ||
validates :body, presence: true | ||
attribute :headline, :string | ||
attribute :body, :string | ||
validates :body, presence: true | ||
|
||
# URI-friendly description of the entry. | ||
attribute :slug, :string | ||
validates :slug, uniqueness: {scope: :journal_id} | ||
# URI-friendly description of the entry. | ||
attribute :slug, :string | ||
validates :slug, uniqueness: {scope: :journal_id} | ||
|
||
# FriendlyId does the legwork to make the slug uri-friendly | ||
extend FriendlyId | ||
friendly_id :headline, use: :scoped, scope: :journal | ||
# FriendlyId does the legwork to make the slug uri-friendly | ||
extend FriendlyId | ||
friendly_id :headline, use: :scoped, scope: :journal | ||
|
||
attribute :published_at, :datetime | ||
attribute :published_at, :datetime | ||
|
||
# @!attribute journal | ||
# @return [Journal::Journal] | ||
belongs_to :journal, class_name: "Journal::Journal", inverse_of: :entries | ||
delegate :room, :space, to: :journal | ||
# @!attribute journal | ||
# @return [Journal::Journal] | ||
belongs_to :journal, class_name: "Journal::Journal", inverse_of: :entries | ||
delegate :room, :space, to: :journal | ||
|
||
def published? | ||
published_at.present? | ||
end | ||
def published? | ||
published_at.present? | ||
end | ||
|
||
def to_html | ||
render_markdown(body) | ||
end | ||
def to_html | ||
render_markdown(body) | ||
end | ||
|
||
def to_param | ||
slug | ||
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 to_param | ||
slug | ||
end | ||
|
||
def location(action = :show) | ||
case action | ||
when :new | ||
[:new] + journal.location + [:entry] | ||
when :edit | ||
[:edit] + journal.location + [self] | ||
else | ||
journal.location + [self] | ||
def location(action = :show) | ||
case action | ||
when :new | ||
[:new] + journal.location + [:entry] | ||
when :edit | ||
[:edit] + journal.location + [self] | ||
else | ||
journal.location + [self] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Journal | ||
class Renderer < Redcarpet::Render::Safe | ||
def autolink(link, link_type) | ||
return link if link_type == :email | ||
"<a href=\"#{link}\">#{link}</a>" | ||
end | ||
|
||
def postprocess(doc) | ||
doc.gsub(/@([a-zA-Z\d]*)@(.*\.[a-zA-Z]*)/, '<a href="https://\2/@\1">@\1@\2</a>') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Journal::Entry do | ||
subject(:entry) { build(:journal_entry, body: body) } | ||
|
||
describe "#to_html" do | ||
subject(:to_html) { entry.to_html } | ||
|
||
context "when #body is 'https://www.google.com @[email protected]'" do | ||
let(:body) { "https://www.google.com @[email protected]" } | ||
|
||
it { is_expected.to include('<a href="https://weirder.earth/@zee">@[email protected]</a>') } | ||
it { is_expected.to include('<a href="https://www.google.com">https://www.google.com</a>') } | ||
end | ||
end | ||
end |