Skip to content

Commit

Permalink
cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
TeaByte committed Jan 6, 2024
1 parent 9181bc6 commit c1c1267
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 153 deletions.
44 changes: 0 additions & 44 deletions components/BlogIndexPage.tsx

This file was deleted.

12 changes: 0 additions & 12 deletions components/Button.tsx

This file was deleted.

20 changes: 20 additions & 0 deletions components/CourseCard.tsx
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>
);
}
2 changes: 1 addition & 1 deletion components/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function NavBar() {
<li>
<a
href={menu.href}
class={"text-gray-500 hover:text-gray-700 py-1 border-gray-500 hidden"}
class="text-gray-500 hover:text-gray-700 py-1 border-gray-500 hidden"
>
{menu.name}
</a>
Expand Down
5 changes: 2 additions & 3 deletions islands/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { StateUpdater, useEffect, useState } from "preact/hooks";
import { Button } from "../components/Button.tsx";
import { useEffect, useState } from "preact/hooks";

interface CounterProps {
preCode: string;
Expand All @@ -8,7 +7,7 @@ interface CounterProps {

declare var window: Window & typeof globalThis;
interface Window {
editor: any; // Replace 'any' with the actual type of your 'editor' property
editor: any;
}

export default function Editor(props: CounterProps) {
Expand Down
4 changes: 2 additions & 2 deletions routes/[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Course } from "../utils/course.ts";
import { getCourse } from "./index.tsx";
import { Course } from "../utils/types.ts";
import { getCourse } from "../utils/course.ts";
import { Handlers } from "$fresh/server.ts";
import { PageProps } from "$fresh/server.ts";
import { CSS, render } from "$gfm";
Expand Down
89 changes: 13 additions & 76 deletions routes/index.tsx
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/std@0.151.0/encoding/front_matter.ts";
import { join } from "$std/path/mod.ts";
import { PageProps } from "$fresh/server.ts";
import { readJson } from "https://deno.land/std@0.66.0/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", "");
Expand All @@ -74,7 +39,6 @@ export async function getCourses(): Promise<
}
}
}

const groupOrder = await getGroupOrder(join("./courses", groupSlug));
groups.push({
courses: groupPromises,
Expand All @@ -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)[] }>,
) {
Expand Down Expand Up @@ -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>
);
}
24 changes: 19 additions & 5 deletions static/resizer.js
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,
});
}
});
43 changes: 33 additions & 10 deletions utils/course.ts
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/std@0.66.0/fs/read_json.ts";
import { extract } from "https://deno.land/std@0.151.0/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;
}
19 changes: 19 additions & 0 deletions utils/types.ts
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;
}

0 comments on commit c1c1267

Please sign in to comment.