-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathMarkdownPage.js
148 lines (133 loc) · 4.45 KB
/
MarkdownPage.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* @emails react-core
* @flow
*/
import Container from 'components/Container';
import Flex from 'components/Flex';
import MarkdownHeader from 'components/MarkdownHeader';
import NavigationFooter from 'templates/components/NavigationFooter';
import React from 'react';
import StickyResponsiveSidebar from 'components/StickyResponsiveSidebar';
import TitleAndMetaTags from 'components/TitleAndMetaTags';
import findSectionForPath from 'utils/findSectionForPath';
import toCommaSeparatedList from 'utils/toCommaSeparatedList';
import {sharedStyles} from 'theme';
import createOgUrl from 'utils/createOgUrl';
import type {Node} from 'types';
type Props = {
authors: Array<string>,
createLink: Function, // TODO: Add better flow type once we Flow-type createLink
date?: string,
enableScrollSync?: boolean,
ogDescription: string,
location: Location,
markdownRemark: Node,
sectionList: Array<Object>, // TODO: Add better flow type once we have the Section component
titlePostfix: string,
};
const getPageById = (sectionList: Array<Object>, templateFile: ?string) => {
if (!templateFile) {
return null;
}
const sectionItems = sectionList.map(section => section.items);
const flattenedSectionItems = [].concat.apply([], sectionItems);
const linkId = templateFile.replace('.html', '');
return flattenedSectionItems.find(item => item.id === linkId);
};
const MarkdownPage = ({
authors = [],
createLink,
date,
enableScrollSync,
ogDescription,
location,
markdownRemark,
sectionList,
titlePostfix = '',
}: Props) => {
const hasAuthors = authors.length > 0;
const titlePrefix = markdownRemark.frontmatter.title || '';
const prev = getPageById(sectionList, markdownRemark.frontmatter.prev);
const next = getPageById(sectionList, markdownRemark.frontmatter.next);
return (
<Flex
direction="column"
grow="1"
shrink="0"
halign="stretch"
css={{
width: '100%',
flex: '1 0 auto',
position: 'relative',
zIndex: 0,
}}>
<TitleAndMetaTags
ogDescription={ogDescription}
ogUrl={createOgUrl(markdownRemark.fields.slug)}
title={`${titlePrefix}${titlePostfix}`}
/>
<div css={{flex: '1 0 auto'}}>
<Container>
<div css={sharedStyles.articleLayout.container}>
<Flex type="article" direction="column" grow="1" halign="stretch">
<MarkdownHeader title={titlePrefix} />
{(date || hasAuthors) && (
<div css={{marginTop: 15}}>
{date}{' '}
{hasAuthors && (
<span>
by{' '}
{toCommaSeparatedList(authors, author => (
<a
css={sharedStyles.link}
href={author.frontmatter.url}
key={author.frontmatter.name}>
{author.frontmatter.name}
</a>
))}
</span>
)}
</div>
)}
<div css={sharedStyles.articleLayout.content}>
<div
css={[sharedStyles.markdown]}
dangerouslySetInnerHTML={{__html: markdownRemark.html}}
/>
{markdownRemark.fields.path && (
<div css={{marginTop: 80}}>
<a
css={sharedStyles.articleLayout.editLink}
href={`https://github.com/reactjs/pt-br.reactjs.org/tree/master/${
markdownRemark.fields.path
}`}>
Edite esta página
</a>
</div>
)}
</div>
</Flex>
<div css={sharedStyles.articleLayout.sidebar}>
<StickyResponsiveSidebar
enableScrollSync={enableScrollSync}
createLink={createLink}
defaultActiveSection={findSectionForPath(
location.pathname,
sectionList,
)}
location={location}
sectionList={sectionList}
/>
</div>
</div>
</Container>
</div>
{(next || prev) && (
<NavigationFooter location={location} next={next} prev={prev} />
)}
</Flex>
);
};
export default MarkdownPage;