GithubHelp home page GithubHelp logo

sukkaw / posthtml-render Goto Github PK

View Code? Open in Web Editor NEW

This project forked from posthtml/posthtml-render

0.0 1.0 0.0 442 KB

Render PostHTMLTree to HTML/XML

License: Other

JavaScript 86.82% HTML 13.18%

posthtml-render's Introduction

npm node deps tests coverage chat

PostHTML

PostHTML Render

Renders a PostHTML Tree to HTML/XML

Install

npm i -D posthtml-render

โ„น๏ธ This module is also available for bower and as an AMD, CommonJS and IIFE (global) module, uncompressed and compressed

Usage

NodeJS

const render = require('posthtml-render')

const tree = []

const node = {}

node.tag = 'ul'
node.attrs = { class: 'list' }
node.content = [
 'one',
 'two',
 'three'
].map((content) => ({ tag: 'li', content }))

tree.push(node)

const html = render(tree, options)
<ul class="list">
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

๐ŸŒ Browser

<!DOCTYPE html>
<html>
<head>
  <title>Title</title>
  <script src="./node_modules/posthtml-render/lib/browser.min.js"></script>
  <script >
    const tree = {
      tag: 'h1',
      attrs: {
        style: 'color: red;'
      },
      content: [ 'Title' ]
    }

    window.onload = function () {
      document.body.innerHTML = render(tree)
    }
  </script>
</head>
<body></body>
</html>

Options

Name Type Default Description
singleTags {Array<String|RegExp>} [] Specify custom single tags (self closing)
closingSingleTag {String} > Specify the single tag closing format
quoteAllAttributes {Boolean} true Put double quotes around all tags, even when not necessary.
replaceQuote {Boolean} true Replaces quotes in attribute values with &quote;.

singleTags

Specify custom single tags (self closing)

{String}

const render = require('posthtml-render')

const tree = [ { tag: 'name' } ]
const options = { singleTags: [ 'name' ] }

const html = render(tree, options)

result.html

<name>

{RegExp}

const render = require('posthtml-render')

const tree = [ { tag: '%=title%' } ]
const options = { singleTags: [ '/^%.*%$/' ] }

const html = render(tree, options)

result.html

<%=title%>

closingSingleTag

Specify the single tag closing format

Formats

const render = require('posthtml-render')

const tree = [ { tag: 'img' } ]
'tag'
const html = render(tree, { closingSingleTag: 'tag' })
<custom></custom>
'slash'
const html = render(tree, { closingSingleTag: 'slash' })
<custom />
'default' (Default)
const html = render(tree)
<img></img>

quoteAllAttributes

Specify if all attributes should be quoted.

true (Default)
<i src="index.js"></i>
false
<i src=index.js></i>

replaceQuote

Replaces quotes in attribute values with &quote;.

true (Default)
<img src="<?php echo $foo[&quote;bar&quote;] ?>">
false
<img src="<?php echo $foo["bar"] ?>">

posthtml-render's People

Contributors

scrum avatar voischev avatar michael-ciniawsky avatar awinogradov avatar hojas avatar kevincox avatar guria avatar anikethsaha avatar 0ctothorp avatar cvrajeesh avatar dependabot[bot] avatar island205 avatar maciej-ka avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.