Skip to content

Commit

Permalink
feat: site map generator
Browse files Browse the repository at this point in the history
  • Loading branch information
YoanRos committed Dec 28, 2023
1 parent 4e0c005 commit 7e6c60a
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 62 deletions.
100 changes: 100 additions & 0 deletions pages/site-map.xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import fs from "fs"
import matter from "gray-matter"
import path from "path"
import { serialize } from "next-mdx-remote/serialize"

const URL = "https://ozensemble.fabrique.social.gouv.fr"

function generateSiteMap(posts) {
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>${URL}</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>1.00</priority>
</url>
<url>
<loc>${URL}/blog</loc>
<lastmod>2023-12-16T19:39:35+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>${URL}/files/Affiche_OZ-Ensemble.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>${URL}/files/AFFICHE_OZ_-_Defi_de_Janvier.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>${URL}/files/DEPLIANT-OZ.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>${URL}/files/Dossier_de_presse_-_2023_-_Defi_de_Janvier.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>${URL}/files/25012023-Oz_Ensemble-Mentions_legales_site_V2.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>${URL}/files/25012023-Oz_Ensemble-Politique_de_confidentialite_site_V3.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>${URL}/stats</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
${posts
.map(({ slug }) => {
return `
<url>
<loc>${`${URL}/blog/${slug}`}</loc>
</url>
`
})
.join("")}
</urlset>
`
}

export default function SiteMap() {
return <div>{/* Render your sitemap content here */}</div>
}

export async function getStaticProps() {
const files = fs.readdirSync(path.join(process.cwd(), "content"))

const posts = await Promise.all(
files
.filter((fn) => fn.endsWith(".mdx"))
.map(async (filename) => {
const filePath = path.join(process.cwd(), "content", filename)
const rawContent = fs.readFileSync(filePath, "utf8")
const { content, data } = matter(rawContent)
const mdxSource = await serialize(content)

return {
...data,
mdxSource,
slug: filename.replace(".mdx", ""),
}
})
)

const sitemap = generateSiteMap(posts)
console.log(sitemap)
fs.writeFileSync(path.join(process.cwd(), "public", "sitemap.xml"), sitemap)

return {
props: { posts },
}
}
54 changes: 54 additions & 0 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://ozensemble.fabrique.social.gouv.fr</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>1.00</priority>
</url>
<url>
<loc>https://ozensemble.fabrique.social.gouv.fr/blog</loc>
<lastmod>2023-12-16T19:39:35+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://ozensemble.fabrique.social.gouv.fr/files/Affiche_OZ-Ensemble.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://ozensemble.fabrique.social.gouv.fr/files/AFFICHE_OZ_-_Defi_de_Janvier.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://ozensemble.fabrique.social.gouv.fr/files/DEPLIANT-OZ.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://ozensemble.fabrique.social.gouv.fr/files/Dossier_de_presse_-_2023_-_Defi_de_Janvier.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://ozensemble.fabrique.social.gouv.fr/files/25012023-Oz_Ensemble-Mentions_legales_site_V2.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://ozensemble.fabrique.social.gouv.fr/files/25012023-Oz_Ensemble-Politique_de_confidentialite_site_V3.pdf</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://ozensemble.fabrique.social.gouv.fr/stats</loc>
<lastmod>2023-12-16T19:39:33+00:00</lastmod>
<priority>0.80</priority>
</url>

<url>
<loc>https://ozensemble.fabrique.social.gouv.fr/blog/reussir-son-dry-january-avec-oz-ensemble</loc>
</url>

</urlset>

62 changes: 0 additions & 62 deletions site-map.xml

This file was deleted.

0 comments on commit 7e6c60a

Please sign in to comment.