Skip to content

Process footnotes #101

@fauno

Description

@fauno

I need to process footnotes and came up with this extension, if you think it could be a generic way to process footnotes, I'll be happy to open a PR.

# frozen_string_literal: true

require 'reverse_markdown'

# Sent as a patch to reverse_markdown
# https://github.com/xijo/reverse_markdown/issues/101
module ReverseMarkdown
  module Converters
    class Footnote < A
      def convert(node, state = {})
        # If the link has a circular reference, we need to check if it's
        # inside a paragraph or it's the first element of a paragraph or
        # list item.
        if node['id'] && node['href']&.start_with?('#')
          parent = node.parent

          # The link could be contained in a <sup>
          until %[p li].include?(parent.name) do
            parent = parent.parent

            # Don't go further than this
            break if parent.name == 'body'
          end

          first_child = parent.first_element_child

          # If it's the first link on the parent, it's the footnote
          # itself, otherwise it's the reference.
          if first_child == node || first_child.children.include?(node)
            "[^#{node['href'].tr('#', '')}]:"
          else
            "[^#{node['id']}]"
          end
        # Just process the link.
        else
          super
        end
      end
    end

    register :a, Footnote.new
  end
end

Edit: The footnote was pointing to it's id instead of the href for the reference. Also the markdown syntax was incorrect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions