UNPKG

@blade47/editorjs-html

Version:

A utility to parse editorjs clean data to HTML.

2 lines (1 loc) 3.29 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).edjsHTML=t()}(this,(function(){"use strict";var e={code:({data:e})=>`<pre><code>${e.code}</code></pre>`,embed:({data:e})=>{const t=e.service;return"vimeo"===t?`<iframe src="${e.embed}" height="${e.height}" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe>`:"youtube"===t?`<iframe width="${e.width}" height="${e.height}" src="${e.embed}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`:(console.error("[editorjs-html]: Only Youtube & Vimeo embeds are available by default. Write your own embed parser for other options."),"")},header:({data:e})=>`<h${e.level}>${e.text}</h${e.level}>`,image:({data:e})=>{const t=e.file?.url||e.url||"",r=e.caption||"",n=!r||""===r.trim();return`<table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%">\n <tr>\n <td align="center" style="padding-top:20px;${n?"padding-bottom:20px;":"padding-bottom:5px;"}">\n <img \n src="${t}" \n alt="${r}" \n width="500" \n height="auto" \n style="display:block; max-width:500px; width:100%; height:auto; border:0; outline:none; text-decoration:none;" \n />\n </td>\n </tr>\n ${n?"":`<tr>\n <td align="center" style="padding-bottom:20px;">\n <p style="margin:0; font-size:14px; line-height:1; color:#999999; text-align:center;">${r}</p>\n </td>\n </tr>`}\n </table>`},list:({data:e})=>{let t="ul";"ordered"===e.style&&(t="ol");const r=(t,n)=>{if(!t||!t.length)return"";const o=t.map((t=>{if("string"==typeof t)return`<li>${t}</li>`;let o=t.content||"",i="";if(t.items&&t.items.length&&(i=r(t.items,n)),"checklist"===e.style&&t.meta&&t.meta.hasOwnProperty("checked")){return`<li><label><input type="checkbox"${t.meta.checked?" checked":""} disabled> ${o}</label>${i}</li>`}return`<li>${o}${i}</li>`}));let i="";return"ol"===n&&e.meta&&(e.meta.start&&1!==e.meta.start&&(i+=` start="${e.meta.start}"`),e.meta.counterType&&"numeric"!==e.meta.counterType&&(i+=` type="${function(e){switch(e){case"lower-alpha":return"a";case"upper-alpha":return"A";case"lower-roman":return"i";case"upper-roman":return"I";default:return"1"}}(e.meta.counterType)}"`)),`<${n}${i}>${o.join("")}</${n}>`};return r(e.items,t)},paragraph:({data:e})=>{const t=e.alignment||e.align;return t?`<p style="text-align:${t}"> ${e.text} </p>`:`<p>${e.text}</p>`},quote:({data:e})=>`<blockquote>${e.text}</blockquote> - ${e.caption}`,delimiter:({data:e})=>"<br/>"};return(t={},r={strict:!1})=>{const n={...e,...t};return{parse:e=>(({blocks:e},t,r)=>e.reduce(((e,n)=>{if(n.type in t)return e+t[n.type](n);const o=`[editorjs-html]: Parser function for ${n.type} does not exist`;if(r.strict)throw new Error(o);return console.error(o),e}),""))(e,n,r),parseBlock:e=>((e,t,r)=>{if(e.type in t)return t[e.type](e);const n=`[editorjs-html]: Parser function for ${e.type} does not exist`;if(r.strict)throw new Error(n);console.error(n)})(e,n,r)}}}));