jml-h
JsonML + Virtual Hypertext generator
Super light-weight 60-line package to work with JsonML
and Virtual Hypertext functions
to generate XML/HTML or virtual DOM from JsonML
.
var lib = ;var h = libh; var html = ;console;// <div class="wrapper"><div class="avatar"><img src="..."/><span>Hello there</span><br/></div></div>
Or using JsonML
:
console;// <div class="wrapper"><div class="avatar"><img src="..."/><span>Hello there</span><br/></div></div>
Reference
;;;
attr(obj: {}): string
Formats a collection of tag attributes into an HTML string.
var attributes = id: 'header' 'class': 'floating';console;// id="header" class="floating"
h(tag: string, attributes: {}, ...children: string[]): string
The most basic Virtual Hypertext function that directly generates an HTML string.
var h = libh;var html = ;console;// <div class="main"><a href="...">Click me</a></div>
traverse(jml: JsonMLNode, callback: (node: JsonMLNodeReplaced) => any): any
Traverses JsonML
object starting from leaf nodes calling callback
for every node. callback
receives a JsonML
node as a single argument.
The value returned by callback
is used to replace that node when the callback
is called for its parent node.
lib;
dom(jml: JsonMLNode, h: VHypertext): any
dom
accepts two arguments: a JsonML
tree and a Virtual Hypertext function h
, it
feeds JsomML
nodes one-by-one to the, Virtual Hypertext effectively creating a Virtual DOM
.
By default it uses the bundled h
function:
var vdom = lib;console; // <div class="wrapper"><a href="#link">Click me!</a></div>
Alternatively you can provide the Virtual Hypertext function of your framework, for example:
var jml = 'div' 'class': 'wrapper' 'a' 'href': '#link' 'Click me!]]; // React.jslib.dom(jml, React.createElement.bind(React)); // Mithril.jslib.dom(jml, m); // virtual-domvar h = require('virtual-dom/h');lib.dom(jml, h);
map(transform: (node: JsonMLNode) => JsonMLNode, h: VHypertext): VHypertext
Based on existing hypertext function h
creates a new hypertext function that applies transform
function
to every JsonML node before giving it to the original h
.
For example, replace div
tags with span
tags:
var jml = 'div' null 'Hello'; { ifnode0 === 'div' node0 = 'span'; return node;}var new_h = lib;console;
JsonML
What is JsonML
is a compact representation of XML/HTML
as JSON or JavaScript objects. Consider the following
HTML snippet:
Click 1 Click 2
In JsonML
it can be represented as follows:
'ul' 'class': 'my-list' 'li' 'a' href: '#link1' 'Click 1' 'li' 'a' href: '#link2' 'Click 2'
Basically, every node is represented by an array, where first element is a tag name, the second element is a collection of attributes and all the rest elements represent child nodes.
;
h
Virtual Hypertext Generator Virtual Hypertext generator function, frequently represented by h
and has a similar syntax to JsonML
.
It is frequently used in virtual DOM templating libraries, such as React.js
, Mithril.js
, virtual-dom
, etc.
Even if your virtual DOM templating library does not have a Virtual Hypertext function, you can create it yourself.
To generate the above HTML with h
, you would write this:
;
Virtual Hypertext function is defined as follows:
htag: string, attributes: , ...children: any: any;
Virtual Tag Functions
Create convenience function div()
, span()
, etc..
var h = libh;libtagsh h 'div' 'span';var div span = h;console;// <div class="test"><span>Hello</span></div>
Usage with React.js
The second argument to the dom()
function is a Virtual Hypertext generator function, you can provide
React.createElement.bind(React)
to it to generate React's virtual DOM.
Generate React Virtual DOM representations from JsonML
lists instead of using React.createElement
or .jsx
files and compiling them to .js
:
var react_dom = lib;
This is equivalent to:
var react_dom = React;
You might consider creating the React's Virtual Hypertext function for convenience:
Reacth = ReactcreateElement;
And then create JsonML
to virtual DOM generator:
React { return lib;};
So now, instead of installing .jsx
to .js
compiler and writing XML
in your JavaScript projects,
like so:
var CommentBox = React;
You can do everything in 100% JavaScript:
var CommentBox = React;
TypeScript definitions for your extension:
declare