1
- import MDRenderer from '../lib/MDRenderer'
2
- import React , { useEffect } from 'react'
1
+ import React , { useCallback , useEffect } from 'react'
3
2
import Mark from 'mark.js'
4
- import Details from './Details.tsx'
5
- import Summary from './Summary.tsx'
6
- import { SmartLink } from './Link'
7
- import SEO from './Seo'
8
3
import clsx from 'clsx'
4
+
5
+ import MDRenderer from '../lib/MDRenderer'
6
+ import Details from './Details'
7
+ import Summary from './Summary'
8
+ import { SmartLink , SmartLinkProps } from './Link'
9
+ import SEO from './Seo'
9
10
import StyledLayout from './StyledLayout'
11
+ import { DeepRequiredNonNull , DeepWriteable , Nullable } from '../types/common'
12
+ import { ReactiveHastComponents } from '../lib/reactiveHast'
13
+
14
+ export interface MdxProps {
15
+ data : DeepWriteable < DeepRequiredNonNull < GatsbyTypes . DocQuery > >
16
+ location : Location & { state : Nullable < { searchKey : string } > } ;
17
+ }
10
18
11
- function Mdx ( { data : { mdx } , location } ) {
12
- // console.log(mdx);
13
- // const headingTitle = mdx.headings[0] && mdx.headings[0].value
14
- const title = mdx . slug === '/' ? null : mdx . frontmatter . title
19
+ const Mdx : React . FC < MdxProps > = ( { data : { mdx } , location } ) => {
20
+ const title = mdx . fields . slug === '/' ? '' : mdx . frontmatter . title
15
21
const description = mdx . frontmatter . description || mdx . excerpt
16
- const authors = mdx . frontmatter . author || null
17
- const tags = mdx . frontmatter . tags || null
18
- const noMeta = mdx . frontmatter . noMeta === 'true' || false
19
- const noComment = mdx . frontmatter . noComment === 'true' || false
20
- const noEdit = mdx . frontmatter . noEdit === 'true' || false
22
+ const authors = mdx . frontmatter . author || ''
23
+ const tags = mdx . frontmatter . tags || [ ]
24
+ const noMeta = mdx . frontmatter . noMeta || false
25
+ const noComment = mdx . frontmatter . noComment || false
26
+ const noEdit = false
21
27
const toc = mdx . toc || null
22
28
const relativePath = mdx . parent . relativePath || ''
23
29
const modifiedTime = mdx . parent . modifiedTime || ''
@@ -26,21 +32,21 @@ function Mdx ({ data: { mdx }, location }) {
26
32
const dateModified = mdx . parent . changeTime || ''
27
33
const isIndex = mdx . fields . isIndex
28
34
29
- const highlightNode = ( tagName , isHighlight ) => {
30
- const mainNodes = document . getElementsByTagName ( 'main' )
31
- const nodes = mainNodes [ 0 ] . getElementsByTagName ( tagName )
32
- const children = [ ...nodes ]
33
- children . forEach ( ( node ) => {
34
- const instance = new Mark ( node )
35
+ const highlightNode = useCallback ( ( tagName : string , isHighlight : boolean ) : void => {
36
+ if ( location . state ?. searchKey ) {
37
+ const mainNodes = document . getElementsByTagName ( 'main' )
38
+ const nodes = mainNodes [ 0 ] . querySelectorAll ( tagName )
39
+ const instance = new Mark ( nodes )
35
40
instance [ isHighlight ? 'mark' : 'unmark' ] (
36
41
location . state . searchKey ,
37
42
{
38
43
exclude : [ 'span' ] ,
39
44
} )
40
- } )
41
- }
45
+ }
46
+ } , [ location . state ?. searchKey ] )
47
+
42
48
useEffect ( ( ) => {
43
- if ( location ? .state ?. searchKey ) {
49
+ if ( location . state ?. searchKey ) {
44
50
highlightNode ( 'h1' , true )
45
51
highlightNode ( 'h2' , true )
46
52
highlightNode ( 'h3' , true )
@@ -53,27 +59,25 @@ function Mdx ({ data: { mdx }, location }) {
53
59
highlightNode ( 'p' , false )
54
60
} , 5000 )
55
61
}
56
- } , [ ] )
62
+ } , [ highlightNode , location . state ?. searchKey ] )
57
63
58
- function LinkGetter ( ) {
59
- return function TooltipLink ( props ) {
60
- return < SmartLink { ...props } pathname = { location . pathname } isIndex = { isIndex } tooltip = { true } />
61
- }
62
- }
64
+ const LinkGetter : React . FC < SmartLinkProps > = ( props ) =>
65
+ < SmartLink { ...props } pathname = { location . pathname } isIndex = { isIndex } tooltip = { true } />
63
66
64
- function InlineCode ( { className, children, ...props } ) {
65
- return < code { ...props } className = { clsx ( className , 'inline-code' ) } > { children } </ code >
67
+ const InlineCode : React . FC < { className : string , [ key : string ] : any } > = ( props ) => {
68
+ const { className, children, ...others } = props
69
+ return < code { ...others } className = { clsx ( className , 'inline-code' ) } > { children } </ code >
66
70
}
67
71
68
72
const myComponents = {
69
73
details : Details ,
70
74
summary : Summary ,
71
- a : LinkGetter ( ) ,
72
- inlineCode : InlineCode ,
75
+ a : LinkGetter ,
73
76
inlinecode : InlineCode ,
74
- }
77
+ } as ReactiveHastComponents
78
+
79
+ const isWIP = wordCount === 0 || ( tags ?. findIndex ( ( x : string ) => x === 'WIP' ) >= 0 )
75
80
76
- const isWIP = wordCount === 0 || ( tags ?. findIndex ( x => x === 'WIP' ) >= 0 )
77
81
return (
78
82
< StyledLayout
79
83
location = { location }
0 commit comments