Skip to content

Commit

Permalink
match metadata generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane98c committed Jan 15, 2025
1 parent 3d87090 commit defbc42
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions utils/generate-metadata.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const fs = require('fs')
const path = require('path')
const matter = require('gray-matter')
const glob = require('glob')
const matter = require('gray-matter')

// We generate this at build to allow us to access from edge functions like dynamic OG images (og.js)

// Get list of all article and commentary folders
const articles = fs
.readdirSync(path.join(process.cwd(), 'articles'))
.filter((p) => p.match(/^[\w|\d|-]+$/))
Expand All @@ -12,18 +13,17 @@ const commentary = fs
.readdirSync(path.join(process.cwd(), 'commentary'))
.filter((p) => p.match(/^[\w|\d|-]+$/))

// Helper function to get metadata for articles and commentary
const getMetadata = (ids, folder) => {
const directory = path.join(process.cwd(), folder)
return ids
.map((id) => {
const source = fs.readFileSync(path.join(directory, `${id}/index.md`))
let references
try {
const referencesBuffer = fs.readFileSync(
references = fs.readFileSync(
path.join(directory, `${id}/references.json`)
)
references = JSON.parse(referencesBuffer.toString())
references = JSON.parse(references)
} catch {
references = {}
}
Expand All @@ -36,9 +36,7 @@ const getMetadata = (ids, folder) => {
path: `${directory}/${id}/index.md`,
}
})
.sort((a, b) => {
return new Date(b.date).getTime() - new Date(a.date).getTime()
})
.sort((a, b) => new Date(b.date) - new Date(a.date))
.map((meta, idx) => ({ ...meta, number: ids.length - 1 - idx }))
}

Expand Down

0 comments on commit defbc42

Please sign in to comment.