A javascript object notation (JSON) convention for representing HTML markup
npm install @lobodeguerra/hrjsonHTML Representation Javascript Object Notation is a convention of representing HTML on a JSON string.
In order to better store complex HTML structures, there is a need for an identical representation of thus structures, on a JSON format, that can separate the layout representation from the way the HTML structure got built.
Focus your UI on manipulating the Document HTML, then use javascript to get the HTML Object, transform it to HRJSON and save it to your DB. Retrieve the JSON, parse it and render it through javascript.
Yes. It supports inline styling.
HTML is a powerful language to layout complex structures, although not the most efficient in terms of traversing it or parsing it. When saving HTML to a database, escaping such data tends to break layouts, and give developers awful regex parsing challenges. Migrations are another point of impact, in which rich content migrated from one site to another break because of problems at escaping data to save it on the database.
Even something as simple as content created with a rich text editor like tinymce, can get distorted in the way of getting stored, retrieved, parsed and displayed. Building a rich editor on top of HTML5 is a great idea, since HTML5 is so powerful to create rich content; so I came up with a solution as simple as making a JSON representation of the rich contents, that are gracefully created through DOM manipulation and HTML tags, to avoid escaping problems.
The problem has never been using HTML to create rich content, but rather using the wrong text representation to store it on the database. That being said, it is obvious that there's need for a better way to represent and store complex structures, that is identical to HTML, but designed for a different purpose: representation and storing, instead of representation and browser interpretation.
HTML builds layouts by nesting tags in an XML based syntax. Each tag consists of:
1. Name
2. Attributes
3. Children
An identical representation can be done with a JSON object. That is HRJSON, a standarized HTML representation on JSON.
The simplest tag with the simplest representation, for example a , only a tag name, with no attributes and no children, can be represented with this HRJSON string:
{"tag":"span","atts":{},"children":[]}
Complex blocks can be easily represented by replicating this atomic structure, like so:
`` This is an example page. It’s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this: Hi there! I’m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin’ caught in the rain.) …or something like this: The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.`
Would be represent with a HRJSON string like this:
`
{
"tag":"div",
"atts":{
},
"children":[
{
"tag":"p",
"atts":{
},
"children":"This is an example page. It’s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:"
},
{
"tag":"blockquote",
"atts":{
"class":"wp-block-quote"
},
"children":[
{
"tag":"p",
"atts":{
},
"children":"Hi there! I’m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin’ caught in the rain.)"
}
]
},
{
"tag":"p",
"atts":{
},
"children":"…or something like this:"
},
{
"tag":"blockquote",
"atts":{
"class":"wp-block-quote"
},
"children":[
{
"tag":"p",
"atts":{
},
"children":"The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community."
}
]
},
{
"tag":"p",
"atts":{
},
"children":[
{
"tag":"a",
"atts":{
"href":"https://cms.lobodeguerra.test/wp-admin/"
},
"children":"your dashboard"
}
]
}
]
}
`
##
import { HRJSON } from '@lobodeguerra/hrjson';
const htmlString = This is an example page. It’s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
Hi there! I’m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin’ caught in the rain.)
…or something like this:
The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.
;
const htmlObject = HRJSON.HTMLString.toHTMLObject(htmlString);
`
#### HRJSONString
###### toHRJSONObject
Transforms a HRJSON String into a HRJSON Object.
`
const hrjsonString = },
"children":[
{
"tag":"div",
"atts":{
},
"children":[
{
"tag":"p",
"atts":{
"style":"margin-bottom: 20px; background-color: #FFF; background-image: url(https://placehold.it/800x600);"
},
"children":"This is an example page. It’s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:"
},
{
"tag":"blockquote",
"atts":{
"class":"wp-block-quote"
},
"children":[
{
"tag":"p",
"atts":{
},
"children":"Hi there! I’m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin’ caught in the rain.)"
}
]
},
{
"tag":"p",
"atts":{
},
"children":"…or something like this:"
},
{
"tag":"blockquote",
"atts":{
"class":"wp-block-quote"
},
"children":[
{
"tag":"p",
"atts":{
},
"children":"The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community."
}
]
},
{
"tag":"p",
"atts":{
},
"children":[
{
"tag":"a",
"atts":{
"href":"https://cms.lobodeguerra.test/wp-admin/"
},
"children":"your dashboard"
}
]
}
]
}
]
};`
const hrjsonObject = HRJSON.HRJSONString.toHRJSONObject(hrjsonString);`
#### HTMLObject
###### toHTMLString
Transforms a HTML Object into a HTML String`
const element = document.getElementById('element');
const htmlString = HRJSON.HTMLObject.toHTMLString(element);`
###### toHRJSONObject
Transforms a HTML Object into a HRJSON Object`
const element = document.getElementById('element');
const hrjsonObject = HRJSON.HTMLObject.toHRJSON(element);
#### HRJSONObject
###### toHRJSONString
Transforms a HRJSON Object into a HRJSON String
``
const hrjsonString = HRJSON.HRJSONObject.toHRJSONString(hrjsonObject);`
###### toHTMLObject
Transforms a HRJSON Object into a HTML Object`
const htmlObject = HRJSON.HRJONSObject.toHTMLObject(hrjsonObject);Installation
Install the module as a dependency on your project by running
npm i --save-dev @lobodeguerra/hrjson
Include the HRJSON constant at the top of your JS file with:
import { HRJSON } from '@lobodeguerra/hrjson';
`
import { HRJSON } from '@lobodeguerra/hrjson';
const htmlString =
This is an example page. It’s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
Hi there! I’m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin’ caught in the rain.)
…or something like this:
The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.
;const example = document.getElementById('example');
if (example) {
const HRJSONObjectTest = HRJSON.HTMLObject.toHRJSONObject(HRJSON.HTMLString.toHTMLObject(htmlString));
example.innerHTML = HRJSON.HTMLObject.toHTMLString(HRJSON.HRJSONObject.toHTMLObject(HRJSONObjectTest));
}
``