Skip to content

Commit

Permalink
Modify links generation and include tags
Browse files Browse the repository at this point in the history
  • Loading branch information
LebCit committed Aug 26, 2023
1 parent b3120b6 commit 539df2e
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions functions/sitemap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { statSync } from "fs"

// Internal Functions
import { getFiles, getPosts, postsByTagList } from "../functions/blog-doc.js"
import { getFiles, getPosts } from "../functions/blog-doc.js"

const viewsFiles = await getFiles("views")
const posts = await getPosts()
Expand Down Expand Up @@ -102,11 +102,13 @@ export function sitemap() {
.pop()
.replace(/\.[^/.]+$/, "")

const fileDir = file.split("/")[1]

let fileLastMod = statSync(file).mtimeMs
let fileLastModDate = new Date(fileLastMod).toLocaleDateString().split("/").reverse().join("-")

let fileObj = {
urlLocation: siteURL + fileTitle,
urlLocation: `${siteURL + fileDir}/${fileTitle}`,
urlLastMod: fileLastModDate,
}

Expand All @@ -122,27 +124,31 @@ export function sitemap() {
// Remove duplicates from tagsArray using a Set
const tagsArray = [...new Set(allTagsArray)]

tagsArray.forEach(async (tag) => {
// If tag is not undefined
if (tag) {
let tagLastMod

const postsByTag = await postsByTagList(tag)

postsByTag.forEach((post) => {
const postTitle = post[0]
const postLastMod = statSync(`views/posts/${postTitle}`).mtimeMs

tagLastMod = new Date(postLastMod).toLocaleDateString().split("/").reverse().join("-")
})

let tagObj = {
urlLocation: siteURL + "tags/" + tag,
urlLastMod: tagLastMod,
}

urlsData.push(tagObj)
tagsArray.forEach((tag) => {
// Define an empty time array
let timeArray = []
// Get the posts in which the tag exist
const postsByTagArray = posts.filter((post) =>
post[1].frontmatter.tags ? post[1].frontmatter.tags.includes(tag) : post[1].frontmatter.tags === []
)
// Push into the time array the last modification time of each post of the tag
postsByTagArray.forEach((post) => {
const postTitle = post[0]
const postLastMod = statSync(`views/posts/${postTitle}`).mtimeMs

timeArray.push(postLastMod)
})
// Get the newest time from the time array
const newestTime = Math.max.apply(Math, timeArray)
// Change the tag's last modification time to a readable date.
const tagLastMod = new Date(newestTime).toLocaleDateString().split("/").reverse().join("-")

let tagObj = {
urlLocation: siteURL + "tags/" + tag,
urlLastMod: tagLastMod,
}

urlsData.push(tagObj)
})
}
tagObj()
Expand Down

0 comments on commit 539df2e

Please sign in to comment.