a048ef1dcaf57931dbf363b0f15c23e59525aa8732d0050747dbba3531212b6889f894e1c6d64314e7fcf6fafbc916b44065b52fe72f64c51e63a6743392cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. [![npm][npm]][npm-url]
  2. [![node]]()
  3. [![deps][deps]][deps-url]
  4. [![tests][tests]][tests-url]
  5. [![coverage][cover]][cover-url]
  6. [![chat][chat]][chat-url]
  7. <div align="center">
  8. <a href="https://github.com/posthtml/posthtml">
  9. <img width="200" height="200" alt="PostHTML"
  10. src="http://posthtml.github.io/posthtml/logo.svg">
  11. </a>
  12. <h1>PostHTML Render</h1>
  13. <p>Renders a PostHTML Tree to HTML/XML</p>
  14. </div>
  15. <h2 align="center">Install</h2>
  16. ```bash
  17. npm i -D posthtml-render
  18. ```
  19. > ℹ️ This module is also available for [bower](http://bower.io) and as an AMD, CommonJS and IIFE (global) module, uncompressed and compressed
  20. <h2 align="center">Usage</h2>
  21. ### `NodeJS`
  22. ```js
  23. const render = require('posthtml-render')
  24. const tree = []
  25. const node = {}
  26. node.tag = 'ul'
  27. node.attrs = { class: 'list' }
  28. node.content = [
  29. 'one',
  30. 'two',
  31. 'three'
  32. ].map((content) => ({ tag: 'li', content }))
  33. tree.push(node)
  34. const html = render(tree, options)
  35. ```
  36. ```html
  37. <ul class="list">
  38. <li>one</li>
  39. <li>two</li>
  40. <li>three</li>
  41. </ul>
  42. ```
  43. ### `🌐 Browser`
  44. ```html
  45. <!DOCTYPE html>
  46. <html>
  47. <head>
  48. <title>Title</title>
  49. <script src="./node_modules/posthtml-render/lib/browser.min.js"></script>
  50. <script >
  51. const tree = {
  52. tag: 'h1',
  53. attrs: {
  54. style: 'color: red;'
  55. },
  56. content: [ 'Title' ]
  57. }
  58. window.onload = function () {
  59. document.body.innerHTML = render(tree)
  60. }
  61. </script>
  62. </head>
  63. <body></body>
  64. </html>
  65. ```
  66. <h2 align="center">Options</h2>
  67. |Name|Type|Default|Description|
  68. |:--:|:--:|:-----:|:----------|
  69. |**[`singleTags`](#singletags)**|`{Array<String\|RegExp>}`|`[]`|Specify custom single tags (self closing)|
  70. |**[`closingSingleTag`](#closingSingleTag)**|`{String}`|[`>`](#default)|Specify the single tag closing format|
  71. |**[`quoteAllAttributes`](#quoteAllAttributes)**|`{Boolean}`|[`true`](#default)|Put double quotes around all tags, even when not necessary.|
  72. |**[`replaceQuote`](#replaceQuote)**|`{Boolean}`|[`true`](#default)|Replaces quotes in attribute values with `&quote;`.|
  73. |**[`quoteStyle`](#quoteStyle)**|`{0|1|2}`|[`2`](#default)|Specify the style of quote arround the attribute values|
  74. ### `singleTags`
  75. Specify custom single tags (self closing)
  76. ### `{String}`
  77. ```js
  78. const render = require('posthtml-render')
  79. const tree = [ { tag: 'name' } ]
  80. const options = { singleTags: [ 'name' ] }
  81. const html = render(tree, options)
  82. ```
  83. **result.html**
  84. ```html
  85. <name>
  86. ```
  87. #### `{RegExp}`
  88. ```js
  89. const render = require('posthtml-render')
  90. const tree = [ { tag: '%=title%' } ]
  91. const options = { singleTags: [ '/^%.*%$/' ] }
  92. const html = render(tree, options)
  93. ```
  94. **result.html**
  95. ```html
  96. <%=title%>
  97. ```
  98. ### `closingSingleTag`
  99. Specify the single tag closing format
  100. #### `Formats`
  101. ```js
  102. const render = require('posthtml-render')
  103. const tree = [ { tag: 'img' } ]
  104. ```
  105. ##### `'tag'`
  106. ```js
  107. const html = render(tree, { closingSingleTag: 'tag' })
  108. ```
  109. ```html
  110. <custom></custom>
  111. ```
  112. ##### `'slash'`
  113. ```js
  114. const html = render(tree, { closingSingleTag: 'slash' })
  115. ```
  116. ```html
  117. <custom />
  118. ```
  119. ##### `'default' (Default)`
  120. ```js
  121. const html = render(tree)
  122. ```
  123. ```html
  124. <img></img>
  125. ```
  126. ### `quoteAllAttributes`
  127. Specify if all attributes should be quoted.
  128. ##### `true (Default)`
  129. ```html
  130. <i src="index.js"></i>
  131. ```
  132. ##### `false`
  133. ```html
  134. <i src=index.js></i>
  135. ```
  136. ### `replaceQuote`
  137. Replaces quotes in attribute values with `&quote;`.
  138. ##### `true (Default)`
  139. ```html
  140. <img src="<?php echo $foo[&quote;bar&quote;] ?>">
  141. ```
  142. ##### `false`
  143. ```html
  144. <img src="<?php echo $foo["bar"] ?>">
  145. ```
  146. ### `quoteStyle`
  147. ##### `2 (Default)`
  148. Attribute values are wrapped in double quotes:
  149. ```html
  150. <img src="https://example.com/example.png" onload="testFunc("test")">
  151. ```
  152. ##### `1`
  153. Attribute values are wrapped in single quote:
  154. ```html
  155. <img src='https://example.com/example.png' onload='testFunc("test")'>
  156. ```
  157. ##### `0`
  158. Quote style is based on attribute values (an alternative for `replaceQuote` option):
  159. ```html
  160. <img src="https://example.com/example.png" onload='testFunc("test")'>
  161. ```
  162. [npm]: https://img.shields.io/npm/v/posthtml-render.svg
  163. [npm-url]: https://npmjs.com/package/posthtml-render
  164. [node]: https://img.shields.io/node/v/posthtml-render.svg
  165. [node-url]: https://img.shields.io/node/v/posthtml-render.svg
  166. [deps]: https://david-dm.org/posthtml/posthtml-render.svg
  167. [deps-url]: https://david-dm.org/posthtml/posthtml-render
  168. [tests]: http://img.shields.io/travis/posthtml/posthtml-render.svg
  169. [tests-url]: https://travis-ci.org/posthtml/posthtml-render
  170. [cover]: https://coveralls.io/repos/github/posthtml/posthtml-render/badge.svg
  171. [cover-url]: https://coveralls.io/github/posthtml/posthtml-render
  172. [chat]: https://badges.gitter.im/posthtml/posthtml.svg
  173. [chat-url]: https://gitter.im/posthtml/posthtml