refactor: aggregate tag count with contentlayer
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { defineDocumentType, ComputedFields, makeSource } from 'contentlayer/source-files'
|
||||
import { writeFileSync } from 'fs'
|
||||
import readingTime from 'reading-time'
|
||||
import GithubSlugger from 'github-slugger'
|
||||
import path from 'path'
|
||||
// Remark packages
|
||||
import remarkGfm from 'remark-gfm'
|
||||
@@ -38,6 +40,26 @@ const computedFields: ComputedFields = {
|
||||
toc: { type: 'string', resolve: (doc) => extractTocHeadings(doc.body.raw) },
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the occurrences of all tags across blog posts and write to json file
|
||||
*/
|
||||
function createTagCount(allBlogs) {
|
||||
const tagCount: Record<string, number> = {}
|
||||
allBlogs.forEach((file) => {
|
||||
if (file.tags && file.draft !== true) {
|
||||
file.tags.forEach((tag) => {
|
||||
const formattedTag = GithubSlugger.slug(tag)
|
||||
if (formattedTag in tagCount) {
|
||||
tagCount[formattedTag] += 1
|
||||
} else {
|
||||
tagCount[formattedTag] = 1
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
writeFileSync('./app/tag-data.json', JSON.stringify(tagCount))
|
||||
}
|
||||
|
||||
export const Blog = defineDocumentType(() => ({
|
||||
name: 'Blog',
|
||||
filePathPattern: 'blog/**/*.mdx',
|
||||
@@ -113,4 +135,9 @@ export default makeSource({
|
||||
rehypePresetMinify,
|
||||
],
|
||||
},
|
||||
onSuccess: async (importData) => {
|
||||
const { allBlogs } = await importData()
|
||||
createTagCount(allBlogs)
|
||||
createSearchIndex(allBlogs)
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user