Skip to content

Commit

Permalink
fix: check articles parution date (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoanRos authored Feb 22, 2024
1 parent 8b3d3a7 commit 88f2c1e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pages/blog/[blog].js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import styles from "../../style/bloga.module.css"
import { ANDROID_URL, IOS_URL } from "../../constants"
import appStorePic from "../../public/images/other/app-store-fr.png"
import googlePlayPic from "../../public/images/other/google-play-fr.png"
import { parse } from "date-fns"
import { fr } from "date-fns/locale"

const Blog = ({ mdxSource, data }) => {
const [showPopup, setShowPopup] = useState(false)
Expand Down Expand Up @@ -89,6 +91,18 @@ export async function getStaticProps({ params }) {
const fileContents = fs.readFileSync(filePath, "utf-8")
const { content, data } = matter(fileContents)
const mdxSource = await serialize(content)

const articleDate = parse(data.date, "MMMM d, yyyy", new Date(), {
locale: fr,
})

const currentDate = new Date()

if (articleDate >= currentDate) {
return {
notFound: true,
}
}
return {
props: {
mdxSource,
Expand Down
14 changes: 14 additions & 0 deletions pages/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Head from "next/head"
import Navigation, { DownloadPopup } from "../../components/Navigation"
import Footer from "../../components/Footer"
import BlogCard from "../../components/BlogCard"
import { parse } from "date-fns"
import { fr } from "date-fns/locale"

const Index = ({ posts }) => {
const [showPopup, setShowPopup] = useState(false)
Expand All @@ -27,6 +29,7 @@ const Index = ({ posts }) => {

<div className="grid grid-cols-1 gap-6 lg:grid-cols-3 text-center">
{posts
.filter((post) => !post.notFound)
.sort((a, b) => a.order - b.order)
.map((post) => (
<BlogCard
Expand Down Expand Up @@ -57,6 +60,17 @@ export async function getStaticProps() {
const rawContent = fs.readFileSync(filePath, "utf8")
const { content, data } = matter(rawContent)
const mdxSource = await serialize(content)
const articleDate = parse(data.date, "MMMM d, yyyy", new Date(), {
locale: fr,
})

const currentDate = new Date()

if (articleDate >= currentDate) {
return {
notFound: true,
}
}

return {
...data,
Expand Down
7 changes: 6 additions & 1 deletion pages/site-map.xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export async function getStaticProps() {
const parsedDate = data.date
? parse(data.date, "MMMM d, yyyy", new Date(), { locale: fr })
: null
const currentDate = new Date()

if (parsedDate >= currentDate) {
return null
}
return {
...data,
mdxSource,
Expand All @@ -99,7 +104,7 @@ export async function getStaticProps() {
})
)

const sitemap = generateSiteMap(posts)
const sitemap = generateSiteMap(posts.filter((post) => post !== null))

fs.writeFileSync(path.join(process.cwd(), "public", "sitemap.xml"), sitemap)

Expand Down

0 comments on commit 88f2c1e

Please sign in to comment.