Revert "Revert "Replace House with Lexical""
This commit is contained in:
Executable
+104
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require_relative "../config/environment"
|
||||
require "redcarpet"
|
||||
require "nokogiri"
|
||||
|
||||
class ActionText::Markdown < ApplicationRecord
|
||||
belongs_to :record, polymorphic: true
|
||||
end
|
||||
|
||||
class MarkdownToActionTextConverter
|
||||
ATTACHMENT_URL_REGEX = %r{/u/(?<slug>[^\/\s\)]+)}
|
||||
|
||||
def initialize(html)
|
||||
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
||||
@attachments = []
|
||||
end
|
||||
|
||||
def convert
|
||||
process_images
|
||||
process_links
|
||||
[ @doc.to_html, @attachments ]
|
||||
end
|
||||
|
||||
private
|
||||
def process_images
|
||||
@doc.css("img").each do |img|
|
||||
src = img["src"].presence
|
||||
if src && match = src.match(ATTACHMENT_URL_REGEX)
|
||||
if (attachment = find_attachment(match[:slug]))
|
||||
img.replace(build_attachment_node(attachment))
|
||||
@attachments << attachment
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def process_links
|
||||
@doc.css("a").each do |link|
|
||||
href = link["href"].presence
|
||||
|
||||
if href && match = href.match(ATTACHMENT_URL_REGEX)
|
||||
if (attachment = find_attachment(match[:slug]))
|
||||
link.replace(build_attachment_node(attachment))
|
||||
@attachments << attachment
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def build_attachment_node(attachment)
|
||||
html = ActionText::Attachment.from_attachable(attachment).to_html
|
||||
fragment = Nokogiri::HTML::DocumentFragment.parse(html)
|
||||
|
||||
node = fragment.at_css("action-text-attachment")
|
||||
node["url"] = Rails.application.routes.url_helpers.rails_blob_path(attachment.blob, only_path: true)
|
||||
|
||||
fragment
|
||||
end
|
||||
|
||||
def find_attachment(slug)
|
||||
ActiveStorage::Attachment.find_by(slug: slug)
|
||||
end
|
||||
end
|
||||
|
||||
class RedcarpetRenderer
|
||||
def self.render(markdown)
|
||||
renderer = Redcarpet::Render::HTML.new
|
||||
markdowner = Redcarpet::Markdown.new(renderer,
|
||||
autolink: true,
|
||||
tables: true,
|
||||
fenced_code_blocks: true,
|
||||
strikethrough: true,
|
||||
superscript: true,
|
||||
)
|
||||
markdowner.render(markdown.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
ApplicationRecord.with_each_tenant do |tenant|
|
||||
puts "Processing tenant: #{tenant}"
|
||||
|
||||
ActionText::Markdown.find_each do |markdown|
|
||||
next unless markdown.record
|
||||
|
||||
html = RedcarpetRenderer.render(markdown.content.to_s)
|
||||
converter = MarkdownToActionTextConverter.new(html)
|
||||
rich_text_html, attachments = converter.convert
|
||||
|
||||
rich_text = ActionText::RichText.create!(
|
||||
name: markdown.name,
|
||||
record: markdown.record,
|
||||
body: rich_text_html
|
||||
)
|
||||
|
||||
attachments.each do |attachment|
|
||||
attachment.update!(record: rich_text)
|
||||
end
|
||||
|
||||
puts "✓ Created rich text for #{markdown.record_type}##{markdown.record_id} (#{markdown.name})"
|
||||
rescue => e
|
||||
warn "✗ Failed to process markdown ##{markdown.id}: #{e.class} - #{e.message}"
|
||||
end
|
||||
end
|
||||
@@ -214,9 +214,6 @@ class EnactTenanting
|
||||
assert_count Tagging, Tagging.count
|
||||
assert_count Watch, Watch.count
|
||||
|
||||
# we're only copying the markdown records that are still accessible.
|
||||
assert_count ActionText::Markdown, Comment.count
|
||||
|
||||
assert_count Event, Event.count
|
||||
assert_count Notification, Notification.count
|
||||
|
||||
@@ -228,22 +225,7 @@ class EnactTenanting
|
||||
end
|
||||
|
||||
def update_action_text_urls
|
||||
update_counter["ActionText::Markdown"] = 0
|
||||
TENANTS.each do |tenant|
|
||||
tenanted_domain = sprintf(TENANTED_DOMAIN, tenant: tenant)
|
||||
|
||||
ApplicationRecord.with_tenant(tenant) do
|
||||
ActionText::Markdown.all.each do |markdown|
|
||||
content = markdown.content
|
||||
next unless content =~ %r{#{CURRENT_DOMAIN}/u/}
|
||||
|
||||
content.gsub!(%r{#{CURRENT_DOMAIN}/u/}, "#{tenanted_domain}/u/")
|
||||
|
||||
markdown.update_column :content, content
|
||||
update_counter["ActionText::Markdown"] += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
raise "No more markdown"
|
||||
end
|
||||
|
||||
def hardlink_active_storage
|
||||
|
||||
@@ -100,9 +100,9 @@ ApplicationRecord.with_each_tenant do |tenant|
|
||||
end
|
||||
|
||||
# Update action_text_markdowns table (polymorphic relationship)
|
||||
ActionText::Markdown.where(record_type: "Card").find_each do |markdown|
|
||||
if id_mapping[markdown.record_id]
|
||||
markdown.update_column(:record_id, id_mapping[markdown.record_id])
|
||||
ActionText::RichText.where(record_type: "Card").find_each do |rich_text|
|
||||
if id_mapping[rich_text.record_id]
|
||||
rich_text.update_column(:record_id, id_mapping[rich_text.record_id])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -73,9 +73,9 @@ ApplicationRecord.with_each_tenant do |tenant|
|
||||
end
|
||||
|
||||
# Update action_text_markdowns table (polymorphic relationship)
|
||||
ActionText::Markdown.where(record_type: "Collection").find_each do |markdown|
|
||||
if id_mapping[markdown.record_id]
|
||||
markdown.update_column(:record_id, id_mapping[markdown.record_id])
|
||||
ActionText::RichText.where(record_type: "Collection").find_each do |rich_text|
|
||||
if id_mapping[rich_text.record_id]
|
||||
rich_text.update_column(:record_id, id_mapping[rich_text.record_id])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user