From da1874a5a3adbf5970ac672a030532784449128d Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Fri, 17 Oct 2025 11:57:52 +1100 Subject: [PATCH] Simplify complicated node replacement logic This extra work was necessary because of a bug in `Nokogiri::XML::Node#replace`! See https://github.com/sparklemotion/nokogiri/issues/3567 and https://github.com/sparklemotion/nokogiri/pull/3568 for a fix. This MR fails CI because that fix isn't released yet. Assuming it does get merged and released, this MR can be updated to use the new Nokogiri version, at which point it'll be ready for merge. --- .../filter/references/reference_filter.rb | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/lib/banzai/filter/references/reference_filter.rb b/lib/banzai/filter/references/reference_filter.rb index 07916f69e60f01..08d1218906acdb 100644 --- a/lib/banzai/filter/references/reference_filter.rb +++ b/lib/banzai/filter/references/reference_filter.rb @@ -266,24 +266,8 @@ def replace_text_with_html(node, index, html) end def replace_and_update_new_nodes(node, index, html) - previous_node = node.previous - next_node = node.next - parent_node = node.parent - # Unfortunately node.replace(html) returns re-parented nodes, not the actual replaced nodes in the doc - # We need to find the actual nodes in the doc that were replaced - node.replace(html) - @new_nodes[index] = [] - - # We replaced node with new nodes, so we find first new node. If previous_node is nil, we take first parent child - new_node = previous_node ? previous_node.next : parent_node&.children&.first - - # We iterate from first to last replaced node and store replaced nodes in @new_nodes - while new_node && new_node != next_node - @new_nodes[index] << new_node.xpath(query) - new_node = new_node.next - end - - @new_nodes[index].flatten! + created = node.replace(html) + @new_nodes[index] = created.xpath(query).to_a end def only_path? -- GitLab