Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 79 additions & 8 deletions app/(single-page)/[pageType]/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,100 @@ type SinglePageType = {
slug: string;
};

/*
* This function fetches the markdown content from a given URL and returns it as a string.
* It also cleans the content by removing any HTML tags.
*/
async function fetchMarkdownContent(url: string): Promise<string> {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Failed to fetch content from ${url}: ${response.statusText}`,
);
}
let content = await response.text();
content = cleanMarkdownContent(content);
return content;
}

/*
* This function removes HTML tags from a given markdown content.
*/
function cleanMarkdownContent(content: string): string {
// Use a regex to remove problematic HTML tags
return content.replace(/<[^>]*>/g, "");
}

/*
* This function fetches the page data for a given page type and slug.
* It also fetches the markdown content if the page has a directMdLink.
*/
async function fetchPageData(pageType: PageType, slug: string) {
let page = getPosts(pageType).find((page) => page.slug === slug);

if (!page || pageType === "faqs") {
return null;
}

// Fetch the markdown content if the page has a directMdLink
if (page.metadata.directMdLink) {
try {
console.log(
`Fetching markdown content from ${page.metadata.directMdLink}`,
);
const content = await fetchMarkdownContent(page.metadata.directMdLink);
page.content = content;
} catch (error) {
console.error(
`Failed to fetch markdown content from ${page.metadata.directMdLink}:`,
error,
);
return null;
}
}

return page;
}

export async function generateStaticParams() {
let posts: SinglePageType[] = [];
console.log(
"Generating static paths for single pages (events, projects, trainings)",
);
posts = [
...posts,
...getPosts("projects").map((p) => {
return { slug: p.slug, pageType: "projects" as PageType };
return {
slug: p.slug,
pageType: "projects" as PageType,
};
}),
...getPosts("events").map((p) => {
return { slug: p.slug, pageType: "events" as PageType };
return {
slug: p.slug,
pageType: "events" as PageType,
};
}),
...getPosts("trainings").map((p) => {
return { slug: p.slug, pageType: "trainings" as PageType };
return {
slug: p.slug,
pageType: "trainings" as PageType,
};
}),
];
return posts;
}

export default function SinglePage({ params }: { params: SinglePageType }) {
let page = getPosts(params.pageType).find(
(page) => page.slug === params.slug,
);
export default async function SinglePage({
params,
}: {
params: SinglePageType;
}) {
const { pageType, slug } = params;

const page = await fetchPageData(pageType, slug);

if (!page || params.pageType === "faqs") {
if (!page) {
notFound();
}

Expand Down
23 changes: 23 additions & 0 deletions content/projects/carbon-service.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: carbon-service by Karim Daw
links:
- url: https://example.com
label: Website
- url: https://github.com/org
label: GitHub
directMdLink: https://raw.githubusercontent.com/karim-daw/carbon-service/master/README.md
---

### Problem

### Solution

### Why Open Source?

### License

### Operating Model

### About the team

### Contact
23 changes: 23 additions & 0 deletions content/projects/openepd.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: OPENEPD by C-Change Labs
links:
- url: https://example.com
label: Website
- url: https://github.com/org
label: GitHub
directMdLink: https://raw.githubusercontent.com/cchangelabs/openepd/dev/README.md
---

### Problem

### Solution

### Why Open Source?

### License

### Operating Model

### About the team

### Contact
Loading