@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.49 kB
JavaScript
function e(n,t={}){let{scoped:o}=t,u="";if(("string"==typeof n||n instanceof String)&&(n=JSON.parse(n)),("string"==typeof t||t instanceof String||!0===t)&&(o=t),"root"===n.type&&n.children.length>0)u+=o?`\n <div class="${!0===o?"rte":o}">\n ${e(n.children,t)}\n </div>\n `:e(n.children,t);else for(const e of n)switch(e.type){case"paragraph":u+=r(e,t);break;case"heading":u+=s(e,t);break;case"list":u+=c(e,t);break;case"list-item":u+=i(e,t);break;case"link":u+=l(e,t);break;case"text":u+=a(e,t)}return u}function n(e,n){return n&&n[e]?n[e]:null}function t(e,t,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:n(e,t)})}>${r}</${e}>`}function r(n,r){const{classes:s}=r;return t("p",s,e(n?.children,r))}function s(n,r){const{classes:s}=r;return t(`h${n?.level}`,s,e(n?.children,r))}function c(n,r){const{classes:s}=r;return t("ordered"===n?.listType?"ol":"ul",s,e(n?.children,r))}function i(n,r){const{classes:s}=r;return t("li",s,e(n?.children,r))}function l(n,r){const{classes:s}=r,c={href:n?.url,title:n?.title,target:n?.target};return t("a",s,e(n?.children,r),c)}function a(e,n){const{classes:r,newLineToBreak:s}=n;return e?.bold&&e?.italic?t("strong",r,t("em",r,e?.value)):e?.bold?t("strong",r,e?.value):e?.italic?t("em",r,e?.value):s?e?.value?.replace(/\n/g,"<br>"):e?.value}export{e as convertSchemaToHtml};
//# sourceMappingURL=shopify-rich-text-renderer.esm.min.js.map