Stream: beginners

Topic: HTML parser in Roc


view this post on Zulip Steve Howell (Mar 15 2026 at 16:38):

I'm looking for an HTML parser in Roc to use with the old Rust compiler. What's easy:

view this post on Zulip Steve Howell (Mar 15 2026 at 16:39):

I will actually be using it to parse Zulip messages. Here is the TS code that I need to port (so ideally the Roc parser would support the handy querySelectorAll interface, but I can walk any AST):

import { parse, HTMLElement } from "node-html-parser";

function fix_src(root: HTMLElement, tag_name: string) {
    const nodes = root.querySelectorAll(tag_name);

    nodes.forEach((node) => {
        const src = node.getAttribute("src");

        if (src) {
            node.setAttribute("data-src", src);
            node.removeAttribute("src");
            node.classList.add("lazyload");
        }
    });
}

export function fix_content(content: string): string {
    const root = parse(content);

    fix_src(root, "img");
    fix_src(root, "video");

    return root.toString();
}

view this post on Zulip Steve Howell (Mar 15 2026 at 16:44):

Suggestions welcome!

view this post on Zulip Luke Boswell (Mar 15 2026 at 20:13):

Here's one https://github.com/lukewilliamboswell/roc-parser

view this post on Zulip Luke Boswell (Mar 15 2026 at 20:13):

Hasn't migrated to the new compiler yet :sweat_smile:

view this post on Zulip Luke Boswell (Mar 15 2026 at 20:14):

Though you wouldn't use it like the TS code

view this post on Zulip Steve Howell (Mar 18 2026 at 12:09):

That's nice code! Unfortunately, I can't really program in Roc until I find a stable version of the compiler. (see #beginners > panic when just adding a new Dict)


Last updated: Mar 20 2026 at 12:28 UTC