Skip to content

Commit

Permalink
Merge branches 'main' and 'main' of https://github.com/hta218/leo-blog
Browse files Browse the repository at this point in the history
* 'main' of https://github.com/hta218/leo-blog:
  fix: tags on snippets

* 'main' of https://github.com/hta218/leo-blog:
  fix: tags on snippets
  • Loading branch information
hta218 committed Aug 28, 2023
2 parents df1c797 + 0b0cf0f commit fbdc37f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 39 deletions.
39 changes: 21 additions & 18 deletions libs/mdx.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,30 @@ export async function getFileBySlug(
}
}

export function getAllFilesFrontMatter(folder: string) {
export function getAllFilesFrontMatter(...folderNames: string[]) {
let root = process.cwd()
let prefixPaths = path.join(root, 'data', folder)

let files = getAllFilesRecursively(prefixPaths)
let allFrontMatter: BlogFrontMatter[] = []

files.forEach((file) => {
// Replace is needed to work on Windows
let fileName = file.slice(prefixPaths.length + 1).replace(/\\/g, '/')
// Remove Unexpected File
if (path.extname(fileName) !== '.md' && path.extname(fileName) !== '.mdx') {
return
}
let source = fs.readFileSync(file, 'utf8')
let grayMatterData = matter(source)
let data = grayMatterData.data as BlogFrontMatter
if (data.draft !== true) {
allFrontMatter.push({ ...data, slug: formatSlug(fileName) })
}
})
for (const folder of folderNames) {
let prefixPaths = path.join(root, 'data', folder)
let files = getAllFilesRecursively(prefixPaths)

files.forEach((file) => {
// Replace is needed to work on Windows
let fileName = file.slice(prefixPaths.length + 1).replace(/\\/g, '/')
// Remove Unexpected File
if (path.extname(fileName) !== '.md' && path.extname(fileName) !== '.mdx') {
return
}
let source = fs.readFileSync(file, 'utf8')
let grayMatterData = matter(source)
let data = grayMatterData.data as BlogFrontMatter
data.readingTime = readingTime(grayMatterData.content)
if (data.draft !== true) {
allFrontMatter.push({ ...data, slug: formatSlug(fileName) })
}
})
}

return allFrontMatter.sort((a, b) => dateSortDesc(a.date, b.date))
}
38 changes: 20 additions & 18 deletions libs/tags.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@ import type { TagsCount } from '~/types/server'
import { kebabCase } from '~/utils/string'
import { getFiles } from './files.server'

export function getAllTags(type: string): TagsCount {
let files = getFiles(type)
export function getAllTags(...types: string[]): TagsCount {
let root = process.cwd()
let tagsCount: TagsCount = {}

// Iterate through each post, putting all found tags into `tags`
files.forEach((file, i) => {
let source = fs.readFileSync(path.join(root, 'data', type, file), 'utf8')
let grayMatterData = matter(source)
let data = grayMatterData.data as MdxFrontMatter
if (data.tags && data.draft !== true) {
data.tags.forEach((tag: string) => {
let formattedTag = kebabCase(tag)
if (formattedTag in tagsCount) {
tagsCount[formattedTag] += 1
} else {
tagsCount[formattedTag] = 1
}
})
}
})
for (const type of types) {
let files = getFiles(type)

// Iterate through each post, putting all found tags into `tags`
files.forEach((file, i) => {
let source = fs.readFileSync(path.join(root, 'data', type, file), 'utf8')
let grayMatterData = matter(source)
let data = grayMatterData.data as MdxFrontMatter
if (data.tags && data.draft !== true) {
data.tags.forEach((tag: string) => {
let formattedTag = kebabCase(tag)
if (formattedTag in tagsCount) {
tagsCount[formattedTag] += 1
} else {
tagsCount[formattedTag] = 1
}
})
}
})
}
return tagsCount
}
2 changes: 1 addition & 1 deletion pages/tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { TagsCount } from '~/types/server'
import { kebabCase } from '~/utils/string'

export async function getStaticProps({ locale }) {
let tags = getAllTags('blog')
let tags = getAllTags('blog', 'snippets')
return {
props: { tags, ...(await serverSideTranslations(locale, ['common'])) },
}
Expand Down
4 changes: 2 additions & 2 deletions pages/tags/[tag].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { BlogFrontMatter } from '~/types/mdx'
import { kebabCase } from '~/utils/string'

export function getStaticPaths({ locale }) {
let tags = getAllTags(`${locale}/blog`)
let tags = getAllTags(`${locale}/blog`, `${locale}/snippets`)
return {
paths: Object.keys(tags).map((tag) => ({
params: {
Expand All @@ -30,7 +30,7 @@ export async function getStaticProps({
params: { tag: string }
locale: string
}) {
let allPosts = getAllFilesFrontMatter(`${locale}/blog`)
let allPosts = getAllFilesFrontMatter(`${locale}/blog`, `${locale}/snippets`)
let filteredPosts = allPosts.filter(
(post) => post.draft !== true && post.tags.map((t) => kebabCase(t)).includes(params.tag)
)
Expand Down

1 comment on commit fbdc37f

@vercel
Copy link

@vercel vercel bot commented on fbdc37f Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

leo-huynh-dot-dev – ./

leo-huynh-dot-dev-git-main-hta218.vercel.app
leo-huynh-dot-dev-hta218.vercel.app
leohuynh.dev
www.leohuynh.dev

Please sign in to comment.