HTML to Mithril View Converter
Convert raw HTML markup into Mithril.js m() hyperscript view functions instantly. Supports attributes, classes, styles, and postRender config.
About
Mithril.js uses a hyperscript function m(tag, attrs, children) to construct virtual DOM nodes. Writing complex nested views by hand from existing HTML is tedious and error-prone. A misplaced bracket or forgotten attribute conversion breaks rendering silently. This tool parses raw HTML into a proper DOM tree, then recursively generates syntactically correct Mithril m() calls with CSS selector shorthand for classes and IDs, object-literal style attributes, and correct nesting. It handles self-closing elements, boolean attributes, and optional postRender config hooks. The output is copy-paste ready for your view function. Note: this tool approximates conversion assuming well-formed HTML input. Malformed or partial fragments may produce unexpected nesting.
Formulas
The conversion follows a recursive tree-walk algorithm. Given a DOM node N, the output O is produced by:
The selector is constructed as: tag + ("#" + id) + ("." + class1 + "." + class2 + …). The attrs object contains all remaining attributes after id and class have been consumed into the selector. The style attribute string is parsed by splitting on ";" then on ":" and converting property names from kebab-case to camelCase. children is built by mapping convert(Nchild) over all child nodes, filtering out NULL results. If only one child exists and it is a text string, it is passed directly instead of as an array.
Reference Data
| HTML Feature | Mithril Output | Notes |
|---|---|---|
| Element with class | m('div.my-class') | CSS selector shorthand |
| Element with ID | m('div#my-id') | ID appended to tag |
| Element with both | m('div#id.cls') | ID before classes |
| Multiple classes | m('div.a.b.c') | Dot-separated |
| Inline style | { style: { color: "red" } } | Converted to JS object |
| Boolean attribute (checked) | { checked: true } | No value needed |
| data-* attributes | { "data-id": "5" } | Preserved as strings |
| Self-closing tags (br, hr, img) | m('br') | No children array |
| Text nodes | "Hello world" | String literal child |
| Mixed children | [m('span'), ' text'] | Array of mixed types |
| Nested elements | m('ul', [m('li')]) | Recursive nesting |
| Comments | Stripped | HTML comments are removed |
| Empty attributes (disabled) | { disabled: true } | Treated as boolean |
| onclick / events | { onclick: ctrl.handler } | Manual wiring required |
| config (postRender) | { config: fn } | Only on wrapper element |
| SVG elements | m('svg', ...) | Namespace preserved |
| Input elements | m('input[type=text]') | Type in selector or attrs |
| Textarea with content | m('textarea', 'text') | Content as child string |
| Whitespace-only text | Trimmed / ignored | Prevents empty strings |
Frequently Asked Questions
{ style: { backgroundColor: "red", fontSize: "14px" } }.{ disabled: true }. This matches Mithril's expectation for toggling boolean DOM properties.