1.20.1 • Published 7 years ago

html-to-article-json v1.20.1

Weekly downloads
261
License
MIT
Repository
github
Last release
7 years ago

html-to-article-jsonBuild Status

html-to-article-json parses & normalizes html to a well-structured & easy to use article json format.

The parsing logic is based on real articles - with sometimes very weird html - so please open an issue if some html doesn't get parsed correctly!

Installation

npm install html-to-article-json

Usage

node.js

var htmlToArticleJson = require('html-to-article-json')();
var htmlString = '<p>Foo<b>bar</b></p>';
var articleJson = htmlToArticleJson(htmlString);

browserify

Using browseriy html-to-article-json can also use DOM as input in the browser!

var htmlToArticleJson = require('html-to-article-json')();
var domElement = document.querySelector('article');
var articleJson = htmlToArticleJson(domElement);

Format

article-json consists of a list of nodes, each node representing a block of content.

Please see the example (npm run example) for a simple WYSIWYG editor & the corresponding article json.

Text

{
  "type": "paragraph",
  "children": [{
    "type": "text",
    "content": "Hello, ",
    "href": null,
    "italic": false,
    "bold": false
  }, {
    "type": "text",
    "content": "mic.com",
    "href": "http://www.mic.com",
    "italic": true,
    "bold": false
  }]
}

The above is an example of a text node - corresponding to something like <p>Hello, <a href="mic.com"><b>mic.com</b></a>.

A text content node is defined by it's visual representation rather than it's code - so html-to-article-json will parse <a href="mic.com"><b>mic.com</b></a> and <b><a href="mic.com">mic.com</a></b> to the same json object.

Valid text nodes are paragraph, header1, header2, header3, header4, header5 & header6.

Embeds

{
  "type": "embed",
  "embedType": "youtube",
  "youtubeId": "eBYFOJxZx4Q",
  "caption": [{
    "type": "text",
    "content": "Here's a video from ",
    "href": null,
    "italic": false,
    "bold": false
  }, {
    "type": "text",
    "content": "mic.com",
    "href": "http://www.mic.com",
    "italic": true,
    "bold": false
  }]
}

The above is an example of an embed node - corresponding to a youtube embed. The caption format is the same as the children array we have in the Text example.

1.20.1

7 years ago

1.20.0

7 years ago

1.19.3

7 years ago

1.19.2

8 years ago

1.19.1

8 years ago

1.19.0

8 years ago

1.18.2

8 years ago

1.18.1

8 years ago

1.18.0

8 years ago

1.17.0

8 years ago

1.16.0

8 years ago

1.15.0

8 years ago

1.14.1

8 years ago

1.14.0

8 years ago

1.13.0

8 years ago

1.12.5

8 years ago

1.12.4

8 years ago

1.12.3

8 years ago

1.12.2

8 years ago

1.12.1

8 years ago

1.12.0

8 years ago

1.11.0

8 years ago

1.10.0

8 years ago

1.9.2

8 years ago

1.9.1

8 years ago

1.9.0

8 years ago

1.8.1

8 years ago

1.8.0

8 years ago

1.7.0

8 years ago

1.6.0

8 years ago

1.5.0

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago