2021-04-14 23:56:29 +00:00
|
|
|
import styles from './text.module.css'
|
2021-06-27 22:28:30 +00:00
|
|
|
import ReactMarkdown from 'react-markdown'
|
|
|
|
import gfm from 'remark-gfm'
|
2023-07-24 21:06:26 +00:00
|
|
|
import { LightAsync as SyntaxHighlighter } from 'react-syntax-highlighter'
|
|
|
|
import atomDark from 'react-syntax-highlighter/dist/cjs/styles/prism/atom-dark'
|
2021-07-15 19:56:23 +00:00
|
|
|
import mention from '../lib/remark-mention'
|
2022-03-03 22:18:38 +00:00
|
|
|
import sub from '../lib/remark-sub'
|
2022-02-03 22:01:42 +00:00
|
|
|
import remarkDirective from 'remark-directive'
|
|
|
|
import { visit } from 'unist-util-visit'
|
2022-02-10 22:41:13 +00:00
|
|
|
import reactStringReplace from 'react-string-replace'
|
2023-07-23 15:08:43 +00:00
|
|
|
import React, { useRef, useEffect, useState, memo } from 'react'
|
2022-07-17 15:33:55 +00:00
|
|
|
import GithubSlugger from 'github-slugger'
|
2022-10-04 17:43:18 +00:00
|
|
|
import LinkIcon from '../svgs/link.svg'
|
|
|
|
import Thumb from '../svgs/thumb-up-fill.svg'
|
2022-11-15 20:51:55 +00:00
|
|
|
import { toString } from 'mdast-util-to-string'
|
2022-10-04 17:43:18 +00:00
|
|
|
import copy from 'clipboard-copy'
|
2023-07-13 00:10:01 +00:00
|
|
|
import { IMGPROXY_URL_REGEXP, IMG_URL_REGEXP } from '../lib/url'
|
|
|
|
import { extractUrls } from '../lib/md'
|
2023-07-23 15:08:43 +00:00
|
|
|
import FileMissing from '../svgs/file-warning-line.svg'
|
2023-08-15 17:55:16 +00:00
|
|
|
import { useMe } from './me'
|
2022-07-21 22:39:05 +00:00
|
|
|
|
2023-07-24 21:06:26 +00:00
|
|
|
function searchHighlighter () {
|
2022-02-03 22:01:42 +00:00
|
|
|
return (tree) => {
|
|
|
|
visit(tree, (node) => {
|
|
|
|
if (
|
|
|
|
node.type === 'textDirective' ||
|
|
|
|
node.type === 'leafDirective'
|
|
|
|
) {
|
|
|
|
if (node.name !== 'high') return
|
|
|
|
|
|
|
|
const data = node.data || (node.data = {})
|
|
|
|
data.hName = 'mark'
|
|
|
|
data.hProperties = {}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 23:56:29 +00:00
|
|
|
|
2023-08-15 17:55:16 +00:00
|
|
|
function decodeOriginalUrl (imgProxyUrl) {
|
|
|
|
const parts = imgProxyUrl.split('/')
|
|
|
|
// base64url is not a known encoding in browsers
|
|
|
|
// so we need to replace the invalid chars
|
|
|
|
const b64Url = parts[parts.length - 1].replace(/-/g, '+').replace(/_/, '/')
|
|
|
|
const originalUrl = Buffer.from(b64Url, 'base64').toString('utf-8')
|
|
|
|
return originalUrl
|
|
|
|
}
|
|
|
|
|
2022-07-17 15:33:55 +00:00
|
|
|
function Heading ({ h, slugger, noFragments, topLevel, children, node, ...props }) {
|
2022-10-04 17:43:18 +00:00
|
|
|
const [copied, setCopied] = useState(false)
|
|
|
|
const [id] = useState(noFragments ? undefined : slugger.slug(toString(node).replace(/[^\w\-\s]+/gi, '')))
|
|
|
|
|
|
|
|
const Icon = copied ? Thumb : LinkIcon
|
2022-07-17 15:33:55 +00:00
|
|
|
|
|
|
|
return (
|
2023-07-23 15:08:43 +00:00
|
|
|
<span className={styles.heading}>
|
2022-07-17 15:33:55 +00:00
|
|
|
{React.createElement(h, { id, ...props }, children)}
|
2022-10-04 17:43:18 +00:00
|
|
|
{!noFragments && topLevel &&
|
2022-11-15 20:51:55 +00:00
|
|
|
<a className={`${styles.headingLink} ${copied ? styles.copied : ''}`} href={`#${id}`}>
|
|
|
|
<Icon
|
|
|
|
onClick={() => {
|
|
|
|
const location = new URL(window.location)
|
|
|
|
location.hash = `${id}`
|
|
|
|
copy(location.href)
|
|
|
|
setTimeout(() => setCopied(false), 1500)
|
|
|
|
setCopied(true)
|
|
|
|
}}
|
|
|
|
width={18}
|
|
|
|
height={18}
|
|
|
|
className='fill-grey'
|
|
|
|
/>
|
|
|
|
</a>}
|
2023-07-23 15:08:43 +00:00
|
|
|
</span>
|
2022-07-17 15:33:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-13 00:10:01 +00:00
|
|
|
const CACHE_STATES = {
|
|
|
|
IS_LOADING: 'IS_LOADING',
|
|
|
|
IS_LOADED: 'IS_LOADED',
|
|
|
|
IS_ERROR: 'IS_ERROR'
|
|
|
|
}
|
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
// this is one of the slowest components to render
|
2023-08-23 20:30:38 +00:00
|
|
|
export default memo(function Text ({ topLevel, noFragments, nofollow, fetchOnlyImgProxy, children }) {
|
2022-02-10 22:41:13 +00:00
|
|
|
// all the reactStringReplace calls are to facilitate search highlighting
|
2022-07-17 15:33:55 +00:00
|
|
|
const slugger = new GithubSlugger()
|
2023-08-23 20:30:38 +00:00
|
|
|
fetchOnlyImgProxy ??= true
|
2022-07-17 15:33:55 +00:00
|
|
|
|
2022-11-15 20:51:55 +00:00
|
|
|
const HeadingWrapper = (props) => Heading({ topLevel, slugger, noFragments, ...props })
|
2022-07-13 23:00:48 +00:00
|
|
|
|
2023-07-13 00:10:01 +00:00
|
|
|
const imgCache = useRef({})
|
|
|
|
const [urlCache, setUrlCache] = useState({})
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-08-23 20:30:38 +00:00
|
|
|
const imgRegexp = fetchOnlyImgProxy ? IMGPROXY_URL_REGEXP : IMG_URL_REGEXP
|
2023-07-13 00:10:01 +00:00
|
|
|
const urls = extractUrls(children)
|
|
|
|
|
|
|
|
urls.forEach((url) => {
|
|
|
|
if (imgRegexp.test(url)) {
|
|
|
|
setUrlCache((prev) => ({ ...prev, [url]: CACHE_STATES.IS_LOADED }))
|
2023-08-23 20:30:38 +00:00
|
|
|
} else if (!fetchOnlyImgProxy) {
|
2023-07-25 14:14:45 +00:00
|
|
|
const img = new window.Image()
|
2023-07-13 00:10:01 +00:00
|
|
|
imgCache.current[url] = img
|
|
|
|
|
|
|
|
setUrlCache((prev) => ({ ...prev, [url]: CACHE_STATES.IS_LOADING }))
|
|
|
|
|
|
|
|
const callback = (state) => {
|
|
|
|
setUrlCache((prev) => ({ ...prev, [url]: state }))
|
|
|
|
delete imgCache.current[url]
|
|
|
|
}
|
|
|
|
img.onload = () => callback(CACHE_STATES.IS_LOADED)
|
|
|
|
img.onerror = () => callback(CACHE_STATES.IS_ERROR)
|
|
|
|
img.src = url
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
Object.values(imgCache.current).forEach((img) => {
|
|
|
|
img.onload = null
|
|
|
|
img.onerror = null
|
|
|
|
img.src = ''
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [children])
|
|
|
|
|
2021-04-14 23:56:29 +00:00
|
|
|
return (
|
2021-06-27 22:28:30 +00:00
|
|
|
<div className={styles.text}>
|
|
|
|
<ReactMarkdown
|
|
|
|
components={{
|
2022-07-17 15:33:55 +00:00
|
|
|
h1: (props) => HeadingWrapper({ h: topLevel ? 'h1' : 'h3', ...props }),
|
|
|
|
h2: (props) => HeadingWrapper({ h: topLevel ? 'h2' : 'h4', ...props }),
|
|
|
|
h3: (props) => HeadingWrapper({ h: topLevel ? 'h3' : 'h5', ...props }),
|
|
|
|
h4: (props) => HeadingWrapper({ h: topLevel ? 'h4' : 'h6', ...props }),
|
|
|
|
h5: (props) => HeadingWrapper({ h: topLevel ? 'h5' : 'h6', ...props }),
|
|
|
|
h6: (props) => HeadingWrapper({ h: 'h6', ...props }),
|
2021-06-27 22:28:30 +00:00
|
|
|
table: ({ node, ...props }) =>
|
2023-07-23 15:08:43 +00:00
|
|
|
<span className='table-responsive'>
|
2021-06-27 22:28:30 +00:00
|
|
|
<table className='table table-bordered table-sm' {...props} />
|
2023-07-23 15:08:43 +00:00
|
|
|
</span>,
|
|
|
|
p: ({ children, ...props }) => <div className={styles.p} {...props}>{children}</div>,
|
2023-07-27 17:10:05 +00:00
|
|
|
code ({ node, inline, className, children, style, ...props }) {
|
2021-06-27 22:28:30 +00:00
|
|
|
return !inline
|
|
|
|
? (
|
2023-07-27 17:10:05 +00:00
|
|
|
<SyntaxHighlighter showLineNumbers style={atomDark} PreTag='div' {...props}>
|
2022-02-10 22:41:13 +00:00
|
|
|
{reactStringReplace(String(children).replace(/\n$/, ''), /:high\[([^\]]+)\]/g, (match, i) => {
|
|
|
|
return match
|
|
|
|
}).join('')}
|
2021-06-27 22:28:30 +00:00
|
|
|
</SyntaxHighlighter>
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
<code className={className} style={atomDark} {...props}>
|
2022-02-10 22:41:13 +00:00
|
|
|
{reactStringReplace(String(children), /:high\[([^\]]+)\]/g, (match, i) => {
|
|
|
|
return <mark key={`mark-${match}`}>{match}</mark>
|
|
|
|
})}
|
2021-06-27 22:28:30 +00:00
|
|
|
</code>
|
|
|
|
)
|
2021-09-02 18:11:27 +00:00
|
|
|
},
|
2022-02-10 22:41:13 +00:00
|
|
|
a: ({ node, href, children, ...props }) => {
|
2022-08-10 15:06:31 +00:00
|
|
|
if (children?.some(e => e?.props?.node?.tagName === 'img')) {
|
|
|
|
return <>{children}</>
|
|
|
|
}
|
|
|
|
|
2023-07-13 00:10:01 +00:00
|
|
|
if (urlCache[href] === CACHE_STATES.IS_LOADED) {
|
2023-08-23 20:30:38 +00:00
|
|
|
return <ZoomableImage topLevel={topLevel} useClickToLoad={fetchOnlyImgProxy} {...props} src={href} />
|
2023-07-13 00:10:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 15:06:31 +00:00
|
|
|
// map: fix any highlighted links
|
2022-07-17 15:33:55 +00:00
|
|
|
children = children?.map(e =>
|
|
|
|
typeof e === 'string'
|
2022-11-15 20:51:55 +00:00
|
|
|
? reactStringReplace(e, /:high\[([^\]]+)\]/g, (match, i) => {
|
2023-07-25 14:14:45 +00:00
|
|
|
return <mark key={`mark-${match}-${i}`}>{match}</mark>
|
|
|
|
})
|
2022-11-15 20:51:55 +00:00
|
|
|
: e)
|
2022-02-10 22:41:13 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
/* eslint-disable-next-line */
|
|
|
|
<a
|
|
|
|
target='_blank' rel={nofollow ? 'nofollow' : 'noreferrer'}
|
|
|
|
href={reactStringReplace(href, /:high%5B([^%5D]+)%5D/g, (match, i) => {
|
|
|
|
return match
|
|
|
|
}).join('')} {...props}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</a>
|
|
|
|
)
|
2022-07-13 23:00:48 +00:00
|
|
|
},
|
2023-08-23 20:30:38 +00:00
|
|
|
img: ({ node, ...props }) => {
|
|
|
|
return <ZoomableImage topLevel={topLevel} useClickToLoad={fetchOnlyImgProxy} {...props} />
|
|
|
|
}
|
2021-06-27 22:28:30 +00:00
|
|
|
}}
|
2023-07-24 21:06:26 +00:00
|
|
|
remarkPlugins={[gfm, mention, sub, remarkDirective, searchHighlighter]}
|
2021-06-27 22:28:30 +00:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</ReactMarkdown>
|
|
|
|
</div>
|
2021-04-14 23:56:29 +00:00
|
|
|
)
|
2023-07-23 15:08:43 +00:00
|
|
|
})
|
2022-07-13 23:00:48 +00:00
|
|
|
|
2023-08-15 17:55:16 +00:00
|
|
|
function ClickToLoad ({ children }) {
|
|
|
|
const [clicked, setClicked] = useState(false)
|
|
|
|
return clicked ? children : <div className='m-1 fst-italic pointer text-muted' onClick={() => setClicked(true)}>click to load image</div>
|
|
|
|
}
|
|
|
|
|
2023-08-23 20:30:38 +00:00
|
|
|
export function ZoomableImage ({ src, topLevel, useClickToLoad, ...props }) {
|
2023-08-15 17:55:16 +00:00
|
|
|
const me = useMe()
|
2023-07-23 15:08:43 +00:00
|
|
|
const [err, setErr] = useState()
|
2023-08-15 17:55:16 +00:00
|
|
|
const [imgSrc, setImgSrc] = useState(src)
|
|
|
|
const [isImgProxy, setIsImgProxy] = useState(IMGPROXY_URL_REGEXP.test(src))
|
2022-07-13 23:00:48 +00:00
|
|
|
const defaultMediaStyle = {
|
|
|
|
maxHeight: topLevel ? '75vh' : '25vh',
|
|
|
|
cursor: 'zoom-in'
|
|
|
|
}
|
2023-08-23 20:30:38 +00:00
|
|
|
useClickToLoad ??= true
|
2022-07-13 23:00:48 +00:00
|
|
|
|
|
|
|
// if image changes we need to update state
|
|
|
|
const [mediaStyle, setMediaStyle] = useState(defaultMediaStyle)
|
|
|
|
useEffect(() => {
|
|
|
|
setMediaStyle(defaultMediaStyle)
|
2023-07-23 15:08:43 +00:00
|
|
|
setErr(null)
|
2022-07-13 23:00:48 +00:00
|
|
|
}, [src])
|
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
if (!src) return null
|
|
|
|
if (err) {
|
2023-08-15 17:55:16 +00:00
|
|
|
if (!isImgProxy) {
|
|
|
|
return (
|
|
|
|
<span className='d-flex align-items-baseline text-warning-emphasis fw-bold pb-1'>
|
|
|
|
<FileMissing width={18} height={18} className='fill-warning me-1 align-self-center' />
|
|
|
|
image error
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const originalUrl = decodeOriginalUrl(src)
|
|
|
|
setImgSrc(originalUrl)
|
|
|
|
setErr(null)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
setErr(err)
|
|
|
|
}
|
|
|
|
// always set to false since imgproxy returned error
|
|
|
|
setIsImgProxy(false)
|
2022-07-13 23:00:48 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 17:55:16 +00:00
|
|
|
const img = (
|
2022-07-17 15:33:55 +00:00
|
|
|
<img
|
|
|
|
className={topLevel ? styles.topLevel : undefined}
|
|
|
|
style={mediaStyle}
|
2023-08-15 17:55:16 +00:00
|
|
|
src={imgSrc}
|
2023-07-23 15:08:43 +00:00
|
|
|
onClick={() => {
|
|
|
|
if (mediaStyle.cursor === 'zoom-in') {
|
|
|
|
setMediaStyle({
|
|
|
|
width: '100%',
|
|
|
|
cursor: 'zoom-out'
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setMediaStyle(defaultMediaStyle)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onError={() => setErr(true)}
|
2022-07-17 15:33:55 +00:00
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
)
|
2023-08-15 17:55:16 +00:00
|
|
|
|
|
|
|
return (
|
2023-08-23 20:30:38 +00:00
|
|
|
(!me || !me.clickToLoadImg || isImgProxy || !useClickToLoad)
|
2023-08-15 17:55:16 +00:00
|
|
|
? img
|
|
|
|
: <ClickToLoad>{img}</ClickToLoad>
|
|
|
|
|
|
|
|
)
|
2022-07-13 23:00:48 +00:00
|
|
|
}
|