diff --git a/app/liquid_tags/link_tag.rb b/app/liquid_tags/link_tag.rb index 2136fce72..6a7a39507 100644 --- a/app/liquid_tags/link_tag.rb +++ b/app/liquid_tags/link_tag.rb @@ -1,52 +1,32 @@ class LinkTag < LiquidTagBase include ActionView::Helpers - attr_reader :article + PARTIAL = "articles/liquid".freeze def initialize(_tag_name, slug_or_path_or_url, _tokens) - @article = parse_url_for_article(slug_or_path_or_url) + @article = get_article(slug_or_path_or_url) + @title = strip_tags(@article.title) end def render(_context) - tags = article.tag_list.map { |t| "##{t}" }.join - <<-HTML -
- HTML + ActionController::Base.new.render_to_string( + partial: PARTIAL, + locals: { article: @article, title: @title }, + ) end - private + def get_article(slug) + slug = ActionController::Base.helpers.strip_tags(slug).strip + article = find_article_by_user(article_hash(slug)) || find_article_by_org(article_hash(slug)) + raise StandardError, "Invalid link URL or link URL does not exist" unless article - def parse_url_for_article(slug_or_path_or_url) - slug_or_path_or_url = ActionController::Base.helpers.strip_tags(slug_or_path_or_url).strip - - hash = article_hash(slug_or_path_or_url) - - raise_error if hash.nil? - - article = find_article_by_user(hash) || find_article_by_org(hash) - raise_error unless article article - rescue StandardError - raise_error end - def article_hash(slug_or_path_or_url) - path = Addressable::URI.parse(slug_or_path_or_url).path + def article_hash(slug) + path = Addressable::URI.parse(slug).path path.slice!(0) if path.starts_with?("/") # remove leading slash if present path.slice!(-1) if path.ends_with?("/") # remove trailing slash if present - template = Addressable::Template.new("{username}/{slug}") - template.extract(path)&.symbolize_keys + Addressable::Template.new("{username}/{slug}").extract(path)&.symbolize_keys end def find_article_by_user(hash) @@ -62,10 +42,6 @@ class LinkTag < LiquidTagBase org.articles.where(slug: hash[:slug])&.first end - - def raise_error - raise StandardError, "Invalid link URL or link URL does not exist" - end end Liquid::Template.register_tag("link", LinkTag) diff --git a/app/views/articles/_liquid.html.erb b/app/views/articles/_liquid.html.erb new file mode 100644 index 000000000..65a478b21 --- /dev/null +++ b/app/views/articles/_liquid.html.erb @@ -0,0 +1,18 @@ +