kingy

kingy

Earmark - Possible to extract all text nodes from a Markdown file?

Is it possible to extract all the raw text nodes from Markdown files using Earmark?

I’ve been using the built-in map_ast_with function and not having much luck, I seem to miss a lot of text.

  defp markdown_to_text(markdown) do
    {:ok, ast, _} = Earmark.as_ast(markdown)
    fun = fn node, acc ->
      case node do
        [content] when is_binary(content) ->
          { node, [acc | content] }
        _ ->
          { node, acc }
      end
    end

    { ast, text } = Earmark.Transform.map_ast_with(ast, [], fun)
    Enum.join(text)
  end

Marked As Solved

knoebber

knoebber

how about this?

  def extract_text_from_markdown(md) when is_binary(md) do
    {:ok, ast, _} = Earmark.as_ast(md)
    String.trim(extract_text_from_ast(ast, ""))
  end

  defp extract_text_from_ast(ast, result) when is_list(ast) and is_binary(result) do
    Enum.reduce(ast, result, fn
      {_html_tag, _atts, children, _m}, acc ->
        extract_text_from_ast(children, acc)

      text_leaf, acc when is_binary(text_leaf) ->
        acc <> " " <> text_leaf
    end)
  end

Example:

iex(46)> md_string = "# A Header\n\n**bold**\n\n+ li 1\n+ li 2"
"# A Header\n\n**bold**\n\n+ li 1\n+ li 2"
iex(47)> Earmark.as_ast!(md_string)
[
  {"h1", [], ["A Header"], %{}},
  {"p", [], [{"strong", [], ["bold"], %{}}], %{}},
  {"ul", [], [{"li", [], ["li 1"], %{}}, {"li", [], ["li 2"], %{}}], %{}}
]
iex(48)> extract_text_from_markdown(md_string)
"A Header bold li 1 li 2"

Also Liked

kingy

kingy

That works perfectly, thanks @knoebber!

I had forgotten you could do pattern matching on anonymous functions like this, it makes reducers so much easier to read.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement