Vague output when converting AST to Markdown

Using markdown 1.0.0-alpha.22, I'm mutating my AST like this:

fn fix_asdoc_markdown_node(node: &mut markdown::mdast::Node, docs_path: Option<&FlexPath>) {
    if let markdown::mdast::Node::Code(code) = node {
        if code.lang.is_none() {
            code.lang = Some("as3".to_owned());
        }
    } else if let markdown::mdast::Node::Image(img) = node {
        if let Some(docs_path) = docs_path {
            if !lazy_regex::regex_is_match!(r"^(a-zA-Z0-9_\-)+:", img.url.as_str()) {
                let mut r = docs_path.resolve(&img.url).to_string();
                r = lazy_regex::regex_replace!(r"^\\\\\?\\UNC", r.as_str(), |_| r"\".to_owned()).into_owned();
                r = lazy_regex::regex_replace!(r"^\\\\\?\\", r.as_str(), |_| r"".to_owned()).into_owned();
                img.url = tower_lsp::lsp_types::Url::from_file_path(&r).map(|uri| uri.to_string()).unwrap_or("".to_owned());
            }
        }
    } else if let markdown::mdast::Node::Definition(defn) = node {
        if let Some(docs_path) = docs_path {
            if !lazy_regex::regex_is_match!(r"^(a-zA-Z0-9_\-)+:", defn.url.as_str()) {
                let mut r = docs_path.resolve(&defn.url).to_string();
                r = lazy_regex::regex_replace!(r"^\\\\\?\\UNC", r.as_str(), |_| r"\".to_owned()).into_owned();
                r = lazy_regex::regex_replace!(r"^\\\\\?\\", r.as_str(), |_| r"".to_owned()).into_owned();
                defn.url = tower_lsp::lsp_types::Url::from_file_path(&r).map(|uri| uri.to_string()).unwrap_or("".to_owned());
            }
        }
    }

    if let Some(children) = node.children_mut() {
        for child in children {
            fix_asdoc_markdown_node(child, docs_path);
        }
    }
}

I'm parsing and converting to Markdown like this:

/// Fixes code block highlighting language and image links in Markdown content.
fn fix_asdoc_markdown(content: &str, location: &whackengine_verifier::ns::Location) -> String {
    let cu = location.compilation_unit();
    if let Some(file_path) = cu.file_path() {
        let asdoc_configuration = AsdocConfiguration::find(&file_path);
        if let Ok(Some((project_directory, asdoc_configuration))) = asdoc_configuration {
            let docs_path = project_directory.resolve(&asdoc_configuration.root_path);
            let mut node = markdown::to_mdast(content, &markdown::ParseOptions {
                ..default()
            }).unwrap();
            fix_asdoc_markdown_node(&mut node, Some(&docs_path));
            return node.to_string();
        }
    }
    let mut node = markdown::to_mdast(content, &markdown::ParseOptions {
        ..default()
    }).unwrap();
    fix_asdoc_markdown_node(&mut node, None);
    node.to_string()
}

I'm getting wrong output. Images don't show up, and code blocks aren't generated as well.

Given:

The A class.

![hi](hi.png)

```
trace(x);
```

I'm getting:

The A class.trace(x);

Can you create a reproducible repo? I can try to help later today.

1 Like

NVM, I asked in their repository and got an answer explaining that I've to use the mdast_util_to_markdown crate to generate Markdown rather than ToString (which worked fine).