fix markdown inline code and headings

This commit is contained in:
keyan 2023-10-23 17:14:37 -05:00
parent 67ec82b6d9
commit 302f3459a1
4 changed files with 27 additions and 27 deletions

View File

@ -15,6 +15,7 @@ import copy from 'clipboard-copy'
import ZoomableImage, { decodeOriginalUrl } from './image' import ZoomableImage, { decodeOriginalUrl } from './image'
import { IMGPROXY_URL_REGEXP } from '../lib/url' import { IMGPROXY_URL_REGEXP } from '../lib/url'
import reactStringReplace from 'react-string-replace' import reactStringReplace from 'react-string-replace'
import { rehypeInlineCodeProperty } from '../lib/md'
export function SearchText ({ text }) { export function SearchText ({ text }) {
return ( return (
@ -28,28 +29,18 @@ export function SearchText ({ text }) {
) )
} }
function Heading ({ level, slugger, noFragments, topLevel, children, node, ...props }) { function Heading ({ slugger, noFragments, topLevel, children, node, ...props }) {
const [copied, setCopied] = useState(false) const [copied, setCopied] = useState(false)
const id = useMemo(() => const id = useMemo(() =>
noFragments ? undefined : slugger.slug(toString(node).replace(/[^\w\-\s]+/gi, '')), [node, noFragments, slugger]) noFragments ? undefined : slugger.slug(toString(node).replace(/[^\w\-\s]+/gi, '')), [node, noFragments, slugger])
const h = useMemo(() => { const h = useMemo(() => {
switch (level) { if (topLevel) return node?.TagName
case 1:
return topLevel ? 'h1' : 'h4' const h = parseInt(node?.tagName)
case 2: if (h < 4) return `h${h + 3}`
return topLevel ? 'h2' : 'h5'
case 3:
return topLevel ? 'h3' : 'h6'
case 4:
return topLevel ? 'h4' : 'h6'
case 5:
return topLevel ? 'h5' : 'h6'
case 6:
return 'h6' return 'h6'
default: }, [node?.tagName, topLevel])
return 'h6'
}
}, [level, topLevel])
const Icon = copied ? Thumb : LinkIcon const Icon = copied ? Thumb : LinkIcon
return ( return (
@ -98,7 +89,7 @@ export default memo(function Text ({ nofollow, imgproxyUrls, children, tab, ...o
) )
}, []) }, [])
const P = useCallback(({ children, ...props }) => <div className={styles.p} {...props}>{children}</div>, []) const P = useCallback(({ children, node, ...props }) => <div className={styles.p} {...props}>{children}</div>, [])
const Img = useCallback(({ node, src, ...props }) => { const Img = useCallback(({ node, src, ...props }) => {
const url = IMGPROXY_URL_REGEXP.test(src) ? decodeOriginalUrl(src) : src const url = IMGPROXY_URL_REGEXP.test(src) ? decodeOriginalUrl(src) : src
@ -110,12 +101,7 @@ export default memo(function Text ({ nofollow, imgproxyUrls, children, tab, ...o
<div className={styles.text}> <div className={styles.text}>
<ReactMarkdown <ReactMarkdown
components={{ components={{
h1: (props) => <Heading {...props} {...outerProps} slugger={slugger.current} />, heading: (props) => <Heading {...props} {...outerProps} slugger={slugger.current} />,
h2: (props) => <Heading {...props} {...outerProps} slugger={slugger.current} />,
h3: (props) => <Heading {...props} {...outerProps} slugger={slugger.current} />,
h4: (props) => <Heading {...props} {...outerProps} slugger={slugger.current} />,
h5: (props) => <Heading {...props} {...outerProps} slugger={slugger.current} />,
h6: (props) => <Heading {...props} {...outerProps} slugger={slugger.current} />,
table: Table, table: Table,
p: P, p: P,
code: Code, code: Code,
@ -157,6 +143,7 @@ export default memo(function Text ({ nofollow, imgproxyUrls, children, tab, ...o
img: Img img: Img
}} }}
remarkPlugins={[gfm, mention, sub]} remarkPlugins={[gfm, mention, sub]}
rehypePlugins={[rehypeInlineCodeProperty]}
> >
{children} {children}
</ReactMarkdown> </ReactMarkdown>

View File

@ -16,7 +16,7 @@ export const INVOICE = gql`
isHeld isHeld
comment comment
lud18Data lud18Data
preimage confirmedPreimage
} }
}` }`

View File

@ -19,6 +19,19 @@ export function mdHas (md, test) {
return found return found
} }
export function rehypeInlineCodeProperty () {
return function (tree) {
console.log(tree)
visit(tree, { tagName: 'code' }, function (node, index, parent) {
if (parent && parent.tagName === 'pre') {
node.properties.inline = false
} else {
node.properties.inline = true
}
})
}
}
export function extractUrls (md) { export function extractUrls (md) {
if (!md) return [] if (!md) return []
const tree = fromMarkdown(md, { const tree = fromMarkdown(md, {

View File

@ -7,7 +7,7 @@ const subRegex = new RegExp(
'gi' 'gi'
) )
export default function mention (options) { export default function sub (options) {
return function transformer (tree) { return function transformer (tree) {
findAndReplace( findAndReplace(
tree, tree,