fix markdown inline code and headings
This commit is contained in:
		
							parent
							
								
									67ec82b6d9
								
							
						
					
					
						commit
						302f3459a1
					
				@ -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 'h6'
 | 
				
			||||||
        return topLevel ? 'h3' : 'h6'
 | 
					  }, [node?.tagName, topLevel])
 | 
				
			||||||
      case 4:
 | 
					 | 
				
			||||||
        return topLevel ? 'h4' : 'h6'
 | 
					 | 
				
			||||||
      case 5:
 | 
					 | 
				
			||||||
        return topLevel ? 'h5' : 'h6'
 | 
					 | 
				
			||||||
      case 6:
 | 
					 | 
				
			||||||
        return 'h6'
 | 
					 | 
				
			||||||
      default:
 | 
					 | 
				
			||||||
        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>
 | 
				
			||||||
 | 
				
			|||||||
@ -16,7 +16,7 @@ export const INVOICE = gql`
 | 
				
			|||||||
      isHeld
 | 
					      isHeld
 | 
				
			||||||
      comment
 | 
					      comment
 | 
				
			||||||
      lud18Data
 | 
					      lud18Data
 | 
				
			||||||
      preimage
 | 
					      confirmedPreimage
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }`
 | 
					  }`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										13
									
								
								lib/md.js
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								lib/md.js
									
									
									
									
									
								
							@ -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, {
 | 
				
			||||||
 | 
				
			|||||||
@ -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,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user