118 lines
2.7 KiB
TypeScript
118 lines
2.7 KiB
TypeScript
import fs from "fs"
|
|
import path from "path"
|
|
|
|
let cachedData: any = null
|
|
|
|
/**
|
|
* Load WordPress processed content
|
|
* This must only be called server-side (in getStaticProps, getServerSideProps, or API routes)
|
|
*/
|
|
export function loadWordPressData() {
|
|
if (cachedData) {
|
|
return cachedData
|
|
}
|
|
|
|
try {
|
|
// In Next.js, process.cwd() returns the code directory (where Next.js runs from)
|
|
const dataPath = path.join(
|
|
process.cwd(),
|
|
"lib",
|
|
"wordpress-data",
|
|
"processed-content.json"
|
|
)
|
|
|
|
// Check if file exists
|
|
if (!fs.existsSync(dataPath)) {
|
|
// Silently return empty data in production
|
|
if (process.env.NODE_ENV === "development") {
|
|
console.error("WordPress data file not found at:", dataPath)
|
|
}
|
|
return { pages: [], posts: [], images: [] }
|
|
}
|
|
|
|
const data = JSON.parse(fs.readFileSync(dataPath, "utf8"))
|
|
cachedData = data
|
|
return data
|
|
} catch (e) {
|
|
// Silently return empty data in production
|
|
if (process.env.NODE_ENV === "development") {
|
|
console.error("Could not load WordPress data:", e)
|
|
}
|
|
return { pages: [], posts: [], images: [] }
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load image mapping
|
|
*/
|
|
export function loadImageMappingData() {
|
|
try {
|
|
const mappingPath = path.join(
|
|
process.cwd(),
|
|
"lib",
|
|
"wordpress-data",
|
|
"image-mapping.json"
|
|
)
|
|
const mapping = JSON.parse(fs.readFileSync(mappingPath, "utf8"))
|
|
return mapping
|
|
} catch (e) {
|
|
// Silently return empty array in production
|
|
if (process.env.NODE_ENV === "development") {
|
|
console.warn("Could not load image mapping:", e)
|
|
}
|
|
return []
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get page by slug
|
|
*/
|
|
export function getPageBySlug(slug: string) {
|
|
const data = loadWordPressData()
|
|
return data.pages.find((p: any) => p.slug === slug)
|
|
}
|
|
|
|
/**
|
|
* Get post by slug and date
|
|
*/
|
|
export function getPostBySlugAndDate(
|
|
slug: string,
|
|
year: string,
|
|
month: string,
|
|
day: string
|
|
) {
|
|
const data = loadWordPressData()
|
|
return data.posts.find(
|
|
(p: any) =>
|
|
p.slug === slug &&
|
|
p.dateComponents?.year === year &&
|
|
p.dateComponents?.month === month &&
|
|
p.dateComponents?.day === day
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Get all page slugs for static generation
|
|
*/
|
|
export function getAllPageSlugs() {
|
|
const data = loadWordPressData()
|
|
return data.pages.map((p: any) => p.slug)
|
|
}
|
|
|
|
/**
|
|
* Get all post params for static generation
|
|
*/
|
|
export function getAllPostParams() {
|
|
const data = loadWordPressData()
|
|
return data.posts
|
|
.map((p: any) => {
|
|
if (!p.dateComponents) return null
|
|
return {
|
|
year: p.dateComponents.year,
|
|
month: p.dateComponents.month,
|
|
day: p.dateComponents.day,
|
|
slug: p.slug,
|
|
}
|
|
})
|
|
.filter(Boolean)
|
|
}
|