import React, {Component, PureComponent} from 'react'; import {format_time,Time,TitleLine} from './infrastructure/widgets'; import {THUHOLE_API_ROOT} from './flows_api'; import HtmlToReact from 'html-to-react' import './Common.css'; import { URL_PID_RE, URL_RE, PID_RE, NICKNAME_RE, split_text } from './text_splitter'; import renderMd from './Markdown' export {format_time,Time,TitleLine}; export const API_BASE=THUHOLE_API_ROOT+'services/thuhole'; // https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex function escape_regex(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } export function build_highlight_re(txt,split,option='g') { return txt ? new RegExp(`(${txt.split(split).filter((x)=>!!x).map(escape_regex).join('|')})`,option) : /^$/g; } export function ColoredSpan(props) { return ( {props.children} ) } function normalize_url(url) { return /^https?:\/\//.test(url) ? url : 'http://'+url; } export class HighlightedText extends PureComponent { render() { return (
{this.props.parts.map((part,idx)=>{
let [rule,p]=part;
return (
{
rule==='url_pid' ? /## :
rule==='url' ? {p} :
rule==='pid' ? {e.preventDefault(); this.props.show_pid(p.substring(1));}}>{p} :
rule==='nickname' ? {p} :
rule==='search' ? {p} :
p
}
);
})}
)
}
}
// props: text, show_pid, color_picker
export class HighlightedMarkdown extends Component {
render() {
const props = this.props
const processDefs = new HtmlToReact.ProcessNodeDefinitions(React)
const processInstructions = [
{
shouldProcessNode (node) {
return node.type === 'text' // pid, nickname, search
},
processNode (node) {
const originalText = node.data
const splitted = split_text(originalText, [
['url_pid', URL_PID_RE],
['url',URL_RE],
['pid',PID_RE],
['nickname',NICKNAME_RE],
])
return (
<>
{splitted.map(([rule, p], idx) => {
return (
{
rule==='url_pid' ? /## :
rule==='url' ? {p} :
rule==='pid' ? {e.preventDefault(); props.show_pid(p.substring(1));}}>{p} :
rule==='nickname' ?