diff --git a/components/text.js b/components/text.js index fe70f0a2..43b9a387 100644 --- a/components/text.js +++ b/components/text.js @@ -17,6 +17,7 @@ import ItemPopover from './item-popover' import classNames from 'classnames' import { CarouselProvider, useCarousel } from './carousel' import rehypeSN from '@/lib/rehype-sn' +import remarkUnicode from '@/lib/remark-unicode' import Embed from './embed' import remarkMath from 'remark-math' import rehypeMathjax from 'rehype-mathjax' @@ -33,7 +34,7 @@ const rehypeSNStyled = () => rehypeSN({ }] }) -const remarkPlugins = [gfm, [remarkMath, { singleDollarTextMath: false }]] +const remarkPlugins = [gfm, remarkUnicode, [remarkMath, { singleDollarTextMath: false }]] const rehypePlugins = [rehypeSNStyled, rehypeMathjax] export function SearchText ({ text }) { diff --git a/lib/remark-unicode.js b/lib/remark-unicode.js new file mode 100644 index 00000000..19c830ad --- /dev/null +++ b/lib/remark-unicode.js @@ -0,0 +1,31 @@ +import { visit } from 'unist-util-visit' + +export default function remarkFilterUnicode () { + return (tree) => { + try { + visit(tree, 'paragraph', (node) => { + node.children = node.children.map(child => { + if (child.type !== 'inlineMath') return child + + // if inline math contains currency symbols, rehypeMathjax will throw + // see https://github.com/stackernews/stacker.news/issues/1525 + // and https://github.com/stackernews/stacker.news/pull/1526 + + let { hChildren } = child.data + hChildren = hChildren.map(child2 => { + return { ...child2, value: filterUnicode(child2.value) } + }) + child.data.hChildren = hChildren + + return { ...child, value: filterUnicode(child.value) } + }) + }) + } catch (err) { + console.error(err) + } + } +} + +function filterUnicode (text) { + return text.replace(/\p{Sc}/u, '') +}