Files
developer-roadmap/src/lib/roadmap-topic.ts
T
Kamran AhmedandArik Chakma a2719bc771 feat: user accounts functionality (#3813)
* feat: integrate astro

* chore: login popup design

* chore: data-popup changed

* refactor: github and google button

* chore: signup page

* chore: login popup design

* chore: signup page design

* chore: auth divider

* feat: integrate astro

* chore: login popup design

* chore: data-popup changed

* refactor: github and google button

* chore: signup page

* chore: login popup design

* chore: signup page design

* chore: auth divider

* chore: login feature

* chore: login error message

* chore: added name in token decode return

* chore: use auth hook

* chore: logout vs login

* chore: download button link

* chore: account dropdown

* fix: dropdown z index

* chore: profile page

* Add missing content for backend roadmap

* Remove unused styles

* Add login with google

* chore: google login implementation

* chore: profile guard clause

* fix: button size

* chore: preact to astro components

* chore: preact to astro comp

* chore: github astro component

* chore: google login error handling

* chore: github login error handling

* chore: change password page

* chore: rename profile to password

* fix: change password rename

* chore: update profile page

* chore: setting sidebar

* fix: setting dropdown design

* chore: required indicator

* chore: change password form

* chore: update profile form

* chore: mobile navigation

* fix: form data empty error

* chore: email login and signup components

* chore: forgot password page

* chore: reset password page

* chore: verify account page

* chore: resend verification email

* fix: types in spinner

* chore: forgot password functionality

* fix: class -> className

* chore: reset password page

* chore: reset password functionality

* chore: login page

* fix: spacing for login and signup page

* refactor: email login form

* chore: astro spinner

* chore: pre-fill user data

* chore: dummy placeholder

* chore: forgot password link add

* fix: replaced constants

* chore: forgot password link

* chore: change password for social provider

* chore: internal pages guard

* chore: internal paths

* refactor: change password errors

* refactor: update profile errors

* chore: mark as done overlay

* fix: uncontrolled to controlled form

* fix: de-structure error

* chore: error messages

* fix: 401 error code redirect to login page

* chore: loading spinner accessibilities

* fix: remove spinner

* chore: keep spinner after success to redirect

* chore: keep the spinner

* style: resend email underline

* chore: chevron down account

* chore: roadmap pdf link download

* chore: roadmap pdf link download

* chore: best practices buttons

* fix: verify account text

* fix: topic overlay hide

* chore: base verify design

* chore: email verify page

* fix: div tag missing

* Formatting

* Refactor top navigation

* Prettier

* Update dependencies

* Refactor top navigation

* Refactor login button

* Remove captcha and add google scripts

* Refactor email sign up form

* Resend verfication email functionality

* Refactor verification pending page

* Add verify account functionality

* Update signup text

* Add login page

* Add login button in top nav

* Email login form

* Handle authenticatoin

* Show hide auth elements change

* Add ease-in on the guest elements

* Refactor logic for download and subscribe popups

* Add forgot password

* Rename fetch lib

* Add authentication popup

* Refactor logic for mark done and pending

* Handle logout

* Add route protection

* Popup opener to close the overlay

* Remember page when logging in

* Add reset password page

* Change placement of constant

* Update profile page

* Add update password form

* Update password page

* Update profile page

* Update design

* chore: toggle mark resource done api

* chore: toggle topic done

* chore: get user resource progress api

* fix: best practice topic toggle

* chore: fetch progress

* fix: query selector for topics

* Keep track of the old page before social login

* Update public api url

* Add user progress tracking

* Update topic done functionality

* Add progress loader

* Add page wide spinner

* Add spinner on setting pages

* Add fingerprint to user requests

* Use http wrapper instead of fetch

* Update fingerprint

* Minor improvements

---------

Co-authored-by: Arik Chakma <arikchangma@gmail.com>
2023-04-14 19:22:52 +01:00

220 lines
6.4 KiB
TypeScript

import type {MarkdownFileType} from './file';
import type {RoadmapFrontmatter} from './roadmap';
// Generates URL from the topic file path e.g.
// -> /src/data/roadmaps/vue/content/102-ecosystem/102-ssr/101-nuxt-js.md
// /vue/ecosystem/ssr/nuxt-js
// -> /src/data/roadmaps/vue/content/102-ecosystem
// /vue/ecosystem
function generateTopicUrl(filePath: string) {
return filePath
.replace('/src/data/roadmaps/', '/') // Remove the base `/src/data/roadmaps` from path
.replace('/content', '') // Remove the `/[roadmapId]/content`
.replace(/\/\d+-/g, '/') // Remove ordering info `/101-ecosystem`
.replace(/\/index\.md$/, '') // Make the `/index.md` to become the parent folder only
.replace(/\.md$/, ''); // Remove `.md` from the end of file
}
/**
* Generates breadcrumbs for the given topic URL from the given topic file details
*
* @param topicUrl Topic URL for which breadcrumbs are required
* @param topicFiles Topic file mapping to read the topic data from
*/
function generateBreadcrumbs(
topicUrl: string,
topicFiles: Record<string, RoadmapTopicFileType>
): BreadcrumbItem[] {
// We need to collect all the pages with permalinks to generate breadcrumbs
// e.g. /backend/internet/how-does-internet-work/http
// /backend
// /backend/internet
// /backend/internet/how-does-internet-work
// /backend/internet/how-does-internet-work/http
const urlParts = topicUrl.split('/');
const breadcrumbUrls = [];
const subLinks = [];
for (let counter = 0; counter < urlParts.length; counter++) {
subLinks.push(urlParts[counter]);
// Skip the following
// -> [ '' ]
// -> [ '', 'vue' ]
if (subLinks.length > 2) {
breadcrumbUrls.push(subLinks.join('/'));
}
}
return breadcrumbUrls.map((breadCrumbUrl): BreadcrumbItem => {
const topicFile = topicFiles[breadCrumbUrl];
const topicFileContent = topicFile?.file;
const firstHeading = topicFileContent?.getHeadings()?.[0];
return {title: firstHeading?.text, url: breadCrumbUrl};
});
}
export type BreadcrumbItem = {
title: string;
url: string;
};
export interface RoadmapTopicFileType {
url: string;
heading: string;
file: MarkdownFileType;
roadmap: RoadmapFrontmatter;
roadmapId: string;
breadcrumbs: BreadcrumbItem[];
}
/**
* Gets all the topic files available for all the roadmaps
* @returns Hashmap containing the topic slug and the topic file content
*/
export async function getRoadmapTopicFiles(): Promise<
Record<string, RoadmapTopicFileType>
> {
const contentFiles = await import.meta.glob<string>(
'/src/data/roadmaps/*/content/**/*.md',
{
eager: true,
}
);
const mapping: Record<string, RoadmapTopicFileType> = {};
for (let filePath of Object.keys(contentFiles)) {
const fileContent: MarkdownFileType = contentFiles[filePath] as any;
const fileHeadings = fileContent.getHeadings();
const firstHeading = fileHeadings[0];
const [, roadmapId] =
filePath.match(/^\/src\/data\/roadmaps\/(.+)?\/content\/(.+)?$/) || [];
const topicUrl = generateTopicUrl(filePath);
const currentRoadmap = await import(
`../data/roadmaps/${roadmapId}/${roadmapId}.md`
);
mapping[topicUrl] = {
url: topicUrl,
heading: firstHeading?.text,
file: fileContent,
roadmap: currentRoadmap.frontmatter,
roadmapId: roadmapId,
breadcrumbs: [],
};
}
// Populate breadcrumbs inside the mapping
Object.keys(mapping).forEach((topicUrl) => {
const {
roadmap: currentRoadmap,
roadmapId,
file: currentTopic,
} = mapping[topicUrl];
const roadmapUrl = `/${roadmapId}`;
// Breadcrumbs for the file
mapping[topicUrl].breadcrumbs = [
{
title: 'Roadmaps',
url: '/roadmaps',
},
{
title: currentRoadmap.briefTitle,
url: `${roadmapUrl}`,
},
{
title: 'Topics',
url: `${roadmapUrl}/topics`,
},
...generateBreadcrumbs(topicUrl, mapping),
];
});
return mapping;
}
// [
// '/frontend/internet/how-does-the-internet-work',
// '/frontend/internet/what-is-http',
// '/frontend/internet/browsers-and-how-they-work',
// '/frontend/internet/dns-and-how-it-works',
// '/frontend/internet/what-is-domain-name',
// '/frontend/internet/what-is-hosting',
// '/frontend/internet',
// '/frontend/html/learn-the-basics',
// '/frontend/html/writing-semantic-html',
// '/frontend/html/forms-and-validations',
// '/frontend/html/conventions-and-best-practices',
// '/frontend/html/accessibility',
// '/frontend/html/seo-basics',
// '/frontend/html',
// '/frontend/css/learn-the-basics',
// '/frontend/css/making-layouts',
// '/frontend/css/responsive-design-and-media-queries',
// '/frontend/css',
// '/frontend/javascript/syntax-and-basic-constructs',
// '/frontend/javascript/learn-dom-manipulation',
// '/frontend/javascript/learn-fetch-api-ajax-xhr',
// '/frontend/javascript/es6-and-modular-javascript',
// '/frontend/javascript/concepts',
// '/frontend/javascript',
// '/frontend/version-control-systems/basic-usage-of-git',
// '/frontend/version-control-systems'
// ]
async function sortTopics(
topics: RoadmapTopicFileType[]
): Promise<RoadmapTopicFileType[]> {
let sortedTopics: RoadmapTopicFileType[] = [];
// For each of the topic, find its place in the sorted topics
for (let i = 0; i < topics.length; i++) {
const currTopic = topics[i];
const currUrl = currTopic.url;
let isPlaced = false;
// Find the first sorted topic which starts with the current topic
for (let j = 0; j < sortedTopics.length; j++) {
const sortedTopic = sortedTopics[j];
const sortedUrl = sortedTopic.url;
// Insert before the current URL and break
if (sortedUrl.startsWith(`${currUrl}/`)) {
sortedTopics.splice(j, 0, currTopic);
isPlaced = true;
break;
}
}
if (!isPlaced) {
sortedTopics.push(currTopic);
}
}
return sortedTopics;
}
/**
* Gets the the topics for a given roadmap
* @param roadmapId Roadmap id for which you want the topics
* @returns Promise<TopicFileType[]>
*/
export async function getTopicsByRoadmapId(
roadmapId: string
): Promise<RoadmapTopicFileType[]> {
const topicFileMapping = await getRoadmapTopicFiles();
const allTopics = Object.values(topicFileMapping);
const roadmapTopics = allTopics.filter(
(topic) => topic.roadmapId === roadmapId
);
return sortTopics(roadmapTopics);
}