-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMarkdown.js
117 lines (109 loc) · 2.81 KB
/
Markdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import ReactMarkdown from 'react-markdown'
import styled from 'styled-components'
import { useEffect, useState } from 'react'
import ProgressiveImage from './markdown/ProgressiveImage'
export default function Markdown({ data }) {
const [ headings, setHeadings ] = useState([])
useEffect(() => {
const hTagsCollection = document.querySelectorAll('h2, h3, h4, h5, h6')
const hTagsArr = [].slice.call(hTagsCollection)
console.log(hTagsCollection, hTagsArr)
setHeadings(hTagsArr)
if (hTagsArr.length !== 0) {
hTagsArr.map(el => el.setAttribute('id', el.textContent))
}
},[])
return (
<>
<Header>
<h1>{data.title}</h1>
<time dateTime={data.published_at.slice(0, 10)}>
{new Date(data.published_at).toDateString().slice(4)}
</time>
<span>
by {data.created_by.firstname} {data.created_by.lastname}
</span>
</Header>
<Figure>
<ProgressiveImage
preview={data.image[0].formats.thumbnail.url}
smallImage={data.image[0].formats.small.url}
mediumImage={data.image[0].formats.medium.url}
largeImage={data.image[0].formats.large.url}
sourceImage={data.image[0].url}
alt={data.image[0].alternativeText}
/>
<figcaption><em>{data.image[0].caption}</em></figcaption>
</Figure>
{
headings.length === 0 ? null :
<Nav className="contents">
<p>Contents</p>
<ul>
{
headings.map(li =>
<li className={li.nodeName} key={li.textContent}>
<a href={`#${li.textContent}`}>{li.textContent}</a>
</li>
)
}
</ul>
</Nav>
}
<ReactMarkdown
source={data.content}
escapeHtml={false}
transformImageUri={uri =>
uri.startsWith('http') ? uri : `${process.env.NEXT_PUBLIC_HCMS_API_URL}${uri}`
}
/>
</>
)
}
const Header = styled.header`
align-self: center;
margin-bottom: 1em;
display: flex;
flex-direction: column;
align-items: center;
> :first-child {
font-size: 2em;
margin-left: 1em;
margin-right: 1em;
}
`
const Figure = styled.figure`
margin: 0;
> img {
width: 100%;
height: auto;
}
> figcaption {
text-align: center;
}
`
const Nav = styled.nav`
border: 2px solid black;
border-radius: 15px;
padding: 1em;
margin-top: 1em;
> p {
margin-top: 0;
}
> ul {
list-style-type: none;
padding-left: 1em;
> .H3 {
padding-left: .5em;
}
> .H4 {
padding-left: 1em;
}
> .H5 {
padding-left: 1.5em;
}
> .H6 {
padding-left: 2em;
}
}
`