Nanoc::Filter.define(:tufte) do |content, params| doc = Nokogiri::HTML.fragment(content) # Convert to array to avoid modifying during iteration nodes = doc.children.to_a # Track the current section we're building current_section = nil # Process each node nodes.each do |node| if node.name == 'h2' # Start a new section current_section = Nokogiri::XML::Node.new('section', doc) node.add_previous_sibling(current_section) current_section.add_child(node) elsif current_section && node.name != 'h2' # Add this node to the current section current_section.add_child(node) end end doc.to_html.gsub(/\[\^([^\]]+)\]:\s*(.+)$/) do |match| ref = $1 note_content = $2.strip "#{note_content}" end end