From 19e5aa3d0b069225d50b862029b8dd534fb1f49f Mon Sep 17 00:00:00 2001 From: "plebeius.eth" Date: Thu, 22 Jun 2023 17:29:46 +0200 Subject: [PATCH] add p/link and u/link quote functionality --- src/components/Post.jsx | 131 +++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 63 deletions(-) diff --git a/src/components/Post.jsx b/src/components/Post.jsx index be6fde1a..5935cabe 100644 --- a/src/components/Post.jsx +++ b/src/components/Post.jsx @@ -43,72 +43,77 @@ const Post = ({ content, postQuoteOnClick, postQuoteOnOver, postQuoteOnLeave, po const createQuotelink = (children, postQuoteOnClick, postQuoteOnOver, postQuoteOnLeave, postQuoteRef) => { - const regexC = /(c\/[A-Za-z0-9]{46}|c\/[A-Za-z0-9]{12})/g; - const regexP = /(p\/([A-Za-z0-9]{52}|[A-Za-z0-9-.]*\.eth))/g; - const regexU = /(u\/([A-Za-z0-9]{52}|[A-Za-z0-9-.]*\.eth))/g; - const regexPC = /(p\/([A-Za-z0-9]{52}|[A-Za-z0-9-.]*\.eth)\/c\/[A-Za-z0-9]{46})/g; - - const regexArray = [regexU, regexPC, regexP, regexC]; - let parts = [children]; - - regexArray.forEach(regex => { - parts = parts.flatMap((child, i) => { - if (typeof child === 'string') { - const newParts = []; - let match; - let lastIndex = 0; - - while ((match = regex.exec(child)) !== null) { - const matchedText = match[0]; - const index = match.index; - - if (index > lastIndex) { - newParts.push(child.substring(lastIndex, index)); - } - - const cid = matchedText.replace('c/', ''); - const linkRef = React.createRef(); - - newParts.push( - {}} - ref={linkRef} - setRefAndCid={(ref) => { - if (typeof postQuoteRef === 'function') { - postQuoteRef(cid, ref); - } - }} - onClick={() => {postQuoteOnClick(cid)}} - onMouseOver={() => { - postQuoteOnOver(cid); - }} - onMouseLeave={() => { - postQuoteOnLeave(); - }} - > - {matchedText} - - ); - - - lastIndex = index + matchedText.length; - } - - if (lastIndex < child.length) { - newParts.push(child.substring(lastIndex)); - } - - return newParts; - } else { - return child; + const patternC = "(c/[A-Za-z0-9]{46}|c/[A-Za-z0-9]{12})"; + const patternP = "(p/([A-Za-z0-9]{52}|[A-Za-z0-9-.]*\\.eth))"; + const patternU = "(u/([A-Za-z0-9]{52}|[A-Za-z0-9-.]*\\.eth))"; + const patternPC = "(p/([A-Za-z0-9]{52}|[A-Za-z0-9-.]*\\.eth)/c/[A-Za-z0-9]{46})"; + + const regex = new RegExp(`${patternC}|${patternPC}|${patternU}|${patternP}`, 'g'); + + return children.flatMap((child, i) => { + if (typeof child !== 'string') { + return child; + } + + const parts = []; + let match; + let lastIndex = 0; + + while ((match = regex.exec(child)) !== null) { + const matchedText = match[0]; + const index = match.index; + + if (index > lastIndex) { + parts.push(child.substring(lastIndex, index)); } - }); + + const cid = matchedText.replace('c/', ''); + const linkRef = React.createRef(); + + let linkTo = () => {}; + const linkTarget = matchedText.startsWith('u/') ? "_blank" : "_self"; + + if (matchedText.startsWith('u/')) { + linkTo = `https://plebbitapp.eth.limo/#/${cid}`; + } else if (matchedText.startsWith('p/') || matchedText.startsWith('p/')) { + linkTo = `/${matchedText}`; + } + + parts.push( + { + if (typeof postQuoteRef === 'function') { + postQuoteRef(cid, ref); + } + }} + onClick={() => {postQuoteOnClick(cid)}} + onMouseOver={() => { + postQuoteOnOver(cid); + }} + onMouseLeave={() => { + postQuoteOnLeave(); + }} + > + {matchedText} + + ); + + lastIndex = index + matchedText.length; + } + + if (lastIndex < child.length) { + parts.push(child.substring(lastIndex)); + } + + return parts; }); - - return parts; }; + return (