@torchlight-api/torchlight-cli
Version:
A CLI for Torchlight - the syntax highlighting API
3 lines (2 loc) • 4.03 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("axios"),e=require("md5"),i=require("lodash.get"),o=require("lodash.chunk"),n=require("chalk");function s(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=s(t),h=s(e),l=s(i),a=s(o),u=s(n);let c=!1;function g(t,e){e=Array.from(e),c||console.log(t(e.shift()),...e)}var f={error:function(){g(u.default.bold.bgRed,arguments)},info:function(){g(u.default.green,arguments)},silence:function(t=!0){c=t}};let d={};function p(){}p.prototype.get=function(t,e){if(!Object.prototype.hasOwnProperty.call(d,t))return e;const i=d[t];return Date.now()/1e3>i.expires?(this.delete(t),e):i.value},p.prototype.set=function(t,e,i=10080){d[t]={expires:Date.now()/1e3+i,value:e}},p.prototype.delete=function(t){delete d[t]},p.prototype.clear=function(){d={}};const y=function(){this.initialized=!1,this.chunkSize=30,this.configuration={}};y.prototype.init=function(t,e){var i,o,n;return this.initialized||(t=t||{},null===(i=process)||void 0===i||null===(o=i.env)||void 0===o||!o.TORCHLIGHT_TOKEN||null!==(n=t)&&void 0!==n&&n.token||(t.token=process.env.TORCHLIGHT_TOKEN),this.initialized=!0,this.configuration=t,this.cache=e||new p),this},y.prototype.config=function(t,e){return l.default(this.configuration,t,e)},y.prototype.configHash=function(){return h.default(this.configuration)},y.prototype.highlight=function(t){t.map((t=>t.setResponseData(this.cache.get(t.hash(),{}))));const e=t.filter((t=>!t.highlighted));return this.request(e).then((i=>(e.forEach((t=>{const e=i.find((e=>t.id===e.id));e&&e.highlighted&&(this.cache.set(t.hash(),{highlighted:e.highlighted,classes:e.classes,styles:e.styles}),t.setResponseData(e))})),t.filter((t=>!t.highlighted)).forEach((t=>{f.error(`A block failed to highlight. The code was: \`${t.code.substring(0,20)} [...]\``),t.highlighted=t.code.split("\n").map((t=>{return`<div class="line">${e=t,String(e).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")}</div>`;var e})).join(""),t.classes="torchlight"})),t)))},y.prototype.request=function(t){if(!t.length)return Promise.resolve([]);const e=this.config("token");if(t.length>this.chunkSize)return this.fan(t);const i=this.config("host","https://api.torchlight.dev");return r.default.post(`${i}/highlight`,{blocks:t.map((t=>t.toRequestParams())),options:this.config("options",{})},{headers:{Authorization:`Bearer ${e}`,"X-Torchlight-Client":"Torchlight CLI"}}).then((t=>t.data.blocks))},y.prototype.fan=function(t){const e=[],i=[],o=a.default(t,this.chunkSize).map((t=>this.request(t)));return Promise.allSettled(o).then((t=>{if(t.forEach((t=>{"fulfilled"===t.status&&e.push(...t.value),"rejected"===t.status&&i.push(t.reason)})),e.length)return i.length&&f.error(`${i.length} fanned request(s) failed, but others succeeded. Error: ${i[0]}.`),e;if(i.length)throw new Error(i[0]);return[]}))};var m=new y;function v(){const t=()=>Math.floor(65536*(1+Math.random())).toString(16).substring(1);return`${t()}${t()}-${t()}-${t()}-${t()}-${t()}${t()}${t()}`.toLowerCase()}function $(t={}){t={id:v(),theme:m.config("theme","nord"),...t},this.id=t.id,this.code=t.code,this.language=t.language,this.theme=t.theme,this.highlighted=null,this.classes=null,this.styles=null}$.prototype.hash=function(){return h.default(""+this.language+this.theme+this.code+m.config("bust",0)+JSON.stringify(m.config("options")))},$.prototype.code=function(t){return this.code=t,this},$.prototype.language=function(t){return this.language=t,this},$.prototype.theme=function(t){return this.theme=t,this},$.prototype.placeholder=function(t=""){return t&&(t=`-${t}`),`__torchlight-block-[${this.id}]${t}__`},$.prototype.setResponseData=function(t){return t&&(this.highlighted=t.highlighted,this.classes=t.classes,this.styles=t.styles),this},$.prototype.toRequestParams=function(){return{id:this.id,hash:this.hash,language:this.language,theme:this.theme,code:this.code}},exports.Block=$,exports.torchlight=m;
//# sourceMappingURL=torchlight-cli.cjs.min.js.map