1.0.2 • Published 2 years ago

dense-parser v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

DENSE JSON-to-HTML PARSER

This nodejs library parses json objects to html directly, with emmet abbreviation built in, helps increasing coding speed, reduce time messing with html

Installation

NodeJS, Deno

Just simply install the library using npm, yarn or pnpm

npm install dense-parser
yarn add dense-parser
pnpm i dense-parser

then

const dense_parser = require("dense-parser") // Of course, you can use methods directly for short

Browser

Add this to your html source

<script language="javascript" type="text/javascript" src="https://github.com/shadichy/dense-parser/raw/master/dist/parser.umd.js"></script>

or

<script language="javascript" type="text/javascript" src="https://github.com/shadichy/dense-parser/raw/master/dist/parser.iife.js"></script>

Usage

To parse an object that represents a document, just pass it to dense-parser's parse function

const htmlObject = {
    title: "This is title",
    logo: "/path/to/your/favicon.png",
    stylesheet: [
        "/path/to/style1.css", 
        "/path/to/style2.css" , 
        "/path/to/style3.css"
    ],
    script: [
        "/path/to/script1.js", 
        "/path/to/script2.js"
    ],
    _: {
        _: "This is a div inside body!"
    }
}

console.log(dense_parser.parse(htmlObject)) 

Output:

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"/><meta http-equiv="X-UA-Compatible"content="IE=edge"/><meta name="viewport"content="width=device-width,initial-scale=1.0"/><title>This is title</title><link rel="shortcut icon"href="/path/to/your/favicon.png"type="image/png"/><meta property="og:title"content="This is title"/><meta property="og:type"content="website"/><link rel="stylesheet"href="/path/to/style1.css"><link rel="stylesheet"href="/path/to/style2.css"><link rel="stylesheet"href="/path/to/style3.css"><script language="javascript"type="text/javascript"src="/path/to/script1.js"></script><script language="javascript"type="text/javascript"src="/path/to/script2.js"></script></head><body><div>This is a div inside body!</div></body></html>

(You'll need to "beautify" it, for sure :))

To parse an object that represents a single html element, pass it to parseElement function

const elementObject = {
    tag: "span",
    id: "someRandomTag",
    class: ["dense"],
    style: {
        display: "block",
        "border-radius": "10px",
        width: "100%"
    },
    content: "div>p{This is the span content}",
    title: "get this when your hover"
}

console.log(dense_parser.parseElement(elementObject))

Output:

'<span tag="span"id="someRandomTag"style="display:block;border-radius:10px;width:100%"title="get this when your hover"class="dense"><div><p>This is the span content</p></div></span>'

(A html beautify library is recommended :))

Syntax

Element snippets

PropertyTypeDesciption
tagStringHTML tag
classString, ArrayElement classes
styleString, ObjectElement CSS style
-, contentString, Array, ObjectElement inner content, can be single element or array of children
#StringComment

And all other HTML element attributes inherited

Document snippets

Inherited from Element syntax with some additions

PropertyTypeDescription
titleStringPage title
descStringPage desciption
logoStringURL to favicon path
previewStringURL to preview picture (for social network)
typeStringPage type, can be website or article
langStringPage language
keywordString, ArrayKeywords for SEO
stylesheetString, Array, ObjectDefine document stylesheet (Object) or link to external CSS paths
scriptString, Array, ObjectCreate <script> tag that contains JavaScript code (String) or link to external JavaScript paths ()

For Dense-eco users

Execute useDense() after import

const { parse, parseElement, useDense } = require("dense-parser")

useDense()

... // Do whatever