Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

technicat/HTML2Markdown

 
 

Repository files navigation

HTML2Markdown

What is this?

It's a Swift Package which attempts to convert HTML into Markdown.

What? That's a stupid idea!

Yes.

How do I use it?

let html = "<p>This is a <em>terrible</em> idea.<br/>I must be daft.</p>"

do {
    let dom = try HTMLParser().parse(html: html)
    let markdown = dom.markdownFormatted(options: .unorderedListBullets)
    print(markdown)
} catch {
    // parsing error
}

This generates the following markdown string:

This is a *terrible* idea.  \nI must be daft.

What is supported?

  • <strong> and <em> for highlighting text
  • ordered and unordered lists (<ol> and <ul>)
  • paragraphs (<p>) and line breaks (<br>)
  • hyperlinks (<a href="...">)

All other HTML tags are removed.

Note: SwiftUI.Text currently cannot render Markdown lists, so I've added a MarkdownGenerator.Options.unorderedListBullets option to generate nicer-looking bullets: instead of *.

License

MIT. See LICENSE.md

Languages

  • Swift 100.0%