From 109aa7d321edcea37cf0ec2236883b940d47464e Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 10 Feb 2022 16:41:13 -0600 Subject: [PATCH] prevent search highlighting from affecting link/code markdown --- components/text.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/components/text.js b/components/text.js index 755510e0..3b239c1f 100644 --- a/components/text.js +++ b/components/text.js @@ -7,6 +7,7 @@ import { atomDark } from 'react-syntax-highlighter/dist/cjs/styles/prism' import mention from '../lib/remark-mention' import remarkDirective from 'remark-directive' import { visit } from 'unist-util-visit' +import reactStringReplace from 'react-string-replace' function myRemarkPlugin () { return (tree) => { @@ -26,6 +27,7 @@ function myRemarkPlugin () { } export default function Text ({ nofollow, children }) { + // all the reactStringReplace calls are to facilitate search highlighting return (
- {String(children).replace(/\n$/, '')} + {reactStringReplace(String(children).replace(/\n$/, ''), /:high\[([^\]]+)\]/g, (match, i) => { + return match + }).join('')} ) : ( - {children} + {reactStringReplace(String(children), /:high\[([^\]]+)\]/g, (match, i) => { + return {match} + })} ) }, - a: ({ node, ...props }) => + a: ({ node, href, children, ...props }) => { + children = children.map(e => typeof e === 'string' + ? reactStringReplace(e, /:high\[([^\]]+)\]/g, (match, i) => { + return {match} + }) + : e) + console.log(href) + + return ( + /* eslint-disable-next-line */ + { + return match + }).join('')} {...props} + > + {children} + + ) + } }} remarkPlugins={[gfm, mention, remarkDirective, myRemarkPlugin]} >