Skip to content

Commit

Permalink
feat: 🎸 improve json-ml types
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 1, 2024
1 parent ef5327e commit a071f3c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/json-ml/toHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export const toHtml = (node: JsonMlNode): string => {
let childrenStr = '';
for (let i = 0; i < childrenLength; i++) childrenStr += toHtml(children[i]);
if (!tag) return childrenStr;
if (attrs) for (const key in attrs) attrStr += ' ' + key + '="' + escapeAttr(attrs[key]) + '"';
if (attrs) for (const key in attrs) attrStr += ' ' + key + '="' + escapeAttr(attrs[key] + '') + '"';
return '<' + tag + attrStr + '>' + childrenStr + '</' + tag + '>';
};
30 changes: 29 additions & 1 deletion src/json-ml/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
/**
* Represents a node in the JsonML tree. Can be a string or an element.
*/
export type JsonMlNode = string | JsonMlElement;
export type JsonMlElement = [tag: string | number, attrs: null | Record<string, unknown>, ...children: JsonMlNode[]];

/**
* Represents an element in the JsonML tree. Lke an HTML element.
*/
export type JsonMlElement = [
/**
* Tag name of the element. An empty string `''` tag represents a *fragment* -
* a list of nodes. Similar to a `DocumentFragment` in the DOM, or
* `React.Fragment` `<>` in React.
*
* When converting to HTML, an empty string tag is not rendered and numeric
* tags are converted to strings.
*/
tag: '' | string | number,

/**
* Attributes of the element. `null` if there are no attributes. Attribute
* object values are converted to strings when formatting to HTML.
*/
attrs: null | Record<string, unknown>,

/**
* Child nodes of the element. Can be a mix of strings and elements.
*/
...children: JsonMlNode[],
];

0 comments on commit a071f3c

Please sign in to comment.