Files
developer-roadmap/src/lib/video.ts
T
Kamran AhmedandArik Chakma ad6002a514 feat: profile pages, custom roadmap pages and SSR (#5494)
* Update

* Add stats and health endpoints

* Add pre-render

* fix: redirect to the error page

* Fix generate-renderer issue

* Rename

* Fix best practice topics not loading

* Handle SSR for static pages

* Refactor faqs

* Refactor best practices

* Fix absolute import

* Fix stats

* Add custom roadmap page

* Minor UI change

* feat: custom roadmap slug routes (#4987)

* feat: replace roadmap slug

* fix: remove roadmap slug

* feat: username route

* fix: user public page

* feat: show roadmap progress

* feat: update public profile

* fix: replace with toast

* feat: user public profile page

* feat: implement profile form

* feat: implement user profile roadmap page

* refactor: remove logs

* fix: increase progress gap

* fix: remove title margin

* fix: breakpoint for roadmaps

* Update dependencies

* Upgrade dependencies

* fix: improper avatars

* fix: heatmap focus

* wip: remove `getStaticPaths`

* fix: add disable props

* wip

* feat: add email icon

* fix: update pnpm lock

* fix: implement author page

* Fix beginner roadmaps not working

* Changes to form

* Refactor profile and form

* Refactor public profile form

* Rearrange sidebar items

* Update UI for public form

* Minor text update

* Refactor public profile form

* Error page for user

* Revamp UI for profile page

* Add public profile page

* Fix vite warnings

* Add private profile banner

* feat: on blur check username

* Update fetch depth

* Add error detail

* Use hybrid mode of rendering

* Do not pre-render stats pages

* Update deployment workflow

* Update deployment workflow

---------

Co-authored-by: Arik Chakma <arikchangma@gmail.com>
2024-04-11 22:35:52 +01:00

95 lines
2.3 KiB
TypeScript

import type { MarkdownFileType } from './file';
import type { AuthorFileType } from './author.ts';
import { getAllAuthors } from './author.ts';
import type { GuideFileType } from './guide.ts';
import { getAllGuides } from './guide.ts';
export interface VideoFrontmatter {
title: string;
description: string;
authorId: string;
seo: {
title: string;
description: string;
};
isNew: boolean;
duration: string;
date: string;
sitemap: {
priority: number;
changefreq: 'daily' | 'weekly' | 'monthly' | 'yearly';
};
tags: string[];
}
export type VideoFileType = MarkdownFileType<VideoFrontmatter> & {
id: string;
author: AuthorFileType;
};
/**
* Generates id from the given video file
* @param filePath Markdown file path
*
* @returns unique video identifier
*/
function videoPathToId(filePath: string): string {
const fileName = filePath.split('/').pop() || '';
return fileName.replace('.md', '');
}
export async function getVideosByAuthor(
authorId: string,
): Promise<VideoFileType[]> {
const allVideos = await getAllVideos();
return allVideos.filter((video) => video.author?.id === authorId);
}
/**
* Gets all the videos sorted by the publishing date
* @returns Promisifed video files
*/
export async function getAllVideos(): Promise<VideoFileType[]> {
const videos = import.meta.glob<VideoFileType>('/src/data/videos/*.md', {
eager: true,
});
const allAuthors = await getAllAuthors();
const videoFiles = Object.values(videos);
const enrichedVideos = videoFiles.map((videoFile) => ({
...videoFile,
id: videoPathToId(videoFile.file),
author: allAuthors.find(
(author) => author.id === videoFile.frontmatter.authorId,
)!,
}));
return enrichedVideos.sort(
(a, b) =>
new Date(b.frontmatter.date).valueOf() -
new Date(a.frontmatter.date).valueOf(),
);
}
export async function getVideoById(id: string): Promise<VideoFileType> {
const videoFilesMap: Record<string, VideoFileType> =
import.meta.glob<VideoFileType>('../data/videos/*.md', {
eager: true,
});
const videoFile = Object.values(videoFilesMap).find((videoFile) => {
return videoPathToId(videoFile.file) === id;
});
if (!videoFile) {
throw new Error(`Video with ID ${id} not found`);
}
return {
...videoFile,
id: videoPathToId(videoFile.file),
};
}