import React from 'react';
import {Link} from 'react-router-dom';
const textParser = (props) => {
const {
data,
links,
anchClass,
addBr
} = props;
const parts = data.split('{');
const addClass = (anchClass)?anchClass:'';
if(parts.length > 0) {
console.log(parts)
return (
<React.Fragment>
{
parts.map((part, idx) => {
if (idx !== 0) {
const anchName = part.split('}')[0];
if (Object.keys(links).indexOf(anchName) !== -1) {
const lastText = (part.split('}')[1])?(' ' + part.split('}')[1]):'';
return (
<React.Fragment>
{
(addBr)
? <br/>
: null
}
<Link to={links[anchName].href} target={links[anchName].target}
className={addClass}>{links[anchName].caption}</Link>
{
(addBr && lastText)
? <br/>
: null
}
{lastText}
</React.Fragment>
);
}else{
return <React.Fragment>part</React.Fragment>;
}
} else {
return <React.Fragment>{part}</React.Fragment>;
}
})
}
</React.Fragment>
);
}else{
return (
<React.Fragment>data</React.Fragment>
);
}
}
export default textParser;
s = 'STRING'
ss = "something {s} something"
ss.replace(/{(.+)}/, function() { return window[arguments[1]] })
// "something STRING something"