import { Text } from 'slate'; export const richTextToHTML = (content: unknown): string => { if (Array.isArray(content)) { return content.reduce((output, node) => { const isTextNode = Text.isText(node); const { text, bold, code, italic, underline, strikethrough, } = node; if (isTextNode) { // convert straight single quotations to curly // "\u201C" is starting double curly // "\u201D" is ending double curly let html = text?.replace(/'/g, '\u2019'); // single quotes if (bold) { html = `${html}`; } if (code) { html = `${html}`; } if (italic) { html = `${html}`; } if (underline) { html = `${html}`; } if (strikethrough) { html = `${html}`; } return `${output}${html}`; } if (node) { let nodeHTML; switch (node.type) { case 'h1': nodeHTML = `

${richTextToHTML(node.children)}

`; break; case 'h2': nodeHTML = `

${richTextToHTML(node.children)}

`; break; case 'h3': nodeHTML = `

${richTextToHTML(node.children)}

`; break; case 'h4': nodeHTML = `

${richTextToHTML(node.children)}

`; break; case 'h5': nodeHTML = `
${richTextToHTML(node.children)}
`; break; case 'h6': nodeHTML = `
${richTextToHTML(node.children)}
`; break; case 'ul': nodeHTML = ``; break; case 'ol': nodeHTML = `
    ${richTextToHTML(node.children)}
`; break; case 'li': nodeHTML = `
  • ${richTextToHTML(node.children)}
  • `; break; case 'link': nodeHTML = `${richTextToHTML(node.children)}`; break; case 'relationship': nodeHTML = `Relationship to ${node.relationTo}: ${node.value}
    `; break; case 'upload': nodeHTML = `${node.relationTo} Upload: ${node.value}
    `; break; case 'p': case undefined: nodeHTML = `

    ${richTextToHTML(node.children)}

    `; break; default: nodeHTML = `${node.type}:
    ${JSON.stringify(node)}`; break; } return `${output}${nodeHTML}\n`; } return output; }, ''); } return ''; };