diff --git a/src/pages/[roadmapId].json.ts b/src/pages/[roadmapId].json.ts index 16ad93c30..c5720a5c4 100644 --- a/src/pages/[roadmapId].json.ts +++ b/src/pages/[roadmapId].json.ts @@ -3,7 +3,6 @@ import path from 'node:path'; import fs from 'node:fs'; import matter from 'gray-matter'; import { fileURLToPath } from 'node:url'; -import type { OfficialRoadmapDocument } from '../queries/official-roadmap'; export const prerender = false; @@ -31,16 +30,10 @@ type RoadmapJson = { export async function fetchRoadmapJson( roadmapId: string, -): Promise { - const isDev = import.meta.env.DEV; - const baseUrl = new URL( - isDev ? 'http://localhost:8080' : 'https://roadmap.sh', +): Promise { + const response = await fetch( + `https://roadmap.sh/api/v1-official-roadmap/${roadmapId}`, ); - baseUrl.pathname = isDev - ? `/v1-official-roadmap/${roadmapId}` - : `/api/v1-official-roadmap/${roadmapId}`; - - const response = await fetch(String(baseUrl)); if (!response.ok) { throw new Error(`Failed to fetch roadmap json: ${response.statusText}`); @@ -63,7 +56,48 @@ export const GET: APIRoute = async function ({ params, request, props }) { }); } + // Construct the path to the markdown file + let roadmapFilePath = path.join( + projectRoot, + 'src', + 'data', + 'roadmaps', + roadmapId, + `${roadmapId}.md`, + ); + + let roadmapJsonPath = path.join( + projectRoot, + 'src', + 'data', + 'roadmaps', + roadmapId, + `${roadmapId}.json`, + ); + + if (!fs.existsSync(roadmapFilePath)) { + return new Response(JSON.stringify({ message: 'Roadmap not found' }), { + status: 404, + }); + } + + // Read and parse the markdown file + const fileContent = fs.readFileSync(roadmapFilePath, 'utf-8'); + const { data: frontmatter, content } = matter(fileContent); + + if (frontmatter.renderer !== 'editor') { + const roadmapJson = JSON.parse(fs.readFileSync(roadmapJsonPath, 'utf-8')); + + return new Response(JSON.stringify(roadmapJson), { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + }); + } + const roadmapJson = await fetchRoadmapJson(roadmapId); + return new Response(JSON.stringify(roadmapJson), { status: 200, headers: { diff --git a/src/queries/official-roadmap.ts b/src/queries/official-roadmap.ts index 9541d75dc..349abb946 100644 --- a/src/queries/official-roadmap.ts +++ b/src/queries/official-roadmap.ts @@ -2,60 +2,14 @@ import { queryOptions } from '@tanstack/react-query'; import { httpGet } from '../lib/query-http'; import type { Node, Edge } from '@roadmapsh/editor'; -export const allowedOfficialRoadmapType = ['skill', 'role'] as const; -export type AllowedOfficialRoadmapType = - (typeof allowedOfficialRoadmapType)[number]; - -export const allowedOfficialRoadmapQuestionType = ['faq', 'main'] as const; -export type AllowedOfficialRoadmapQuestionType = - (typeof allowedOfficialRoadmapQuestionType)[number]; - -export type OfficialRoadmapQuestion = { - _id: string; - type: AllowedOfficialRoadmapQuestionType; - title: string; - // Tiptap JSON Content - description: any; -}; - export interface OfficialRoadmapDocument { _id: string; - order: number; - - title: { - card: string; - page: string; - }; - description: string; - + title: string; + description?: string; slug: string; nodes: Node[]; edges: Edge[]; - draft: { - nodes: Node[]; - edges: Edge[]; - }; - - seo: { - title: string; - description: string; - keywords: string[]; - }; - partner?: { - description: string; - linkText: string; - link: string; - }; - type: AllowedOfficialRoadmapType; - dimensions?: { - height: number; - width: number; - }; - - questions?: OfficialRoadmapQuestion[]; - relatedRoadmaps?: string[]; - createdAt: Date; updatedAt: Date; }