{"id":25184087,"url":"https://github.com/thebeyondgroup/shopify-rich-text-renderer","last_synced_at":"2025-04-05T15:03:41.393Z","repository":{"id":148710013,"uuid":"620672975","full_name":"TheBeyondGroup/shopify-rich-text-renderer","owner":"TheBeyondGroup","description":"A package to convert Shopify's rich text schema to an HTML string","archived":false,"fork":false,"pushed_at":"2025-01-16T05:55:47.000Z","size":482,"stargazers_count":56,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-05T09:45:58.135Z","etag":null,"topics":["javascript","metafields","metaobject","rich-text","rich-text-serializer","rich-text-to-html","shopify"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@thebeyondgroup/shopify-rich-text-renderer","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TheBeyondGroup.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-29T06:34:54.000Z","updated_at":"2025-02-28T21:25:02.000Z","dependencies_parsed_at":"2024-05-19T04:20:42.189Z","dependency_job_id":"c7708871-fc6b-4f08-9acd-99dbe2950199","html_url":"https://github.com/TheBeyondGroup/shopify-rich-text-renderer","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheBeyondGroup%2Fshopify-rich-text-renderer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheBeyondGroup%2Fshopify-rich-text-renderer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheBeyondGroup%2Fshopify-rich-text-renderer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheBeyondGroup%2Fshopify-rich-text-renderer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TheBeyondGroup","download_url":"https://codeload.github.com/TheBeyondGroup/shopify-rich-text-renderer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247353729,"owners_count":20925329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","metafields","metaobject","rich-text","rich-text-serializer","rich-text-to-html","shopify"],"created_at":"2025-02-09T19:29:40.242Z","updated_at":"2025-04-05T15:03:41.378Z","avatar_url":"https://github.com/TheBeyondGroup.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Shopify Rich Text Renderer\n\nThis package converts the rich text schema returned by Shopify's Storefront API to an HTML string. In particular, this package is useful when dealing with the rich text field type for _MetaObjects_ and _Metafields_ when using the Storefront API. Supports all of Shopify's Richtext Editor markup, including new line characters, and both **_bold \u0026 italic_** on the same text node.\n\n### Usage\n\n- Install `yarn add @thebeyondgroup/shopify-rich-text-renderer`\n- Then import the `convertSchemaToHtml()` function\n\n```javascript\nimport { convertSchemaToHtml } from '@thebeyondgroup/shopify-rich-text-renderer'\n\n/*  this is an example of the rich text Shopify returns\nconst richTextResponse = {\\\"type\\\":\\\"root\\\",\\\"children: [{\\\"type\\\":\\\"heading\\\"\n\\\"level\\\":1,\\\"children\\\":[{\\\"type\\\":\\\"text\\\",\\\"value\\\":\\\n\"Test Heading\\\", \\\"bold\\\": true, \\\"italic\\\": true}]},{\\\"listType\\\":\\\"ordered\\\",\\\"type\\\":\\\"list\\\",\n\\\"children\\\":[{\\\"type\\\":\\\"list-item\\\",\\\"children\\\":...\" */\n\nconvertSchemaToHtml(richTextResponse)\n```\n\n```html\n\u003c!-- Output: --\u003e\n\u003ch1\u003e\n  \u003cstrong\u003e\n    \u003cem\u003eTest Heading\u003c/em\u003e\n  \u003c/strong\u003e\n\u003c/h1\u003e\n\u003col\u003e\n  ...\n\u003c/ol\u003e\n...\n```\n\nTo get scoped HTML pass either true or the name of a class(es) to use in your scoped css selectors in the `scoped` property of the `options` parameter (options.scoped). This allows for the rich text HTML to be easily styled. _Note: You can also pass a scoped class name or_ `true`(to use default scoped class) instead of the options object, i.e. `convertSchemaToHtml(richTextResponse, 'rich-text-wrap')`.\n\n```javascript\n// scoped html\nconvertSchemaToHtml(richTextResponse, { scoped: true })\n```\n\n```html\n\u003c!-- Output: --\u003e\n\u003cdiv class=\"rte\"\u003e\n  \u003ch1\u003e\n    \u003cstrong\u003e\n      \u003cem\u003eTest Heading\u003c/em\u003e\n    \u003c/strong\u003e\n  \u003c/h1\u003e\n  \u003col\u003e\n    ...\n  \u003c/ol\u003e\n  ...\n\u003c/div\u003e\n```\n\nYou can also pass in a custom class name to be used as the scoped class instead of the default `rte`.\n\n```javascript\n//scoped w/ custom class name\nconvertSchemaToHtml(richTextResponse, { scoped: 'rich-text-wrap' })\n```\n\n```html\n\u003c!-- Output: --\u003e\n\u003cdiv class=\"rich-text-wrap\"\u003e\n  \u003ch1\u003e\n    \u003cstrong\u003e\u003cem\u003eTest Heading\u003c/em\u003e\u003c/strong\u003e\n  \u003c/h1\u003e\n  \u003col\u003e\n    ...\n  \u003c/ol\u003e\n  ...\n\u003c/div\u003e\n```\n\nIf you want to be more specific or are using something like Tailwind CSS you can pass a string of classes to be used with specific HTML elements to the `classes` property of the `options` parameter (options.classes).\n\nThis makes it easy to write your own wrapper class to apply a default classlist to various elements. There is also an option to convert new line character's to `\u003cbr/\u003e` (You can create new lines using `shift + space` in Shopify's rich text editor).\n\n```javascript\nconst options = {\n  scoped: false,\n  newLineToBreak: true, // convert new line character to \u003cbr/\u003e\n  classes: {\n    p: 'mt-3 text-lg', // paragraph classes\n    h1: 'mb-4 text-2xl md:text-4xl', // heading1 classes\n    h2: 'mb-4 text-xl md:text-3xl', // heading2 classes\n    h3: 'mb-3 text-lg md:text-2xl', // heading3 classes\n    h4: 'mb-3 text-base md:text-lg', // heading4 classes\n    h5: 'mb-2.5 text-sm md:text-base', // heading5 classes\n    h6: 'mb-2 text-xs md:text-sm', // heading6 classes\n    ol: 'my-3 ml-3 flex flex-col gap-y-2', // order list classes\n    ul: 'my-3 ml-3 flex flex-col gap-y-2', // unordered list classes\n    li: 'text-sm md:text-base', // list item classes\n    a: 'underline text-blue-500 hover:text-blue-700', // anchor/link classes\n    strong: 'font-medium', // bold/strong classes\n    em: 'font-italic', // italic/em classes\n  },\n}\n\n// Applying classes directly to elements\nconvertSchemaToHtml(richTextResponse, options)\n```\n\n```html\n\u003c!-- Output: --\u003e\n\u003ch1 class=\"mb-4 text-2xl md:text-4xl\"\u003eGroceries\u003c/h1\u003e\n\u003cp class=\"mt-3 text-lg\"\u003e\n  Here is my shopping list for various fruit to buy at\n  \u003ca href=\"https://grocerystore.com\" class=\"underline text-blue-500 hover:text-blue-700\"\u003e The Grocery Store \u003c/a\u003e\n\u003c/p\u003e\n\u003col class=\"my-3 ml-3 flex flex-col gap-y-2\"\u003e\n  \u003cli class=\"text-sm md:text-base\"\u003eapples\u003c/li\u003e\n  \u003cli class=\"text-sm md:text-base\"\u003eoranges\u003c/li\u003e\n  \u003cli class=\"text-sm md:text-base\"\u003ebananas\u003c/li\u003e\n\u003c/ol\u003e\n```\n\n**React Typescript Component Example**\n\n```typescript\n// @/components/RichtextToHTML.tsx\nimport {convertSchemaToHtml} from '@thebeyondgroup/shopify-rich-text-renderer';\nimport type {Schema, Options} from '@thebeyondgroup/shopify-rich-text-renderer';\nimport {useEffect, useState} from 'react';\n\n/**\n * Default options for the HTML conversion, using Tailwind CSS classes\n * for styling, you can update these to suit your specific needs\n **/\n\nconst defaultOptions = {\n  scoped: false,\n  newLineToBreak: false, // convert new line character to \u003cbr/\u003e\n  classes: {\n    p: 'mt-2 text-sm', // paragraph classes\n    h1: 'mb-4 text-3xl md:text-4xl', // heading1 classes\n    h2: 'mb-4 text-2xl md:text-3xl', // heading2 classes\n    h3: 'mb-3 text-lg md:text-2xl', // heading3 classes\n    h4: 'mb-3 text-base md:text-lg', // heading4 classes\n    h5: 'mb-2.5 text-sm md:text-base', // heading5 classes\n    h6: 'mb-2 text-xs md:text-sm', // heading6 classes\n    ol: 'my-3 ml-3 flex flex-col gap-y-2', // order list classes\n    ul: 'my-3 ml-3 flex flex-col gap-y-2', // unordered list classes\n    li: 'text-sm md:text-base', // list item classes\n    a: 'underline text-gray-700 hover:text-blue-700 text-sm', // anchor/link classes\n    strong: 'font-semibold', // bold/strong classes\n    em: 'font-italic', // italic/em classes\n  },\n};\n\nfunction mergeOptions(\n  options: Options,\n  defaultOptions: Options,\n  classes?: any,\n  newLineToBreak?: boolean,\n) {\n  return {\n    ...defaultOptions,\n    ...options,\n    classes: {\n      ...defaultOptions?.classes,\n      ...options.classes,\n      ...classes,\n    },\n    newLineToBreak,\n  };\n}\n\ninterface RichtextToHtmlProps {\n  schema: string | Schema | Schema[];\n  options?: Options;\n  className?: string;\n  newLineToBreak?: boolean;\n  classes?: {\n    p?: string;\n    h1?: string;\n    h2?: string;\n    h3?: string;\n    h4?: string;\n    h5?: string;\n    h6?: string;\n    ol?: string;\n    ul?: string;\n    li?: string;\n    a?: string;\n    strong?: string;\n    em?: string;\n  };\n}\n\n/**\n * React component that converts a Shopify Richtext schema to HTML\n **/\n\nexport default function RichtextToHtml({\n  schema,\n  options,\n  className,\n  newLineToBreak,\n  classes,\n}: RichtextToHtmlProps) {\n\n  //options passed via props override default options (classes, scoped, newLineToBreak) etc...\n  const combinedOptions = mergeOptions(\n    (options as Options) || {},\n    defaultOptions,\n    classes,\n    newLineToBreak,\n  );\n\n  const html = convertSchemaToHtml(schema, combinedOptions);\n  return (\n    \u003c\u003e\n      \u003cdiv className={className} dangerouslySetInnerHTML={{__html: html}} /\u003e\n    \u003c/\u003e\n  );\n}\n```\n\n**Example of the react RichtextToHTML components in use**\n\n```typescript\n// App.tsx\nimport React from 'react'\nimport RichTextToHtml from './RichTextToHtml'\n\n/**\n * Normally schema would be passed through\n * a loader after requesting it from shopify api\n * */\nconst productDescriptionSchema = {\n  type: 'root',\n  children: [\n    ...\n  ],\n}\n\nconst userProfileSchema =  {\n  type: 'root',\n  children: [\n    ...\n  ],\n}\n\n\n// Custom options for demonstration of overriding defaults.\n// Note: you probably wouldn't need to set the scoped class name \u0026 apply unique classes per element\nconst customOptions = {\n  scoped: 'custom-scope',\n  classes: {\n    p: 'text-lg text-slate-800 my-2',\n    h2: 'text-2xl md:text-4xl font-semibold leading-none tracking-wide mb-2',\n  },\n  newLineToBreak: true,\n}\n\n// Main React App Component\nconst App = () =\u003e {\n  return (\n    \u003cdiv className=\"container flex flex-col gap-4 mx-auto p-4\"\u003e\n      \u003ch1 className=\"text-3xl font-bold mb-6\"\u003eShopify Storefront\u003c/h1\u003e\n      \u003csection\u003e\n        \u003ch2 className=\"text-xl font-semibold\"\u003eProduct Description\u003c/h2\u003e\n        \u003cRichTextToHtml className=\"my-5 py-3\" schema={productDescriptionSchema} options={customOptions} /\u003e\n      \u003c/section\u003e\n      \u003csection\u003e\n        \u003ch2 className=\"text-xl font-semibold\"\u003eUser Profile\u003c/h2\u003e\n        {/**\n          * Example showing prop priority, classes has priority over prop option.classes.\n          * scoped:false, p: 'my-3 text-sm font-medium', h6: 'text-xs font-bold' newLineToBreak: true\n          */}\n        \u003cRichTextToHtml\n          schema={userProfileSchema}\n          newLineToBreak={true}\n          classes={{ p: 'my-3 text-sm font-medium'}}\n          options={{scoped: false, classes:{h6: 'text-xs font-bold', p: 'text-xs my-2'}}}\n        /\u003e\n      \u003c/section\u003e\n    \u003c/div\u003e\n  )\n}\n\nexport default App\n```\n\n**Live Examples**\n\n- [JSFiddle: Basic Use](https://jsfiddle.net/r2d4wsna/)\n- [Stackblitz: React Typescript Component](https://stackblitz.com/edit/react-starter-typescript-ohxvltnb?file=components%2FRichtextToHtml.tsx)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthebeyondgroup%2Fshopify-rich-text-renderer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthebeyondgroup%2Fshopify-rich-text-renderer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthebeyondgroup%2Fshopify-rich-text-renderer/lists"}