posthtml-render
Advanced tools
Comparing version 2.0.6 to 3.0.0
@@ -0,3 +1,17 @@ | ||
## 3.0.0 (2021-07-27) | ||
* style: after lint ([d3d9d8b](https://github.com/posthtml/posthtml-render/commit/d3d9d8b)) | ||
* style: indent ([3d1fe36](https://github.com/posthtml/posthtml-render/commit/3d1fe36)) | ||
* build: update dep dev ([332eef4](https://github.com/posthtml/posthtml-render/commit/332eef4)) | ||
* docs: update usage ([d80591f](https://github.com/posthtml/posthtml-render/commit/d80591f)) | ||
* refactor: export as not default render, close #63 ([f45d8ed](https://github.com/posthtml/posthtml-render/commit/f45d8ed)), closes [#63](https://github.com/posthtml/posthtml-render/issues/63) | ||
* perf: move types to index ([7431c0f](https://github.com/posthtml/posthtml-render/commit/7431c0f)) | ||
* perf: remove trash ([fa85549](https://github.com/posthtml/posthtml-render/commit/fa85549)) | ||
* fix: referenced incorrectly in package.json close #62 ([d0bd697](https://github.com/posthtml/posthtml-render/commit/d0bd697)), closes [#62](https://github.com/posthtml/posthtml-render/issues/62) | ||
## <small>2.0.6 (2021-06-11)</small> | ||
* 2.0.6 ([aa66af1](https://github.com/posthtml/posthtml-render/commit/aa66af1)) | ||
* style: ignore ts error ([aaab81d](https://github.com/posthtml/posthtml-render/commit/aaab81d)) | ||
@@ -4,0 +18,0 @@ * fix: incorrect detect json format ([c334ef2](https://github.com/posthtml/posthtml-render/commit/c334ef2)) |
import { NodeText, NodeTag } from 'posthtml-parser'; | ||
enum closingSingleTagOptionEnum { | ||
tag = 'tag', | ||
slash = 'slash', | ||
default = 'default', | ||
closeAs = 'closeAs' | ||
declare enum quoteStyleEnum { | ||
Smart = 0, | ||
Single = 1, | ||
Double = 2 | ||
} | ||
enum closingSingleTagTypeEnum { | ||
tag = 'tag', | ||
slash = 'slash', | ||
default = 'default' | ||
declare enum closingSingleTagOptionEnum { | ||
tag = "tag", | ||
slash = "slash", | ||
default = "default", | ||
closeAs = "closeAs" | ||
} | ||
enum quoteStyleEnum { | ||
Smart, | ||
Single, | ||
Double | ||
declare enum closingSingleTagTypeEnum { | ||
tag = "tag", | ||
slash = "slash", | ||
default = "default" | ||
} | ||
type Options = { | ||
/** | ||
* Custom single tags (selfClosing). | ||
* | ||
* @default [] | ||
*/ | ||
singleTags?: Array<string | RegExp>; | ||
/** | ||
* Closing format for single tag. | ||
* | ||
* Formats: | ||
* | ||
* tag: `<br></br>`, slash: `<br />`, default: `<br>` | ||
* | ||
*/ | ||
closingSingleTag?: closingSingleTagOptionEnum; | ||
/** | ||
* If all attributes should be quoted. | ||
* Otherwise attributes will be unquoted when allowed. | ||
* | ||
* @default true | ||
*/ | ||
quoteAllAttributes?: boolean; | ||
/** | ||
* Replaces quotes in attribute values with `"e;`. | ||
* | ||
* @default true | ||
*/ | ||
replaceQuote?: boolean; | ||
/** | ||
* Quote style | ||
* | ||
* 0 - Smart quotes | ||
* <img src="https://example.com/example.png" onload='testFunc("test")'> | ||
* 1 - Single quotes | ||
* <img src='https://example.com/example.png' onload='testFunc("test")'> | ||
* 2 - double quotes | ||
* <img src="https://example.com/example.png" onload="testFunc("test")"> | ||
* | ||
* @default 2 | ||
*/ | ||
quoteStyle?: quoteStyleEnum; | ||
}; | ||
declare type Node = NodeText | NodeTag & { | ||
closeAs?: closingSingleTagTypeEnum; | ||
}; | ||
declare type Options = { | ||
/** | ||
* Custom single tags (selfClosing). | ||
* | ||
* @default [] | ||
*/ | ||
singleTags?: Array<string | RegExp>; | ||
/** | ||
* Closing format for single tag. | ||
* | ||
* Formats: | ||
* | ||
* tag: `<br></br>`, slash: `<br />`, default: `<br>` | ||
* | ||
*/ | ||
closingSingleTag?: closingSingleTagOptionEnum; | ||
/** | ||
* If all attributes should be quoted. | ||
* Otherwise attributes will be unquoted when allowed. | ||
* | ||
* @default true | ||
*/ | ||
quoteAllAttributes?: boolean; | ||
/** | ||
* Replaces quotes in attribute values with `"e;`. | ||
* | ||
* @default true | ||
*/ | ||
replaceQuote?: boolean; | ||
/** | ||
* Quote style | ||
* | ||
* 0 - Smart quotes | ||
* <img src="https://example.com/example.png" onload='testFunc("test")'> | ||
* 1 - Single quotes | ||
* <img src='https://example.com/example.png' onload='testFunc("test")'> | ||
* 2 - double quotes | ||
* <img src="https://example.com/example.png" onload="testFunc("test")"> | ||
* | ||
* @default 2 | ||
*/ | ||
quoteStyle?: quoteStyleEnum; | ||
}; | ||
declare function render(tree?: Node | Node[], options?: Options): string; | ||
export default render; | ||
export { Node, Options, closingSingleTagOptionEnum, closingSingleTagTypeEnum, quoteStyleEnum, render }; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var D=Object.defineProperty;var I=Object.prototype.hasOwnProperty;var x=Object.getOwnPropertySymbols,U=Object.prototype.propertyIsEnumerable;var N=(n,r,f)=>r in n?D(n,r,{enumerable:!0,configurable:!0,writable:!0,value:f}):n[r]=f,A=(n,r)=>{for(var f in r||(r={}))I.call(r,f)&&N(n,f,r[f]);if(x)for(var f of x(r))U.call(r,f)&&N(n,f,r[f]);return n};var _isjson = require('is-json'); var _isjson2 = _interopRequireDefault(_isjson);var u;(function(l){l.tag="tag",l.slash="slash",l.default="default",l.closeAs="closeAs"})(u||(u={}));var i;(function(s){s.tag="tag",s.slash="slash",s.default="default"})(i||(i={}));var b;(function(s){s[s.Smart=0]="Smart",s[s.Single=1]="Single",s[s.Double=2]="Double"})(b||(b={}));var y=["area","base","br","col","command","embed","hr","img","input","keygen","link","menuitem","meta","param","source","track","wbr"],j=/[\t\n\f\r "'`=<>]/,v={closingSingleTag:void 0,quoteAllAttributes:!0,replaceQuote:!0,quoteStyle:b.Double};function G(n,r={}){var h;let f=y;r.singleTags&&(f=[...new Set([...y,...r.singleTags])]),r=A(A(A({},v),r),{singleTags:f});let{singleTags:s,closingSingleTag:l,quoteAllAttributes:m,replaceQuote:R,quoteStyle:$}=r,g=(h=s==null?void 0:s.filter(a=>a instanceof RegExp))!=null?h:[];return Array.isArray(n)||(n||(n=""),n=[n]),d(n);function d(a){let t="";for(let e of a){if(e===!1||e===void 0||e===null||typeof e=="string"&&e.length===0||Number.isNaN(e))continue;if(Array.isArray(e)){if(e.length===0)continue;t+=d(e);continue}if(typeof e=="string"||typeof e=="number"){t+=e;continue}if(Array.isArray(e.content)||(e.content||(e.content=""),e.content=[e.content]),e.tag===!1){t+=d(e.content);continue}let o=typeof e.tag=="string"?e.tag:"div";t+=`<${o}`,e.attrs&&(t+=Q(e.attrs));let c={[i.tag]:`></${o}>`,[i.slash]:" />",[i.default]:">"};if(k(o)){switch(l){case u.tag:t+=c[i.tag];break;case u.slash:t+=c[i.slash];break;case u.closeAs:t+=c[e.closeAs?i[e.closeAs]:i.default];break;default:t+=c[i.default]}e.content&&(t+=d(e.content))}else if(l===u.closeAs&&e.closeAs){let w=e.closeAs?i[e.closeAs]:i.default;t+=`${c[w]}${d(e.content)}`}else t+=`>${d(e.content)}</${o}>`}return t}function k(a){return g.length>0?g.some(t=>t.test(a)):!!(s==null?void 0:s.includes(a.toLowerCase()))}function Q(a){let t="";for(let[e,o]of Object.entries(a))if(typeof o=="string")if(_isjson2.default.call(void 0, o))t+=p(e,o);else if(m||j.test(o)){let c=o;R&&(c=o.replace(/"/g,""")),t+=p(e,c,$)}else o===""?t+=` ${e}`:t+=` ${e}=${o}`;else o===!0?t+=` ${e}`:typeof o=="number"&&(t+=p(e,o,$));return t}function p(a,t,e=1){return e===1?` ${a}='${t}'`:e===2?` ${a}="${t}"`:typeof t=="string"&&t.includes('"')?` ${a}='${t}'`:` ${a}="${t}"`}}var F=G;exports.default = F; | ||
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var I=Object.defineProperty,U=Object.defineProperties;var _=Object.getOwnPropertyDescriptors;var N=Object.getOwnPropertySymbols;var j=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var g=(o,r,n)=>r in o?I(o,r,{enumerable:!0,configurable:!0,writable:!0,value:n}):o[r]=n,b=(o,r)=>{for(var n in r||(r={}))j.call(r,n)&&g(o,n,r[n]);if(N)for(var n of N(r))v.call(r,n)&&g(o,n,r[n]);return o},R=(o,r)=>U(o,_(r));var _isjson = require('is-json'); var _isjson2 = _interopRequireDefault(_isjson);var $;(function(s){s[s.Smart=0]="Smart",s[s.Single=1]="Single",s[s.Double=2]="Double"})($||($= exports.quoteStyleEnum ={}));var d;(function(c){c.tag="tag",c.slash="slash",c.default="default",c.closeAs="closeAs"})(d||(d= exports.closingSingleTagOptionEnum ={}));var i;(function(s){s.tag="tag",s.slash="slash",s.default="default"})(i||(i= exports.closingSingleTagTypeEnum ={}));var y=["area","base","br","col","command","embed","hr","img","input","keygen","link","menuitem","meta","param","source","track","wbr"],L=/[\t\n\f\r "'`=<>]/,B={closingSingleTag:void 0,quoteAllAttributes:!0,replaceQuote:!0,quoteStyle:2};function q(o,r={}){var x;let n=y;r.singleTags&&(n=[...new Set([...y,...r.singleTags])]),r=R(b(b({},B),r),{singleTags:n});let{singleTags:s,closingSingleTag:c,quoteAllAttributes:k,replaceQuote:Q,quoteStyle:p}=r,h=(x=s==null?void 0:s.filter(a=>a instanceof RegExp))!=null?x:[];return Array.isArray(o)||(o||(o=""),o=[o]),u(o);function u(a){let t="";for(let e of a){if(e===!1||e===void 0||e===null||typeof e=="string"&&e.length===0||Number.isNaN(e))continue;if(Array.isArray(e)){if(e.length===0)continue;t+=u(e);continue}if(typeof e=="string"||typeof e=="number"){t+=e;continue}if(Array.isArray(e.content)||(e.content||(e.content=""),e.content=[e.content]),e.tag===!1){t+=u(e.content);continue}let f=typeof e.tag=="string"?e.tag:"div";t+=`<${f}`,e.attrs&&(t+=w(e.attrs));let l={[i.tag]:`></${f}>`,[i.slash]:" />",[i.default]:">"};if(m(f)){switch(c){case d.tag:t+=l[i.tag];break;case d.slash:t+=l[i.slash];break;case d.closeAs:t+=l[e.closeAs?i[e.closeAs]:i.default];break;default:t+=l[i.default]}e.content&&(t+=u(e.content))}else if(c===d.closeAs&&e.closeAs){let D=e.closeAs?i[e.closeAs]:i.default;t+=`${l[D]}${u(e.content)}`}else t+=`>${u(e.content)}</${f}>`}return t}function m(a){return h.length>0?h.some(t=>t.test(a)):!!(s==null?void 0:s.includes(a.toLowerCase()))}function w(a){let t="";for(let[e,f]of Object.entries(a))if(typeof f=="string")if(_isjson2.default.call(void 0, f))t+=A(e,f);else if(k||L.test(f)){let l=f;Q&&(l=f.replace(/"/g,""")),t+=A(e,l,p)}else f===""?t+=` ${e}`:t+=` ${e}=${f}`;else f===!0?t+=` ${e}`:typeof f=="number"&&(t+=A(e,f,p));return t}function A(a,t,e=1){return e===1?` ${a}='${t}'`:e===2?` ${a}="${t}"`:typeof t=="string"&&t.includes('"')?` ${a}='${t}'`:` ${a}="${t}"`}}exports.closingSingleTagOptionEnum = d; exports.closingSingleTagTypeEnum = i; exports.quoteStyleEnum = $; exports.render = q; |
{ | ||
"name": "posthtml-render", | ||
"version": "2.0.6", | ||
"version": "3.0.0", | ||
"description": "Renders PostHTML Tree to HTML/XML", | ||
@@ -33,24 +33,24 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@antfu/eslint-config-ts": "^0.6.4", | ||
"@commitlint/cli": "^12.1.1", | ||
"@commitlint/config-angular": "^12.1.1", | ||
"@types/node": "^15.0.2", | ||
"@antfu/eslint-config-ts": "^0.7.0", | ||
"@commitlint/cli": "^13.1.0", | ||
"@commitlint/config-angular": "^13.1.0", | ||
"@types/node": "^16.4.3", | ||
"ava": "^3.13.0", | ||
"c8": "^7.7.2", | ||
"c8": "^7.7.3", | ||
"clinton": "^0.14.0", | ||
"conventional-changelog-cli": "^2.0.34", | ||
"esbuild-register": "^2.5.0", | ||
"eslint": "^7.25.0", | ||
"esbuild-register": "^2.6.0", | ||
"eslint": "^7.31.0", | ||
"esm": "^3.2.25", | ||
"husky": "^6.0.0", | ||
"lint-staged": "^10.5.3", | ||
"posthtml-parser": "^0.8.5", | ||
"husky": "^7.0.1", | ||
"lint-staged": "^11.1.1", | ||
"posthtml-parser": "^0.9.1", | ||
"rewire": "^5.0.0", | ||
"rimraf": "^3.0.0", | ||
"ts-node": "^9.0.0", | ||
"tsup": "^4.10.1", | ||
"typescript": "^4.2.4", | ||
"xo": "^0.38.2" | ||
"ts-node": "^10.1.0", | ||
"tsup": "^4.12.5", | ||
"typescript": "^4.3.5", | ||
"xo": "^0.42.0" | ||
}, | ||
"types": "types/index.d.ts" | ||
"types": "dist/index.d.ts" | ||
} |
@@ -21,4 +21,2 @@ [![npm][npm]][npm-url] | ||
> âšī¸ This module is also available for [bower](http://bower.io) and as an AMD, CommonJS and IIFE (global) module, uncompressed and compressed | ||
<h2 align="center">Usage</h2> | ||
@@ -29,10 +27,10 @@ | ||
```js | ||
const render = require('posthtml-render') | ||
import { render } from ''posthtml-render; | ||
const tree = [] | ||
const tree = []; | ||
const node = {} | ||
const node = {}; | ||
node.tag = 'ul' | ||
node.attrs = { class: 'list' } | ||
node.tag = 'ul'; | ||
node.attrs = { class: 'list' }; | ||
node.content = [ | ||
@@ -42,7 +40,7 @@ 'one', | ||
'three' | ||
].map((content) => ({ tag: 'li', content })) | ||
].map((content) => ({ tag: 'li', content })); | ||
tree.push(node) | ||
tree.push(node); | ||
const html = render(tree, options) | ||
const html = render(tree, options); | ||
@@ -59,28 +57,2 @@ ``` | ||
### `đ Browser` | ||
```html | ||
<!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> | ||
``` | ||
<h2 align="center">Options</h2> | ||
@@ -87,0 +59,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31079
77
226