react-latex
Advanced tools
Comparing version 0.0.2 to 0.0.3
'use strict'; | ||
var React = window.React || require('react'); | ||
var TeX = require('react-components/js/tex.jsx'); | ||
var katex = require('katex'); | ||
var React = require('react'); | ||
var detectLatex = function detectLatex(string) { | ||
var regularExpression = arguments.length <= 1 || arguments[1] === undefined ? /\$\$[\s\S]+?\$\$|\$[\s\S]+?\$/g : arguments[1]; | ||
string = string.replace(/(<([^>]+)>)/ig, ''); | ||
var regularExpression = /\$\$[\s\S]+?\$\$|\$[\s\S]+?\$/g; | ||
var stripDollars = function stripDollars(stringToStrip) { | ||
@@ -14,4 +18,16 @@ | ||
} | ||
return stringToStrip; | ||
}; | ||
var renderLatexString = function renderLatexString(s) { | ||
try { | ||
var renderedString = katex.renderToString(s); | ||
} catch (err) { | ||
console.log('couldn`t convert string', s); | ||
return s; | ||
} | ||
return renderedString; | ||
}; | ||
var result = []; | ||
@@ -39,6 +55,21 @@ | ||
string: string, | ||
type: 'string' | ||
type: 'text' | ||
}); | ||
} | ||
return result; | ||
var processResult = function processResult(resultToProcess) { | ||
var newResult = resultToProcess.map(function (r) { | ||
if (r.type === 'text') { | ||
return r.string; | ||
} | ||
if (r.type === 'latex') { | ||
return renderLatexString(r.string); | ||
} | ||
}); | ||
return newResult.join(' '); | ||
}; | ||
return processResult(result); | ||
}; | ||
@@ -50,43 +81,11 @@ | ||
propTypes: { | ||
className: React.PropTypes.string, | ||
children: React.PropTypes.string.isRequired | ||
children: React.PropTypes.string | ||
}, | ||
getDefaultProps: function getDefaultProps() { | ||
return { | ||
className: '' | ||
children: '' | ||
}; | ||
}, | ||
getInitialState: function getInitialState() { | ||
var patternToDected = /\$\$[\s\S]+?\$\$|\$[\s\S]+?\$/g; | ||
var content = detectLatex(this.props.children, patternToDected); | ||
return { | ||
content: content | ||
}; | ||
}, | ||
render: function render() { | ||
var content = this.state.content.map(function (c) { | ||
if (c.type === 'latex') { | ||
return React.createElement( | ||
TeX, | ||
null, | ||
c.string | ||
); | ||
} else { | ||
return React.createElement( | ||
'span', | ||
null, | ||
c.string | ||
); | ||
} | ||
}); | ||
return React.createElement( | ||
'string', | ||
{ className: this.props.className }, | ||
content | ||
); | ||
return React.createElement('span', { dangerouslySetInnerHTML: { __html: latexString(this.props.children) } }); | ||
} | ||
@@ -93,0 +92,0 @@ |
{ | ||
"name": "react-latex", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "React component to render latex strings", | ||
@@ -44,4 +44,3 @@ "homepage": "", | ||
"dependencies": { | ||
"react-components": "^0.2.0" | ||
} | ||
} |
# react-latex [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] | ||
> React component to render latex strings | ||
> React component to render latex strings, based on [Katex](https://github.com/Khan/KaTeX) | ||
@@ -4,0 +4,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
5580
80
- Removedreact-components@^0.2.0
- Removedreact-components@0.2.0(transitive)