refactor(tags): centralize tag slug logic in client-safe util
This commit is contained in:
15
lib/posts.ts
15
lib/posts.ts
@@ -2,6 +2,9 @@ import fs from 'fs'
|
||||
import path from 'path'
|
||||
import matter from 'gray-matter'
|
||||
import { cache } from 'react'
|
||||
import { tagToSlug } from '@/lib/tags'
|
||||
|
||||
export { tagToSlug } from '@/lib/tags'
|
||||
|
||||
const postsDirectory = path.join(process.cwd(), 'content/posts')
|
||||
|
||||
@@ -61,18 +64,6 @@ export const normalizePostData = (
|
||||
readingTime,
|
||||
})
|
||||
|
||||
export const tagToSlug = (tag: string): string =>
|
||||
tag
|
||||
.normalize('NFKD')
|
||||
.toLowerCase()
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.replace(/\+/g, ' plus ')
|
||||
.replace(/#/g, ' sharp ')
|
||||
.replace(/[\\/]+/g, ' ')
|
||||
.replace(/[^\p{L}\p{N}]+/gu, '-')
|
||||
.replace(/-{2,}/g, '-')
|
||||
.replace(/^-|-$/g, '')
|
||||
|
||||
const getMdxFiles = cache(async () => {
|
||||
try {
|
||||
const files = await fs.promises.readdir(postsDirectory)
|
||||
|
||||
11
lib/tags.ts
Normal file
11
lib/tags.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export const tagToSlug = (tag: string): string =>
|
||||
tag
|
||||
.normalize('NFKD')
|
||||
.toLowerCase()
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.replace(/\+/g, ' plus ')
|
||||
.replace(/#/g, ' sharp ')
|
||||
.replace(/[\\/]+/g, ' ')
|
||||
.replace(/[^\p{L}\p{N}]+/gu, '-')
|
||||
.replace(/-{2,}/g, '-')
|
||||
.replace(/^-|-$/g, '')
|
||||
Reference in New Issue
Block a user