MichaelHatherly / CommonMark.jl

A CommonMark-compliant Markdown parser for Julia.
Other
84 stars 11 forks source link

Markup -> Plaintext #18

Closed tlienart closed 3 years ago

tlienart commented 3 years ago

Hello Mike,

In the context of dealing with RSS (not the most fun thing to figure out), it'd be useful to have a way to go from "markdown" to "plaintext". The reason is that in RSS the <description> field, if given along with a <content:encoded> one, should be plain text (no markup). You can write <[CDATA... stuff but in theory you shouldn't, the encoding should go in the content encoded.

As a result the useful thing to have is a way for people to write:

+++
abstract = "This is a description of the blog post with **markup** and [links](https://juliacon.org)"
+++

{{insert abstract}}

... Rest of the blog post ...

and have the {{insert abstract}} do two things:

This is a description of the blog post with markup and links.

so that would use a "reverse" path: MD -> text. Is that doable easily with common mark?

Thanks!

MichaelHatherly commented 3 years ago

Hey,

Kind of depends on how feature complete you'd want the plaintext to be I guess. Something like the following already does an alright job of downgrading to plaintext:

julia> using CommonMark

julia> ast = cm"This is a description of the blog post with **markup** and [links](https://juliacon.org)";

julia> function plaintext(io, ast)
           for (node, enter) in ast
               isa(node.t, CommonMark.Text) && print(io, node.literal)
           end
       end
plaintext (generic function with 1 method)

julia> plaintext(stdout, ast)
This is a description of the blog post with markup and links
tlienart commented 3 years ago

Thanks for the quick response! I think this does the job just fine :)