Skip to content

Instantly share code, notes, and snippets.

@DominikAngerer
Created April 16, 2020 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DominikAngerer/8e325b84580984acb3de88959ce5cd63 to your computer and use it in GitHub Desktop.
Save DominikAngerer/8e325b84580984acb3de88959ce5cd63 to your computer and use it in GitHub Desktop.
RichText with DOMPurify
const StoryblokClient require('storyblok-js-client')
const createDOMPurify = require('dompurify')
const { JSDOM } = require('jsdom')
// Storyblok is now available as variable which contains the richTextResolver functionality
const Storyblok = new StoryblokClient({})
// For Node this needs to use the jhttps://github.com/cure53/DOMPurify#okay-makes-sense-lets-move-on
const jsDomWindow = new JSDOM('').window;
const DOMPurify = createDOMPurify(jsDomWindow);
function createMarkup(storyblokHTML) {
return {
__html: DOMPurify.sanitize(Storyblok.richTextResolver.render(storyblokHTML))
}
}
const RichTextField = ({ data }) => {
return <div dangerouslySetInnerHTML={createMarkup(data)} />
}
export default RichTextField
// Usage in other components -> import that richTextField Component
// and use like: <RichTextField data=your_field></RichTextField>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment