Files
new-website-jekyll-theme/_plugins/update_internal_permalinks.rb
2025-05-07 18:05:32 -03:00

29 lines
1.0 KiB
Ruby

# frozen_string_literal: true
# Discover internal links created from the Sutty CMS and update the URLs
# if they changed. This plugin also serves as a base to add other
# things, such as more involved tooltips and previews, based on metadata
# obtained from the linked_doc.
Jekyll::Hooks.register :documents, :post_convert do |doc|
# Not every document will have internal links, so we don't need to
# parse their content.
next unless doc.content.include? 'data-id'
doc.to_html.css('a[data-id]').each do |internal_link|
linked_doc = doc.site.collections.values.map(&:docs).flatten.find do |x|
x.data['uuid'] == internal_link['data-id']
end
if linked_doc
Jekyll.logger.info doc.relative_path, 'Updating internal link'
internal_link.delete('data-id')
internal_link['href'] = linked_doc.url
internal_link['title'] = linked_doc.data['title']
else
Jekyll.logger.warn doc.relative_path, "Removing broken internal link for #{internal_link}"
internal_link.replace(internal_link.text)
end
end
end