Class: Oembed::EmbedLinkDelocalizer

Inherits:
Object
  • Object
show all
Defined in:
app/services/oembed/embed_link_delocalizer.rb

Overview

Rewrites the internal links inside oEmbed blocks (product/FAQ/video/image)
from a concrete locale (/en-US/..., or a stray /en/... fabricated when an
embed renders under the framework-default :en locale) back to the
{locale} placeholder — the canonical form the blog editor stores and the
form Blog::ContentRules#internal_links_use_locale_placeholder requires.

Used by Oembed::ContentRefreshService (after it re-renders embeds) and by
the OnlineMigrations::DataMigrations::DelocalizeBlogEmbedLinks backfill that
heals posts whose embeds already baked a concrete locale.

Idempotent: links already in {locale} form, external links, CDN assets,
anchors and mailto/tel values are returned unchanged, so re-running on a
healed body writes nothing.

Constant Summary collapse

CONTAINER_SELECTOR =

Containers for every oEmbed type the refresher rewrites. Video embeds use
div/video elements (not ), so a single tag selector won't do.

[
  '[data-wy-oembed]',
  '.wy-faq-embed',
  '.wy-product-embed',
  '.wy-image-embed',
  '.wy-video-embed',
  '.wy-video-embed-wrapper'
].join(', ').freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(html) ⇒ String?

Parse, delocalize, and serialize a body HTML string.

Parameters:

  • html (String, nil)

    post body HTML

Returns:

  • (String, nil)

    body with embed internal links delocalized; the
    input is returned unchanged when blank.



34
35
36
37
38
39
40
# File 'app/services/oembed/embed_link_delocalizer.rb', line 34

def self.call(html)
  return html if html.blank?

  doc = Nokogiri::HTML5.fragment(html)
  new.delocalize!(doc)
  doc.to_html
end

Instance Method Details

#delocalize!(doc) ⇒ void

This method returns an undefined value.

Rewrite embed links in +doc+ in place. Callers that already hold a parsed
fragment (e.g. ContentRefreshService) use this to avoid a re-parse.

Parameters:

  • doc (Nokogiri::HTML5::DocumentFragment)


47
48
49
50
51
52
# File 'app/services/oembed/embed_link_delocalizer.rb', line 47

def delocalize!(doc)
  doc.css(CONTAINER_SELECTOR).each do |container|
    container.css('a[href]').each { |a| a['href'] = canonical_internal_link(a['href']) }
    container.css('form[action]').each { |form| form['action'] = canonical_internal_link(form['action']) }
  end
end