Sha256: 079f4aeefa328a66987db5c85984c204bfdcc5b3ee12ceba9844357b40bb53f9

Contents?: true

Size: 1.03 KB

Versions: 24

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

# rubocop:disable Style/ClassAndModuleChildren
module Maglev::GetPageSections::TransformLinkConcern
  def transform_link_content_setting(content, _setting)
    return unless content['value'].is_a?(Hash) && %w[page static_page].include?(content.dig('value', 'link_type'))

    content['value'] = replace_href_in_link(content['value'])
  end

  def replace_href_in_link(link)
    path = get_fullpath_from_link(link)

    if path
      anchor = link['section_id']
      link['href'] = anchor.present? ? "#{path}#section-#{anchor}" : path
    end

    link
  end

  def get_fullpath_from_link(link)
    is_static_page = link['link_type'] == 'static_page'
    page_id = link['link_id']

    # since the static pages don't have a preview version, we need the raw url
    if is_static_page
      page = fetch_static_pages.call.find { |static_page| static_page.id == page_id }
      page&.path
    else
      get_page_fullpath.call(page: page_id, locale: locale)
    end
  end
end
# rubocop:enable Style/ClassAndModuleChildren

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
maglevcms-1.7.3 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.7.2 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.7.1 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.7.0 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.6.1 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.6.0 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.5.1 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.4.0 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.3.0 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.2.2 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.2.1 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.2.0 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.1.7 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.1.6 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.1.5 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.1.4 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.1.3 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.1.2 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.1.1 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb
maglevcms-1.1.0 app/services/concerns/maglev/get_page_sections/transform_link_concern.rb