Skip to content

Commit

Permalink
Journal: Entries with @handle@example.com link to https://example.com…
Browse files Browse the repository at this point in the history
…/@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
zspencer authored Dec 21, 2022
1 parent e46c767 commit 25868ed
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 37 deletions.
85 changes: 49 additions & 36 deletions app/furniture/journal/entry.rb
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
12 changes: 12 additions & 0 deletions app/furniture/journal/renderer.rb
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
6 changes: 5 additions & 1 deletion app/lib/renders_markdown.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module RendersMarkdown
def render_markdown(content)
RendersMarkdown.renderer.render(content)
if self.class.respond_to?(:renderer)
self.class.renderer.render(content)
else
RendersMarkdown.renderer.render(content)
end
end

def self.renderer
Expand Down
16 changes: 16 additions & 0 deletions spec/furniture/journal/entry_spec.rb
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

0 comments on commit 25868ed

Please sign in to comment.