-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
109 additions
and
153 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Course } from "../utils/types.ts"; | ||
|
||
export default function CourseCard(props: { course: Course }) { | ||
const { course } = props; | ||
return ( | ||
<div | ||
class="py-4 gray-200 hover:opacity-75" | ||
style={{ order: course.order }} | ||
> | ||
<a href={btoa(`/${course.slug}`)}> | ||
<h3 class="gray-900 font-bold"> | ||
{course.title} | ||
</h3> | ||
<div class="text-gray-500 truncate text-ellipsis max-w-[300px] md:max-w-full overflow-hidden"> | ||
{course.snippet} | ||
</div> | ||
</a> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,35 @@ | ||
import { Handlers } from "$fresh/server.ts"; | ||
import { extract } from "https://deno.land/[email protected]/encoding/front_matter.ts"; | ||
import { join } from "$std/path/mod.ts"; | ||
import { PageProps } from "$fresh/server.ts"; | ||
import { readJson } from "https://deno.land/[email protected]/fs/read_json.ts"; | ||
import { Course, CourseGroup } from "../utils/course.ts"; | ||
import { join } from "$std/path/mod.ts"; | ||
|
||
export interface CourseAttributes { | ||
title?: string; | ||
snippet?: string; | ||
order?: number; | ||
} | ||
import CourseCard from "../components/CourseCard.tsx"; | ||
import { getCourse, getGroupOrder } from "../utils/course.ts"; | ||
import { Course, CourseGroup } from "../utils/types.ts"; | ||
|
||
const cache: { merged: (Course | CourseGroup)[] } = { merged: [] }; | ||
|
||
export async function getCourse( | ||
slug: string, | ||
): Promise<Course | null> { | ||
const text = await Deno.readTextFile(join("./courses", `${slug}.md`)); | ||
const { attrs, body } = extract(text); | ||
const courseAttrs = attrs as CourseAttributes; | ||
|
||
const course: Course = { | ||
slug, | ||
title: courseAttrs.title || "بدون عنوان", | ||
content: body || "لايوجد محتوى", | ||
snippet: courseAttrs.snippet || "لا يوجد", | ||
order: courseAttrs.order || 999, | ||
}; | ||
|
||
return course; | ||
} | ||
|
||
async function getGroupOrder( | ||
groupPath: string, | ||
): Promise<{ order: number; label: string } | undefined> { | ||
try { | ||
const dataJsonPath = join(groupPath, "_data.json"); | ||
const jsonData = await readJson(dataJsonPath) as { | ||
order: number; | ||
label: string; | ||
}; | ||
return { ...jsonData }; | ||
} catch (error) { | ||
return undefined; | ||
} | ||
} | ||
export const handler: Handlers<{ merged: (Course | CourseGroup)[] }> = { | ||
async GET(_req, ctx) { | ||
const courses = await getCourses(); | ||
return ctx.render(courses); | ||
}, | ||
}; | ||
|
||
export async function getCourses(): Promise< | ||
{ merged: (Course | CourseGroup)[] } | ||
> { | ||
// if (cache.merged.length > 0) { | ||
// return cache; | ||
// } | ||
if (cache.merged.length > 0) { | ||
return cache; | ||
} | ||
|
||
console.log("Fetching courses..."); | ||
|
||
const files = Deno.readDir("./courses"); | ||
const groups: CourseGroup[] = []; | ||
const nonGroups: Course[] = []; | ||
|
||
for await (const file of files) { | ||
if (file.isDirectory) { | ||
const groupSlug = file.name; | ||
const groupFiles = Deno.readDir(join("./courses", groupSlug)); | ||
const groupPromises = []; | ||
|
||
for await (const groupFile of groupFiles) { | ||
if (!groupFile.isDirectory && groupFile.name.endsWith(".md")) { | ||
const slug = groupFile.name.replace(".md", ""); | ||
|
@@ -74,7 +39,6 @@ export async function getCourses(): Promise< | |
} | ||
} | ||
} | ||
|
||
const groupOrder = await getGroupOrder(join("./courses", groupSlug)); | ||
groups.push({ | ||
courses: groupPromises, | ||
|
@@ -93,17 +57,9 @@ export async function getCourses(): Promise< | |
const merged: (Course | CourseGroup)[] = [...nonGroups, ...groups]; | ||
merged.sort((a, b) => (a.order || 999) - (b.order || 999)); | ||
cache.merged = merged; | ||
|
||
return cache; | ||
} | ||
|
||
export const handler: Handlers<{ merged: (Course | CourseGroup)[] }> = { | ||
async GET(_req, ctx) { | ||
const courses = await getCourses(); | ||
return ctx.render(courses); | ||
}, | ||
}; | ||
|
||
export default function BlogIndexPage( | ||
props: PageProps<{ merged: (Course | CourseGroup)[] }>, | ||
) { | ||
|
@@ -140,22 +96,3 @@ export default function BlogIndexPage( | |
</main> | ||
); | ||
} | ||
|
||
function CourseCard(props: { course: Course }) { | ||
const { course } = props; | ||
return ( | ||
<div | ||
class={`py-4 gray-200 hover:opacity-75 `} | ||
style={{ order: course.order }} | ||
> | ||
<a href={btoa(`/${course.slug}`)}> | ||
<h3 class="gray-900 font-bold"> | ||
{course.title} | ||
</h3> | ||
<div class="text-gray-500 truncate text-ellipsis max-w-[300px] md:max-w-full overflow-hidden"> | ||
{course.snippet} | ||
</div> | ||
</a> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
function isMobile() { | ||
return window.innerWidth <= 768; | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", function () { | ||
document.body.style.overflow = "hidden"; | ||
|
||
Split(["#split-0", "#split-1"], { | ||
gutterAlign: "start", | ||
minSize: 0, | ||
gutterSize: 13, | ||
}); | ||
if (isMobile()) { | ||
Split(["#split-0", "#split-1"], { | ||
sizes: [0, 100], | ||
gutterAlign: "start", | ||
minSize: 0, | ||
gutterSize: 13, | ||
}); | ||
} else { | ||
Split(["#split-0", "#split-1"], { | ||
sizes: [50, 50], | ||
gutterAlign: "start", | ||
minSize: 0, | ||
gutterSize: 13, | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
export interface Course { | ||
slug: string; | ||
title: string; | ||
content: string; | ||
snippet: string; | ||
order: number; | ||
import { join } from "$std/path/mod.ts"; | ||
import { readJson } from "https://deno.land/[email protected]/fs/read_json.ts"; | ||
import { extract } from "https://deno.land/[email protected]/encoding/front_matter.ts"; | ||
|
||
import { Course, CourseAttributes } from "../utils/types.ts"; | ||
|
||
export async function getGroupOrder( | ||
groupPath: string, | ||
): Promise<{ order: number; label: string } | undefined> { | ||
try { | ||
const dataJsonPath = join(groupPath, "_data.json"); | ||
const jsonData = await readJson(dataJsonPath) as { | ||
order: number; | ||
label: string; | ||
}; | ||
return { ...jsonData }; | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
export interface CourseGroup { | ||
courses: Course[]; | ||
order?: number; | ||
label?: string; | ||
export async function getCourse( | ||
slug: string, | ||
): Promise<Course | null> { | ||
const text = await Deno.readTextFile(join("./courses", `${slug}.md`)); | ||
const { attrs, body } = extract(text); | ||
const courseAttrs = attrs as CourseAttributes; | ||
const course: Course = { | ||
slug, | ||
title: courseAttrs.title || "بدون عنوان", | ||
content: body || "لايوجد محتوى", | ||
snippet: courseAttrs.snippet || "لا يوجد", | ||
order: courseAttrs.order || 999, | ||
}; | ||
return course; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export interface Course { | ||
slug: string; | ||
title: string; | ||
content: string; | ||
snippet: string; | ||
order: number; | ||
} | ||
|
||
export interface CourseGroup { | ||
courses: Course[]; | ||
order?: number; | ||
label?: string; | ||
} | ||
|
||
export interface CourseAttributes { | ||
title?: string; | ||
snippet?: string; | ||
order?: number; | ||
} |