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
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"
1
Also Liked
Popular in Questions
For example for a current url like
http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2,
I would like to get:
...
New
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
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
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
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
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> somethi...
New
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
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
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
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
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
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
Hello, how can I check the Phoenix version ?
Thanks !
New
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








