marschro
Hello,
so I started to integrate markdown editing in my app and I ended up integrating earmark, which works fine for me.
Currently I have a utility function that I can call everywhere in my LiveViews in order to generate html from markdown.
Here how his looks ( I reuse styles, defined in my CoreComponents):
@doc """
parse markdown to styled html
"""
def markdown(nil), do: nil
def markdown(content) do
case Earmark.Parser.as_ast(content) do
{:ok, ast, _warnings} ->
ast
|> parse_ast()
|> Earmark.Transform.transform()
{:error, _ast, warnings} ->
{:warning, _id, msg} = List.first(warnings)
"Invalid markup: #{msg}"
end
end
defp parse_ast(ast) do
ast
|> Earmark.Transform.map_ast(fn node -> parse_node(node) end, ignore_strings: true)
|> List.flatten()
end
defp parse_node({_tag, _attrs, _children_or_content, _meta} = node) do
add = fn class_list ->
fn node -> Earmark.AstTools.merge_atts_in_node(node, class: Enum.join(class_list, " ")) end
end
processors = [
{"h1", add.(PortalWeb.CoreComponents.typography(:h1))},
{"h2", add.(PortalWeb.CoreComponents.typography(:h2))},
{"h3", add.(PortalWeb.CoreComponents.typography(:h3))},
{"h4", add.(PortalWeb.CoreComponents.typography(:h4))},
{"p", add.(PortalWeb.CoreComponents.typography(:p))},
{"ul", add.(PortalWeb.CoreComponents.typography(:ul))},
{"ol", add.(PortalWeb.CoreComponents.typography(:ol))},
{"li", add.(PortalWeb.CoreComponents.typography(:li))},
{"a", add.(PortalWeb.CoreComponents.typography(:a))},
{"strong", add.(PortalWeb.CoreComponents.typography(:strong))},
{"em", add.(PortalWeb.CoreComponents.typography(:em))},
{"u", add.(PortalWeb.CoreComponents.typography(:u))},
{"img", add.(PortalWeb.CoreComponents.typography(:img))},
{"blockquote", add.(PortalWeb.CoreComponents.typography(:blockquote))},
{"hr", add.(PortalWeb.CoreComponents.typography(:hr))},
{"code", add.(PortalWeb.CoreComponents.typography(:code))},
{"pre", add.(PortalWeb.CoreComponents.typography(:pre))}
]
postprocessor =
Earmark.Options.make_options!(registered_processors: processors)
|> Earmark.Transform.make_postprocessor()
postprocessor.(node)
end
No I have two questions:
- I ran into the thing, that earmark_parser was extracted from earmark. So if I only use earmark_parser - how do I generate html markup from the generated ast ? With floki? How is that done.
- Also I struggle with styling nested elements like lists in list. Has anyone a good example how that can be achieved? Currently I only can add classes to every
- element but cannot differntiate if this is a list of 1st or second level… (don’t want to do that via css but with tailwind classes…)
Any advice appreciated ![]()
… also how is such a cool editor done like this one I am typing in…
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
RobertDober
It is back in Earmark so you should not have any problem. No it is not floki but homemade.
Transform.transformI am no frontend guy by any means, therefore I do not know what exactly is needed, but seems you are doing fine above, no?
As I am not maintaining Earmark I cannot change the code which might be a little bit too complicated but basically use
Of course you can using the postprocessor as above if it is enough
HTH
marschro
Thank you very much for your reply!
well basically, when a generated list with a list within looks like that:
But what I was not able to figure out, is, how can I be aware during Transformation (when preprocessors are added) if the list-item is of level 1 or 2…
… probably I can’t… ?
RobertDober
There is no built in way to do this easily as far as I remember, the idea is that you need to transform the ast yourself, here is a draft of how this should work