UNPKG

@thebeyondgroup/shopify-rich-text-renderer

Version:

Convert Shopify's Metafields/Metaobjects rich text type from a rich text AST/schema to HTML. Works great with hydrogen/headless & regular storefronts.

3 lines (2 loc) 1.73 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).convertSchemaToHtml={})}(this,(function(e){"use strict";function n(e,t={}){let{scoped:r}=t,u="";if(("string"==typeof e||e instanceof String)&&(e=JSON.parse(e)),("string"==typeof t||t instanceof String||!0===t)&&(r=t),"root"===e.type&&e.children.length>0)u+=r?`\n <div class="${!0===r?"rte":r}">\n ${n(e.children,t)}\n </div>\n `:n(e.children,t);else for(const n of e)switch(n.type){case"paragraph":u+=s(n,t);break;case"heading":u+=c(n,t);break;case"list":u+=i(n,t);break;case"list-item":u+=l(n,t);break;case"link":u+=o(n,t);break;case"text":u+=a(n,t)}return u}function t(e,n){return n&&n[e]?n[e]:null}function r(e,n,r,s={}){return`<${e}${function(e){return!e&&e?.class?"":Object.keys(e).map((n=>{if(e[n])return` ${n}="${e[n]}"`})).join("")}(s={...s,class:t(e,n)})}>${r}</${e}>`}function s(e,t){const{classes:s}=t;return r("p",s,n(e?.children,t))}function c(e,t){const{classes:s}=t;return r(`h${e?.level}`,s,n(e?.children,t))}function i(e,t){const{classes:s}=t;return r("ordered"===e?.listType?"ol":"ul",s,n(e?.children,t))}function l(e,t){const{classes:s}=t;return r("li",s,n(e?.children,t))}function o(e,t){const{classes:s}=t,c={href:e?.url,title:e?.title,target:e?.target};return r("a",s,n(e?.children,t),c)}function a(e,n){const{classes:t,newLineToBreak:s}=n;return e?.bold&&e?.italic?r("strong",t,r("em",t,e?.value)):e?.bold?r("strong",t,e?.value):e?.italic?r("em",t,e?.value):s?e?.value?.replace(/\n/g,"<br>"):e?.value}e.convertSchemaToHtml=n})); //# sourceMappingURL=shopify-rich-text-renderer.min.js.map