Skip to content

Instantly share code, notes, and snippets.

@JacobKnaack
Last active February 8, 2019 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobKnaack/ad5e6c1d92a70d42cfe3817d1edc916a to your computer and use it in GitHub Desktop.
Save JacobKnaack/ad5e6c1d92a70d42cfe3817d1edc916a to your computer and use it in GitHub Desktop.
docs - initial setup before createDoc
import React from 'react'import React from 'react'
import PropTypes from 'prop-types'
import { Link } from 'gatsby'
// formats our slugs to be used in links to our display page
function formatSlug(title) {
return title
.toLowerCase()
.replace(/[^a-zA-Z ]/g, "")
.replace(/\s/g, '-')
}
// formats datestrings into an object so that they can
// easily be used for display
function formatDate(dateString) {
const date = new Date(dateString)
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
]
const hh = date.getHours()
let minutes = date.getMinutes()
let hour = hh
let dayTime = 'AM'
if (hour >= 12) {
hour = hh - 12
dayTime = 'PM'
}
if (hour == 0) {
hour = 12
}
minutes = minutes < 10 ? '0' + minutes : minutes
return {
year: date.getFullYear(),
month: months[date.getMonth()],
date: date.getDate(),
hour: hour,
minutes: minutes,
dayTime: dayTime,
}
}
const Docs = ({ docs }) => (
<div className="docs-container">
<div className="docs-list">
{docs.map(doc => {
const date_created = formatDate(doc.created_at)
return (
<Link
key={doc._id}
to={`/doc/${formatSlug(doc.title)}`}
className="docs-item"
>
<div className="date-container">
<h3 className="date-yearMonthDate">{`${date_created.month} ${
date_created.date
}, ${date_created.year}`}</h3>
<p className="date-timeDayTime">{`at ${date_created.hour}:${
date_created.minutes
} ${date_created.dayTime}`}</p>
</div>
<h2 className="doc-title">{doc.title}</h2>
<div
className="doc-preview"
dangerouslySetInnerHTML={{
__html: doc.content,
}}
/>
</Link>
)
})}
</div>
</div>
)
Docs.propTypes = {
docs: PropTypes.array.isRequired,
pageContext: PropTypes.object,
}
export default Docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment