html-creator
Version:
Generate HTML with node.js
2 lines (1 loc) • 3.77 kB
JavaScript
"use strict";var t=require("prettier"),e=require("path"),n=require("fs"),r=require("mkdirp");const s=["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"],i=t=>t?Object.keys(t).filter((e=>void 0!==t[e]&&null!==t[e])).map((e=>` ${e.replace(/([A-Z])/g,(t=>`-${t[0].toLowerCase()}`))}="${t[e]}"`)).join(""):"",o=t=>{return t.type?s.includes(t.type)?`<${t.type}${i(t.attributes)} />`:`<${t.type}${i(t.attributes)}>${e=t.content,e&&e.constructor===Array?e.map((t=>o(t))).join(""):e||""}</${t.type}>`:"string"==typeof t.content||t.content instanceof String?t.content:"";var e},a=(t,e="default")=>{const n={default:"[37m%s[0m",success:"[32m%s[0m",error:"[31m%s[0m"},r=n[e]?n[e]:n.default,s=`HTML-Creator >> ${t}`;return void 0===process.env.JEST_WORKER_ID&&console.log(r,s),{logColor:r,logMsg:s}},c=({stack:t,type:e,id:n,className:r})=>{const s=[];t&&Array.isArray(t)&&(s.push(t.filter((t=>e?t.type===e:n?t.attributes&&t.attributes.id===n:r?t.attributes&&t.attributes.class===r:null))),t.forEach((t=>{if(t.content&&t.content.constructor===Array){const i=c({stack:t.content,type:e,id:n,className:r});i&&s.push(i)}})));const i=s.flat();return 1===i.length?[i[0]]:i};class l{constructor(t){this.content=t&&Array.isArray(t)?t:[]}renderContent(){let t="";return this.content&&this.content.forEach((e=>{t+=`${o(e)}`})),t}getHTML(e){const n=`<!DOCTYPE html>${o({type:"html",content:this.renderContent(),attributes:e?.htmlTagAttributes})}`;return e?.disablePrettier?n.replace(/(\r\n|\n|\r)/gm,""):t.format(n,{parser:"html"})}setTitle(t){const e=this.findElementByType("title")[0];if(e)return e.content=t,t;const n=this.findElementByType("head")[0];return n?(n.content&&n.content.constructor===Array?n.content.push({type:"title",content:t}):n.content=[{type:"title",content:t}],t):(this.content.push({type:"head",content:[{type:"title",content:t}]}),this)}addElement(t){var e,n;return this.content=(e=this.content,n=t,Array.isArray(n)?e.concat(n):(e.push(n),e)),this}addElementToTarget(t,e){if(!e)return this;let n=[];return e.id?n=this.findElementById(e.id):e.class?n=this.findElementByClassName(e.class):e.type&&(n=this.findElementByType(e.type)),n.length>0&&n.forEach((e=>{e.content?Array.isArray(e.content)?e.content.push(t):"string"!=typeof e.content&&e.constructor!==String||(e.content=[{content:e.content},t]):e.content=[t]})),this}addElementToClass(t,e){if(Array.isArray(e)){for(const n of e)this.addElementToTarget(n,{class:t});return this}return this.addElementToTarget(e,{class:t})}addElementToId(t,e){if(Array.isArray(e)){for(const n of e)this.addElementToTarget(n,{id:t});return this}return this.addElementToTarget(e,{id:t})}addElementToType(t,e){if(Array.isArray(e)){for(const n of e)this.addElementToTarget(n,{type:t});return this}return this.addElementToTarget(e,{type:t})}findElementByType(t){return c({stack:this.content,type:t})}findElementById(t){return c({stack:this.content,id:t})}findElementByClassName(t){return c({stack:this.content,className:t})}withBoilerplate(){return this.content=[{type:"head",content:[{type:"meta",attributes:{charset:"utf-8"}},{type:"meta",attributes:{name:"viewport",content:"width=device-width, initial-scale=1, shrink-to-fit=no"}}]},{type:"body",content:this.content}],this}}module.exports=class{constructor(t){this.document=new l(t)}withBoilerplate(){return this.document.withBoilerplate(),this}renderHTMLToFile(t,s){return t?(async(t,s)=>{await r.mkdirp(e.dirname(t)),n.writeFileSync(t,s)})(t,this.renderHTML(s)).then((()=>a(`HTML generated (${t})`,"success"))).catch((t=>a(t,"error"))):Promise.reject(a("A file path is required","error"))}renderHTML(t){if(t){const{excludeHTMLtag:e}=t;if(e)return this.document.renderContent()}return this.document.getHTML(t)}};