Skip to content

Add support for markdown preview #29

Open

Description

@daveberrys

What would you like?

I was bummed that Markdown Preview hasn't been implemented yet. So I made my own with pulldown_cmark! The video shown here is made with it and currently made for my app.

2026-11-11_12-24-32.mp4

Here's a summary on how it works:

It first calls markdown_to_elements and pass in the markdown string and parse it with Parser::new_ext. It reads reads through every line and check with Event::Start if it's a special element like h1, h2, etc and push it to stack. Other elements such as Text <p>, code <code>, soft " " and hard <br/> break, rule <hr/>, etc gets checked too so nothing gets left out.

Consider it to be like a blenderui::markdown and then they can call it by markdown(&value) and they wouldn't need to write any code from it. Just a simple function that return's blenderui's elements.

The code I provided below has all the markdown elements that exists and nothing was left out. To note, this is written by my agent, but it works flawlessly and only takes about 6mb(?) of ram. So we're not losing anything from the code.

The biggest constraints I hit when making this.

Since rsx! was only on compile time, it was frustrating at first because I didn't know how to do it since the signals are dynamic instead of static(?). So, I told my agent to write every single markdown element with the runtime builder API such as; .p(), .h1(), .b(), etc...

What would you use it for?

A markdown previewer for changelogs, descriptions, or etc would be helpful for other developers too.

Example code

use blenderui::prelude::*;
use pulldown_cmark::{Event, HeadingLevel, Options, Parser, Tag};

// one pulldown-cmark tag -> the blenderui element it stands for.
fn element_for(tag: &Tag) -> Div {
    match tag {
        Tag::Paragraph => p(),
        Tag::Heading { level, .. } => match level {
            HeadingLevel::H1 => h1(),
            HeadingLevel::H2 => h2(),
            HeadingLevel::H3 => h3(),
            HeadingLevel::H4 => h4(),
            HeadingLevel::H5 => h5(),
            HeadingLevel::H6 => h6(),
        },
        Tag::BlockQuote(_) => blockquote(),
        Tag::CodeBlock(_) => pre(),
        Tag::List(Some(_)) => ol(),
        Tag::List(None) => ul(),
        Tag::Item => li(),
        Tag::FootnoteDefinition(_) => div(),
        Tag::DefinitionList => dl(),
        Tag::DefinitionListTitle => dt(),
        Tag::DefinitionListDefinition => dd(),
        Tag::Table(_) => table(),
        Tag::TableHead => thead(),
        Tag::TableRow => tr(),
        Tag::TableCell => td(),
        Tag::Emphasis => em(),
        Tag::Strong => strong(),
        Tag::Strikethrough => s(),
        Tag::Superscript => sup(),
        Tag::Subscript => sub(),
        Tag::Link { dest_url, .. } => a().attr("href", dest_url.to_string()),
        Tag::Image { dest_url, .. } => img().attr("src", dest_url.to_string()),
        Tag::HtmlBlock | Tag::MetadataBlock(_) => div(),
    }
}

// put a finished element (or text) into whatever is open, else the roots.
fn attach(roots: &mut Vec<AnyElement>, stack: &mut Vec<Div>, child: impl IntoElement) {
    let child = child.into_any_element();
    match stack.last_mut() {
        Some(parent) => parent.children_mut().push(child),
        None => roots.push(child),
    }
}

// walk the markdown events and build real blenderui elements:
// `Start` opens a tag, `End` closes it, text lands in whatever is open.
fn markdown_to_elements(markdown: &str) -> Vec<AnyElement> {
    let options =
        Options::ENABLE_TABLES
        | Options::ENABLE_STRIKETHROUGH
        | Options::ENABLE_FOOTNOTES
        | Options::ENABLE_TASKLISTS
        | Options::ENABLE_MATH
        | Options::ENABLE_DEFINITION_LIST;
    let mut roots: Vec<AnyElement> = Vec::new();
    let mut stack: Vec<Div> = Vec::new();

    for event in Parser::new_ext(markdown, options) {
        match event {
            Event::Start(tag) => stack.push(element_for(&tag)),
            Event::End(_) => {
                if let Some(done) = stack.pop() {
                    attach(&mut roots, &mut stack, done);
                }
            }
            Event::Text(text) => attach(&mut roots, &mut stack, text.to_string()),
            Event::Code(code) => attach(&mut roots, &mut stack, blenderui::html::code().child(code.to_string())),
            Event::SoftBreak => attach(&mut roots, &mut stack, " "),
            Event::HardBreak => attach(&mut roots, &mut stack, br()),
            Event::Rule => attach(&mut roots, &mut stack, hr()),
            Event::FootnoteReference(label) => attach(&mut roots, &mut stack, format!("[{label}]")),
            Event::TaskListMarker(checked) => {
                let marker = if checked { "[x] " } else { "[ ] " };
                attach(&mut roots, &mut stack, marker);
            }
            // blenderui draws no HTML, so raw HTML events are dropped
            Event::Html(_) | Event::InlineHtml(_) => {}
            // math has no renderer here: show the raw TeX, monospace
            Event::InlineMath(tex) | Event::DisplayMath(tex) => {
                attach(&mut roots, &mut stack, blenderui::html::code().child(tex.to_string()))
            }
        }
    }
    // defensive flush for anything the parser left open
    while let Some(done) = stack.pop() {
        attach(&mut roots, &mut stack, done);
    }
    roots
}

#[component]
pub fn MarkdownPreviewer(value: String) {
    let elements = markdown_to_elements(&value);
    rsx! {
        <div class="markdown_previewer">
            {...elements}
        </div>
    }
}

Activity

  1. added
    enhancement
    on Nov 11, 2026
  2. self-assigned this
    on Nov 11, 2026
  3. ppmpreetham commented on Nov 11, 2026

    @ppmpreetham
    Owner

    Thank you, we're planning to add this as an addon soon.

    Thanks for your efforts :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions