How to use html-to-react - 10 common examples

To help you get started, we’ve selected a few html-to-react examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github freeCodeCamp / pantry-for-good / client / components / ContentPage.js View on Github external
import React, {Component} from 'react'
import {connect} from 'react-redux'
import {Link} from 'react-router-dom'
import {Parser, ProcessNodeDefinitions} from 'html-to-react'
import PropTypes from 'prop-types'

import selectors from '../store/selectors'
import {loadPage} from '../modules/page/reducer'

import 'react-quill/dist/quill.snow.css'
import 'react-quill/dist/quill.core.css'

const htmlToReactParser = new Parser()
const processNodeDefinitions = new ProcessNodeDefinitions(React)

const getProcessingInstructions = bindings => [{
  // convert internal links to Link components
  shouldProcessNode: node => node.name && node.name === 'a' &&
    node.attribs.href.startsWith('/'),
  processNode: function generateLink(node, children) {
    return {children}
  }
}, {
  // bind placeholders
  shouldProcessNode: node => node.name && node.name === 'span' &&
    node.attribs.class === 'ql-placeholder-content',
  processNode: function insertPlaceholder(node) {
    const content = bindings[node.attribs['data-id']] || '{{missing}}'
    return <span>{content}</span>
github triplecanopy / b-ber / packages / b-ber-reader / src / helpers / XMLAdaptor.js View on Github external
return new Promise(resolve =&gt; {
      const promises = []
      const htmlToReactParser = new HtmlToReactParser()
      const documentProcessor = new DocumentProcessor({
        paddingLeft,
        columnGap,
        responseURL,
      })

      const { xml, doc } = documentProcessor.parseXML(response.data)
      const re = /]*?&gt;([\s\S]*)&lt;\/body&gt;/

      // create react element that will be appended to our #frame element.
      // we wrap this in a Promise so that we can resolve the content and
      // styles at the same time
      let data_
      data_ = xml.match(re)
      data_ = data_[1] // eslint-disable-line prefer-destructuring
      data_ = data_.replace(/&gt;\s*?&lt;')
github JulianMayorga / render-hearthstone-card-react-svg / components / StylableCard.js View on Github external
// @flow

import React, { PropTypes } from 'react';
import { Parser } from 'html-to-react';

const parser = new Parser();

const hideElement = (hide, elementName) => ((hide && hide.includes(elementName)) ? 'none' : 'block');

const StylableCard = ({
  card: {
    title,
    id,
    text,
    race,
    rarity,
    set,
    cost,
    strength,
    health,
  },
  hide,
github kyma-project / website / src / components / shared / ReactMarkdown / parseHTML / index.tsx View on Github external
import React from "react";
import ReactMarkdown from "react-markdown";
// @ts-ignore
import HtmlToReact from "html-to-react";
// @ts-ignore
import htmlParser from "react-markdown/plugins/html-parser";

import { tabs } from "./Tabs";

const isValidNode = (node: any) => node.type !== "script";
const processNodeDefinitions = new HtmlToReact.ProcessNodeDefinitions(React);

export default (headingPrefix: string) =>
  htmlParser({
    isValidNode,
    processingInstructions: [
      // Tabs processing
      tabs(headingPrefix),
      {
        // Anything else
        shouldProcessNode: (node: any) => true,
        processNode: processNodeDefinitions.processDefaultNode,
      },
    ],
  });
github mattermost / mattermost-webapp / utils / message_html_to_component.jsx View on Github external
export function messageHtmlToComponent(html, isRHS, options = {}) {
    if (!html) {
        return null;
    }

    const parser = new Parser();
    const processNodeDefinitions = new ProcessNodeDefinitions(React);

    function isValidNode() {
        return true;
    }

    const processingInstructions = [

        // Workaround to fix MM-14931
        {
            replaceChildren: false,
            shouldProcessNode: (node) => node.type === 'tag' && node.name === 'input' && node.attribs.type === 'checkbox',
            processNode: (node) => {
                const attribs = node.attribs || {};
                node.attribs.checked = Boolean(attribs.checked);

                return React.createElement('input', {...node.attribs});
github kyma-project / console / components / react / src / components / ReactMarkdown / parseHTML / index.js View on Github external
import React from 'react';
import HtmlToReact from 'html-to-react';
import htmlParser from 'react-markdown/plugins/html-parser';

import { tabs } from './Tabs';
import { link } from './Link';
import { img } from './Image';

const isValidNode = node => node.type !== 'script';
const processNodeDefinitions = new HtmlToReact.ProcessNodeDefinitions(React);

export default htmlParser({
  isValidNode,
  processingInstructions: [
    tabs,
    link,
    img,
    {
      // Anything else
      shouldProcessNode: node => true,
      processNode: processNodeDefinitions.processDefaultNode,
    },
  ],
});
github jimmyleray / Emendare / client / src / components / markdown.js View on Github external
import React from 'react'

import HtmlToReact from 'html-to-react' // https://github.com/aknuds1/html-to-react
import ReactMarkdown from 'react-markdown' // https://github.com/rexxars/react-markdown
import htmlParser from 'react-markdown/plugins/html-parser'

import 'github-markdown-css' // https://github.com/sindresorhus/github-markdown-css

// See https://github.com/aknuds1/html-to-react#with-custom-processing-instructions
// for more info on the processing instructions
const processNodeDefinitions = new HtmlToReact.ProcessNodeDefinitions(React)
const parseHtml = htmlParser({
  isValidNode: node =&gt; node.type !== 'script',
  processingInstructions: [
    {
      shouldProcessNode: node =&gt; {
        return true
      },
      processNode: processNodeDefinitions.processDefaultNode
    }
  ]
})

export const Markdown = ({ children, className }) =&gt; (
  <div>
    </div>
github freeCodeCamp / pantry-for-good / client / components / ContentPage.js View on Github external
import React, {Component} from 'react'
import {connect} from 'react-redux'
import {Link} from 'react-router-dom'
import {Parser, ProcessNodeDefinitions} from 'html-to-react'
import PropTypes from 'prop-types'

import selectors from '../store/selectors'
import {loadPage} from '../modules/page/reducer'

import 'react-quill/dist/quill.snow.css'
import 'react-quill/dist/quill.core.css'

const htmlToReactParser = new Parser()
const processNodeDefinitions = new ProcessNodeDefinitions(React)

const getProcessingInstructions = bindings =&gt; [{
  // convert internal links to Link components
  shouldProcessNode: node =&gt; node.name &amp;&amp; node.name === 'a' &amp;&amp;
    node.attribs.href.startsWith('/'),
  processNode: function generateLink(node, children) {
    return {children}
  }
}, {
  // bind placeholders
  shouldProcessNode: node =&gt; node.name &amp;&amp; node.name === 'span' &amp;&amp;
    node.attribs.class === 'ql-placeholder-content',
  processNode: function insertPlaceholder(node) {
    const content = bindings[node.attribs['data-id']] || '{{missing}}'
    return <span>{content}</span>
  }
github TerriaJS / terriajs / lib / ReactViews / Custom / parseCustomHtmlToReact.js View on Github external
"use strict";

const React = require("react");
const HtmlToReact = require("html-to-react");
const combine = require("terriajs-cesium/Source/Core/combine").default;
const defined = require("terriajs-cesium/Source/Core/defined").default;
const utils = require("html-to-react/lib/utils");

const CustomComponents = require("./CustomComponents");

const htmlToReactParser = new HtmlToReact.Parser({
  decodeEntities: true
});
const processNodeDefinitions = new HtmlToReact.ProcessNodeDefinitions(React);

const isValidNode = function() {
  return true;
};

const shouldProcessEveryNodeExceptWhiteSpace = function(node) {
  // Use this to avoid white space between table elements, eg.
  //     \n<table> <tbody> <tr><td>x</td> <td>3</td> </tr> </tbody> </table>
  // being rendered as empty <span> elements, and causing React errors.
  return node.type !== "text" || node.data.trim();
};

let keyIndex = 0;

/**
 * @private</span>
github TerriaJS / terriajs / lib / ReactViews / Custom / parseCustomHtmlToReact.js View on Github external
processNode: function(node, children, index) {
      // eslint-disable-line react/display-name
      const elementProps = {
        key: "anchor-" + keyIndex++,
        target: "_blank",
        rel: "noreferrer noopener"
      };
      node.attribs = combine(node.attribs, elementProps);
      return utils.createElement(node, index, node.data, children);
    }
  });

html-to-react

A lightweight library that converts raw HTML to a React DOM structure.

MIT
Latest version published 1 year ago

Package Health Score

72 / 100
Full package analysis