All files Refractor.js

92.31% Statements 24/26
85.71% Branches 12/14
66.67% Functions 2/3
95.83% Lines 23/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 681x 1x 1x 1x 1x     1x     5x 5x                   5x   5x 1x 1x     5x 5x 1x     5x   5x 5x     1x                                   1x           2x 1x   1x  
const React = require('react')
const PropTypes = require('prop-types')
const fract = require('refractor/core.js')
const mapChildren = require('./mapChildren')
const addMarkers = require('./addMarkers')
 
// eslint-disable-next-line id-length
const h = React.createElement
 
function Refractor(props) {
  Eif (process.env.NODE_ENV !== 'production') {
    Iif (!fract.registered(props.language)) {
      // eslint-disable-next-line no-console
      console.warn(
        `No language definitions for "${
          props.language
        }" seems to be registered, did you forget to call \`Refractor.registerLanguage()\`?`
      )
    }
  }
 
  const codeProps = {className: `language-${props.language}`}
 
  if (props.inline) {
    codeProps.style = {display: 'inline'}
    codeProps.className = props.className
  }
 
  let ast = fract.highlight(props.value, props.language)
  if (props.markers && props.markers.length > 0) {
    ast = addMarkers(ast, {markers: props.markers})
  }
 
  const value = ast.length === 0 ? props.value : ast.map(mapChildren.depth(0))
 
  const code = h('code', codeProps, value)
  return props.inline ? code : h('pre', {className: props.className}, code)
}
 
Refractor.propTypes = {
  className: PropTypes.string,
  inline: PropTypes.bool,
  language: PropTypes.string.isRequired,
  value: PropTypes.string.isRequired,
  astPlugins: PropTypes.arrayOf(PropTypes.func),
  markers: PropTypes.arrayOf(
    PropTypes.oneOfType([
      PropTypes.number,
      PropTypes.shape({
        line: PropTypes.number.isRequired,
        className: PropTypes.string,
        component: PropTypes.oneOfType([PropTypes.node, PropTypes.func])
      })
    ])
  )
}
 
Refractor.defaultProps = {
  astPlugins: [],
  className: 'refractor',
  inline: false
}
 
Refractor.registerLanguage = lang => fract.register(lang)
Refractor.hasLanguage = lang => fract.registered(lang)
 
module.exports = Refractor