@nent/core
Version:
4 lines • 3.12 kB
JavaScript
/*!
* NENT 2022
*/
import{r as t,f as s,h as i,a as r,H as e}from"./index-916ca544.js";import{e as n}from"./index-f7016b94.js";import{f as a,e as o}from"./logging-5a93c8af.js";import{d as h,b as c,r as l}from"./remote-0f753d9a.js";import{r as m}from"./elements-1b845a48.js";import{b as d,a as f}from"./expressions-2c27c47c.js";import{D as u}from"./interfaces-8c5cd1b8.js";import{f as p}from"./jsonata.worker-8bcb5e74.js";import{resolveTokens as b}from"./tokens-78f8cdbe.js";import{d as w}from"./utils-337f03b7.js";import{s as j}from"./state-adf07580.js";import{a as g,o as k}from"./state-27a8a5bc.js";import"./factory-acbf0d3d.js";import"./index-4bfabbbd.js";import"./values-ddfac998.js";import"./promises-584c4ece.js";import"./strings-47d55561.js";const v=d(import("./remarkable.worker-3e911557.js").then((t=>t.worker)),"stencil.remarkable.worker","renderMarkdown"),y=class{constructor(s){t(this,s),this.contentClass="rendered-content",this.renderCache={},this.location=j.location,this.contentElement=null,this.mode="cors",this.resolveTokens=!1,this.deferLoad=!1,this.noCache=!1}async getContentKey(){return this.src?await h(this.src):this.childScript?"script":null}get canCache(){return!1===this.noCache&&!1===this.resolveTokens}get childScript(){return this.el.querySelector("script")}async componentWillLoad(){(this.resolveTokens||null!=this.when)&&(g.dataEnabled?this.subscribeToDataEvents():this.dataSubscription=k("dataEnabled",(t=>{t&&(this.subscribeToDataEvents(),this.dataSubscription())})))}subscribeToDataEvents(){this.dataSubscription=n.on(u.DataChanged,(()=>{s(this.el)}))}async componentWillRender(){let t=!this.deferLoad;if(t&&this.when&&(t=await f(this.when)),t){if(this.contentElement&&this.canCache)return;this.contentElement=await this.resolveContentElement()}else this.contentElement=null}async resolveContentElement(){var t;const s=await this.getContentKey();if(s&&this.renderCache[s])return this.renderCache[s];const i=this.src?await this.getContentFromSrc():await this.getContentFromScript();if(null==i)return null;const r=document.createElement("div");return r.innerHTML=await v(i)||"",r.className=this.contentClass,g.elementsEnabled&&m(r),null===(t=j.router)||void 0===t||t.captureInnerLinks(r),this.highlight(r),s&&this.canCache&&(this.renderCache[s]=r),r}async getContentFromSrc(){try{let t=await c(window,this.src,this.mode,this.resolveTokens);if(t&&this.json){a(g.debug,`n-content-markdown: filtering: ${this.json}`);const s=JSON.parse(t);t=await p(this.json,s)}return t}catch(t){return o(`n-content-markdown: unable to retrieve content from ${this.src}. ${t}`),null}}async getContentFromScript(){const t=this.childScript;if(!(null==t?void 0:t.textContent))return null;let s=w(t.textContent);return this.resolveTokens&&(s=await b(s)),s}highlight(t){const s=window.Prism;(null==s?void 0:s.highlightAllUnder)&&s.highlightAllUnder(t)}render(){return l(this.el,`.${this.contentClass}`,this.contentElement),i(e,{hidden:null==this.contentElement})}disconnectedCallback(){var t;null===(t=this.dataSubscription)||void 0===t||t.call(this)}get el(){return r(this)}};export{y as n_content_markdown}