Parsing Markdown

kramdown - fast, pure-Ruby Markdown-superset converter

ChatGPT is able to mostly code that parser itself while guided.

# mostly written by ChatGPT
require 'kramdown'

def extract_urls(el, urls)
    if el.type == :a
        urls << "#{el.attr['href']} - (#{  el.children[0].value})"
    end

    el.children.each { |child| extract_urls(child, urls) }
end
  
  def extract_markdown_urls(markdown_text)
    doc = Kramdown::Document.new(markdown_text)
    urls = []
    extract_urls(doc.root, urls)
    urls
  end

markdown_text = File.read("input.md")
urls = extract_markdown_urls(markdown_text)

puts urls

Alternatives

  • redcarpet - a Ruby library for Markdown processing

see also

  • What is Markdown? - Created by John Gruber in 2004, Markdown is now one of the world’s most popular markup languages.
Written on February 10, 2023, Last update on February 12, 2023
markdown compiler