|
| 1 | +// Copyright 2025 V Kontakte LLC |
| 2 | +// |
| 3 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +import { memo, useContext, useMemo } from 'react'; |
| 8 | +import ReactMarkdown, { Components } from 'react-markdown'; |
| 9 | +import remarkGfm from 'remark-gfm'; |
| 10 | +import { useApiProxy } from '@/api/proxy'; |
| 11 | +import { useMetricName } from '@/hooks/useMetricName'; |
| 12 | +import { OuterInfoContextProvider } from '@/contexts/OuterInfoContextProvider'; |
| 13 | +import { OuterInfoContext } from '@/contexts/OuterInfoContext'; |
| 14 | +import cn from 'classnames'; |
| 15 | +import css from './style.module.css'; |
| 16 | + |
| 17 | +const remarkPlugins = [remarkGfm]; |
| 18 | + |
| 19 | +function usePlaceholderInfo(href: string, value: string) { |
| 20 | + const metric_name = useMetricName(true); |
| 21 | + return useMemo(() => { |
| 22 | + const valuePlaceholder = value.indexOf('⚡ ') === 0 ? value.replace('⚡ ', '') : value; |
| 23 | + return href.replace(`%7Bvalue%7D`, valuePlaceholder).replace('%7Bmetric_name%7D', metric_name); |
| 24 | + }, [href, metric_name, value]); |
| 25 | +} |
| 26 | + |
| 27 | +type MarkdownLoadUrlProps = { |
| 28 | + description?: string; |
| 29 | + href: string; |
| 30 | +}; |
| 31 | +function MarkdownLoadUrl({ description, href }: MarkdownLoadUrlProps) { |
| 32 | + const value = useContext(OuterInfoContext); |
| 33 | + const loadHref = usePlaceholderInfo(href, value); |
| 34 | + const query = useApiProxy(loadHref); |
| 35 | + if (query.isLoading) { |
| 36 | + return <p>loading...</p>; |
| 37 | + } |
| 38 | + if (!query.data) { |
| 39 | + return <p>{value || description}</p>; |
| 40 | + } |
| 41 | + return ( |
| 42 | + <ReactMarkdown remarkPlugins={remarkPlugins} components={customLinkComponents}> |
| 43 | + {query.data} |
| 44 | + </ReactMarkdown> |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +const customLinkComponents: Components = { |
| 49 | + a: function ATag({ node, ...props }) { |
| 50 | + return <a {...props} target="_blank" rel="noopener noreferrer" />; |
| 51 | + }, |
| 52 | + p: function PTag({ node, ...props }) { |
| 53 | + const otherChildren = useMemo(() => { |
| 54 | + if (Array.isArray(props.children)) { |
| 55 | + const [_1, _2, ...ch] = props.children; |
| 56 | + return ch; |
| 57 | + } |
| 58 | + return props.children; |
| 59 | + }, [props.children]); |
| 60 | + const description = useMemo( |
| 61 | + () => |
| 62 | + (node && |
| 63 | + node.children?.[1]?.type === 'element' && |
| 64 | + node.children[1].children?.[0]?.type === 'text' && |
| 65 | + node.children[1].children?.[0]?.value) || |
| 66 | + undefined, |
| 67 | + [node] |
| 68 | + ); |
| 69 | + if ( |
| 70 | + node && |
| 71 | + node.children[0].type === 'text' && |
| 72 | + node.children[0].value === '$' && |
| 73 | + node.children[1].type === 'element' && |
| 74 | + node.children[1].tagName === 'a' && |
| 75 | + typeof node.children[1].properties.href === 'string' |
| 76 | + ) { |
| 77 | + return ( |
| 78 | + <> |
| 79 | + <MarkdownLoadUrl href={node.children[1].properties.href} description={description} /> |
| 80 | + <p>{otherChildren}</p> |
| 81 | + </> |
| 82 | + ); |
| 83 | + } |
| 84 | + return <p {...props} />; |
| 85 | + }, |
| 86 | +}; |
| 87 | + |
| 88 | +const inlineMarkdownAllowedElements = ['p', 'a']; |
| 89 | + |
| 90 | +export type MarkdownRenderProps = { |
| 91 | + children?: string; |
| 92 | + className?: string; |
| 93 | + value?: string; |
| 94 | + inline?: boolean; |
| 95 | +}; |
| 96 | + |
| 97 | +export const MarkdownRender = memo(function MarkdownRender({ |
| 98 | + children = '', |
| 99 | + className, |
| 100 | + value = '', |
| 101 | + inline, |
| 102 | +}: MarkdownRenderProps) { |
| 103 | + return ( |
| 104 | + <OuterInfoContextProvider value={value}> |
| 105 | + <div className={cn(css.markdown, inline && css.markdownPreview, className)}> |
| 106 | + <ReactMarkdown |
| 107 | + remarkPlugins={remarkPlugins} |
| 108 | + components={customLinkComponents} |
| 109 | + allowedElements={inline ? inlineMarkdownAllowedElements : undefined} |
| 110 | + unwrapDisallowed={inline} |
| 111 | + > |
| 112 | + {children} |
| 113 | + </ReactMarkdown> |
| 114 | + </div> |
| 115 | + </OuterInfoContextProvider> |
| 116 | + ); |
| 117 | +}); |
0 commit comments