crw icon indicating copy to clipboard operation
crw copied to clipboard

Markdown: code samples on MUI-rendered pages collapse to one line (docs.unity3d.com 6000.3)

Open revouwu opened this issue 3 weeks ago • 2 comments

Problem

Some pages render each code line as its own stacked <span>, with no <pre> and no newline between lines. htmd treats those spans as inline text, so the sample comes out as one line of prose with no code fence. A // comment then runs into the code that follows it.

Evidence

crw 0.43.1, Windows x64, default config:

crw scrape https://docs.unity3d.com/6000.3/Documentation/ScriptReference/Rigidbody.AddForce.html

Output under # Examples:

using UnityEngine;using UnityEngine.InputSystem;public class Example : MonoBehaviour{    Rigidbody m_Rigidbody;    public float m_Thrust = 20f;    void Start()    {        //Fetch the Rigidbody from the GameObject with this script

Source markup for each line (42 such lines in -f html output, none with nested tags):

<div class="MuiStack-root mui-1u6odbp">
<span class="MuiSkeleton-root MuiSkeleton-text MuiSkeleton-pulse MuiSkeleton-withChildren MuiSkeleton-fitContent mui-1y71r9t" style="height:18px"><span class="MuiTypography-root MuiTypography-code mui-15abx7k">using UnityEngine;</span></span>

(Line break after the div added for reading. The spans are adjacent in the page.)

With the change below, the same page goes from 142 to 184 lines and from 16 to 24 fence lines:

```
using UnityEngine;
using UnityEngine.InputSystem;

public class Example : MonoBehaviour
{
    Rigidbody m_Rigidbody;
    public float m_Thrust = 20f;

Fix

In crates/crw-extract/src/markdown.rs, rewrite a run of these spans to <pre><code> before calling htmd:

static MUI_CODE_RUN_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"(?s)(?:<span class="MuiSkeleton-root[^>]*><span class="MuiTypography-root MuiTypography-code[^"]*">.*?</span></span>)+"#).unwrap()
});
static MUI_CODE_LINE_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"(?s)<span class="MuiTypography-root MuiTypography-code[^"]*">(.*?)</span></span>"#).unwrap()
});

fn fence_mui_code_lines(html: &str) -> Cow<'_, str> {
    if !html.contains("MuiTypography-code") {
        return Cow::Borrowed(html);
    }
    MUI_CODE_RUN_RE.replace_all(html, |run: &regex::Captures<'_>| {
        let lines: Vec<&str> = MUI_CODE_LINE_RE
            .captures_iter(&run[0])
            .map(|l| l.get(1).map_or("", |m| m.as_str()))
            .collect();
        format!("<pre><code>{}</code></pre>", lines.join("\n"))
    })
}

Call it in html_to_markdown_with, just before htmd::convert:

let html = fence_mui_code_lines(&html);
let md = htmd::convert(&html).unwrap_or_default();

Tested on v0.43.1: cargo test -p crw-extract --lib markdown passes (22 tests, none new) and the page above renders as shown.

Limits: the regexes are tied to these class names, so this only covers pages with this markup, and .*?</span></span> would stop early if a code line contained nested spans (none do on this page). If you would rather not keep class names in the converter, this needs a more general rule for consecutive line-like spans. I have not tested one.

revouwu avatar Nov 09 '26 23:11 revouwu

thanks, reproduced on the unity page: each line is its own span with no newline, so the sample collapses into one line of prose. working on a fix now along the lines you sketched, with a test built from that markup. will link the PR here.

us avatar Nov 10 '26 03:11 us

fixed in #709 and live on fastcrw.com now. we went with the more general rule you mentioned instead of the class names: when a <pre> has no newline and one <div> inside it holds all of its text as a run of identical sibling elements, each sibling becomes a line of a fenced block. single-line token spans, lines with links or <br>, and any text outside the run keep the old path. the AddForce page now has the Examples sample as a multi-line fence (24 fence lines). thanks for tracking this down.

us avatar Nov 10 '26 08:11 us