Skip to content

Commit

Permalink
fix: h1,h2 check and code tag adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
0tuedon committed Nov 22, 2024
1 parent 151f4e8 commit a5b37f2
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 160 deletions.
141 changes: 108 additions & 33 deletions contentlayer.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import path from "path";
import * as fs from "fs";
import { createSlug, SpeakerData, TopicsData, unsluggify } from "./src/utils";
import { defineDocumentType, defineNestedType, makeSource } from "contentlayer2/source-files";
import { Transcript as ContentTranscriptType, Source as ContentSourceType } from "./.contentlayer/generated/types";
import {
defineDocumentType,
defineNestedType,
makeSource,
} from "contentlayer2/source-files";
import {
Transcript as ContentTranscriptType,
Source as ContentSourceType,
} from "./.contentlayer/generated/types";
import { LanguageCodes } from "./src/config";

const Resources = defineNestedType(() => ({
name: "Resources",
Expand Down Expand Up @@ -94,7 +102,11 @@ function organizeTags(transcripts: ContentTranscriptType[]) {
});

// Process all tags at once
const allTags = new Set(transcripts.flatMap((transcript) => transcript.tags?.map((tag) => tag) || []));
const allTags = new Set(
transcripts.flatMap(
(transcript) => transcript.tags?.map((tag) => tag) || []
)
);

allTags.forEach((tag) => {
const catInfo = categoryMap.get(tag);
Expand All @@ -116,11 +128,13 @@ function organizeTags(transcripts: ContentTranscriptType[]) {

// Add "Miscellaneous" category with remaining uncategorized tags
if (tagsWithoutCategory.size > 0) {
tagsByCategory["Miscellaneous"] = Array.from(tagsWithoutCategory).map((tag) => ({
name: tag,
slug: tag,
count: tagCounts[tag] || 0,
}));
tagsByCategory["Miscellaneous"] = Array.from(tagsWithoutCategory).map(
(tag) => ({
name: tag,
slug: tag,
count: tagCounts[tag] || 0,
})
);
}

// Sort tags alphabetically within each category
Expand Down Expand Up @@ -190,7 +204,10 @@ function createSpeakers(transcripts: ContentTranscriptType[]) {
fs.writeFileSync("./public/speaker-data.json", JSON.stringify(speakerArray));
}

function generateSourcesCount(transcripts: ContentTranscriptType[], sources: ContentSourceType[]) {
function generateSourcesCount(
transcripts: ContentTranscriptType[],
sources: ContentSourceType[]
) {
const sourcesArray: TagInfo[] = [];
const slugSources: Record<string, number> = {};

Expand All @@ -204,7 +221,10 @@ function generateSourcesCount(transcripts: ContentTranscriptType[], sources: Con
slugSources[slug] = sourcesLength;

const getSourceName = (slug: string) =>
sources.find((source) => source.language === "en" && source.slugAsParams[0] === slug)?.title ?? unsluggify(slug);
sources.find(
(source) =>
source.language === "en" && source.slugAsParams[0] === slug
)?.title ?? unsluggify(slug);

sourcesArray[sourcesLength] = {
slug,
Expand All @@ -214,12 +234,21 @@ function generateSourcesCount(transcripts: ContentTranscriptType[], sources: Con
}
});

fs.writeFileSync("./public/source-count-data.json", JSON.stringify(sourcesArray));
fs.writeFileSync(
"./public/source-count-data.json",
JSON.stringify(sourcesArray)
);
return { sourcesArray, slugSources };
}

const createTypesCount = (transcripts: ContentTranscriptType[], sources: ContentSourceType[]) => {
const { sourcesArray, slugSources } = generateSourcesCount(transcripts, sources);
const createTypesCount = (
transcripts: ContentTranscriptType[],
sources: ContentSourceType[]
) => {
const { sourcesArray, slugSources } = generateSourcesCount(
transcripts,
sources
);
const nestedTypes: any = {};

sources.forEach((transcript) => {
Expand All @@ -234,7 +263,8 @@ const createTypesCount = (transcripts: ContentTranscriptType[], sources: Content
if (!nestedTypes[slugType]) {
nestedTypes[slugType] = [];
} else {
if (nestedTypes[slugType].includes(getSource) || getSource === null) return;
if (nestedTypes[slugType].includes(getSource) || getSource === null)
return;
nestedTypes[slugType].push(getSource);
}
});
Expand All @@ -244,11 +274,27 @@ const createTypesCount = (transcripts: ContentTranscriptType[], sources: Content
fs.writeFileSync("./public/types-data.json", JSON.stringify(nestedTypes));
};

function organizeContent(transcripts: ContentTranscriptType[], sources: ContentSourceType[]) {
function organizeContent(
transcripts: ContentTranscriptType[],
sources: ContentSourceType[]
) {
const tree: any = {};

sources.forEach((source) => {
const { _id, slugAsParams, language, _raw, weight, body, hosts, transcription_coverage, url, type, types, ...metaData } = source;
const {
_id,
slugAsParams,
language,
_raw,
weight,
body,
hosts,
transcription_coverage,
url,
type,
types,
...metaData
} = source;
const params = source.slugAsParams;
const topParam = params[0] as string;
const nestedSource = params.length > 1;
Expand All @@ -257,16 +303,21 @@ function organizeContent(transcripts: ContentTranscriptType[], sources: ContentS
tree[topParam] = {};
}
const allTranscriptsForSourceLanguage = transcripts.filter(
(transcript) => transcript._raw.sourceFileDir === source._raw.sourceFileDir && transcript.language === language
(transcript) =>
transcript._raw.sourceFileDir === source._raw.sourceFileDir &&
transcript.language === language
);

const allTranscriptsForSourceLanguageURLs = allTranscriptsForSourceLanguage.map((transcript) => transcript.url);
const allTranscriptsForSourceLanguageURLs =
allTranscriptsForSourceLanguage.map((transcript) => transcript.url);

if (!nestedSource) {
tree[topParam] = {
...tree[topParam],
[language]: {
data: allTranscriptsForSourceLanguageURLs.length ? allTranscriptsForSourceLanguageURLs : {},
data: allTranscriptsForSourceLanguageURLs.length
? allTranscriptsForSourceLanguageURLs
: {},
metadata: {
...metaData,
},
Expand All @@ -276,7 +327,9 @@ function organizeContent(transcripts: ContentTranscriptType[], sources: ContentS
tree[topParam][language].data = {
...tree[topParam][language].data,
[params[1]]: {
data: allTranscriptsForSourceLanguageURLs.length ? allTranscriptsForSourceLanguageURLs : {},
data: allTranscriptsForSourceLanguageURLs.length
? allTranscriptsForSourceLanguageURLs
: {},
metadata: {
...metaData,
},
Expand Down Expand Up @@ -326,22 +379,42 @@ export const Transcript = defineDocumentType(() => ({
type: "string",
resolve: (doc) => {
const transcript = doc._raw.flattenedPath.split("/").pop();
const lan = transcript?.split(".").length === 2 ? transcript?.split(".")[1] : "en";
return lan;
const lan = transcript?.match(/[.]\w+/gi);
const lanWithoutDot = (lan?.[lan.length - 1] || "").replace(".", "");
const finalLanguage = LanguageCodes.includes(lanWithoutDot)
? lanWithoutDot
: "en";
return finalLanguage;
},
},
languageURL:{
type:"string",
resolve:(doc)=> {
languageURL: {
type: "string",
resolve: (doc) => {
const transcript = doc._raw.flattenedPath.split("/").pop();
const pathWithoutDot = doc._raw.flattenedPath.replace(/[.]\w+/gi,"")
const lan = transcript?.split(".").length === 2 ? `/${transcript?.split(".")[1]}` : "";
return `${lan}/${pathWithoutDot}`
}
const fullPathWithoutDot = doc._raw.flattenedPath.replace(
/[.]\w{2}$/gi, // Removes the last two characters if there's a dot
""
);

const stringAfterDot = transcript?.match(/\.(\w{2})$/gi); // Removes the last two characters if there's a dot
const languageWithoutDot = (stringAfterDot?.[1] || "")

if (LanguageCodes.includes(languageWithoutDot)) {
return `/${languageWithoutDot}/${fullPathWithoutDot}`;
}

return `/${fullPathWithoutDot}`;
},
},
slugAsParams: {
type: "list",
resolve: (doc) => doc._raw.flattenedPath.split("/").slice(0, -1),
resolve: (doc) => {
const pathWithoutDot = doc._raw.flattenedPath.replace(
/[.]\w{2}$/gi, // Removes the last two characters if there's a dot
""
);
return pathWithoutDot.split("/");
},
},
},
}));
Expand All @@ -363,13 +436,15 @@ export const Source = defineDocumentType(() => ({
computedFields: {
url: {
type: "string",
resolve: (doc) => `/${doc._raw.flattenedPath.split("/").slice(0, -1).join("/")}`,
resolve: (doc) =>
`/${doc._raw.flattenedPath.split("/").slice(0, -1).join("/")}`,
},
language: {
type: "string",
resolve: (doc) => {
const index = doc._raw.flattenedPath.split("/").pop();
const lan = index?.split(".").length === 2 ? index?.split(".")[1] : "en";
const lan =
index?.split(".").length === 2 ? index?.split(".")[1] : "en";
return lan;
},
},
Expand Down Expand Up @@ -403,4 +478,4 @@ export default makeSource({
generateSourcesCount(allTranscripts, allSources);
organizeContent(allTranscripts, allSources);
},
});
});
14 changes: 3 additions & 11 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const nextConfig = {
return {
fallback: [
{
source: "/:path*.:ext([^/]+)", // intercept all paths ending with a file extension
destination: "/gh-pages/:path*.:ext", // rewrite to gh-pages/[path_here].ext
source: "/:path*.:ext([a-zA-Z0-9_+]{1,4})", // Match extensions that are 1-4 AlphaNumeric characters long
destination: "/gh-pages/:path*.:ext", // Rewrite to gh-pages/[path_here].ext
},
{
source: "/tags/:path",
Expand All @@ -29,17 +29,9 @@ const nextConfig = {
destination: "/gh-pages/pt/index.html",
},
{
source: "/:path((?!.*\\.[^/]+).*)", // Matches paths without a file extension
source: "/:path((?!.*\\.[a-zA-Z0-9]{1,4}$).*)", // Matches paths without a valid file extension
destination: "/transcript/:path*", // Rewrite to /transcripts/[path...]
},
{
source: "/transcripts",
destination: "/gh-pages/index.html",
},
{
source: "/types",
destination: "/gh-pages/categories/index.html",
},
{
source: "/:path*",
destination: "/gh-pages/:path*/index.html",
Expand Down
62 changes: 45 additions & 17 deletions src/app/transcript/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,70 @@
import React from "react";
import { allTranscripts } from "contentlayer/generated";
import { LanguageCodes } from "@/config";
import allSources from "@/public/sources-data.json";
import { notFound } from "next/navigation";
import IndividualTranscript from "@/components/individual-transcript/IndividualTranscript";
import { createSlug } from "@/utils";

// forces 404 for paths not generated from `generateStaticParams` function.
export const dynamicParams = false;

export function generateStaticParams() {
const allSingleTranscriptPaths = allTranscripts.map((transcript) => {
const slugForLanguage = transcript.languageURL.split("/").filter(path => Boolean(path.trim()));
const slugForLanguage = transcript.languageURL
.split("/")
.filter((path) => Boolean(path.trim()));
return {
slug: slugForLanguage
}
slug: slugForLanguage,
};
});

return allSingleTranscriptPaths;
}

const Page = ({ params }: { params: { slug: string[] } }) => {
const slugArray = params.slug;
const isNonEnglishLanguage = LanguageCodes.includes(slugArray[0]);
let transcriptUrl = "";
if (isNonEnglishLanguage) {
const languageCode = slugArray.shift();
slugArray[slugArray.length - 1] = slugArray[slugArray.length - 1] + `.${languageCode}`;
transcriptUrl = `/${slugArray.join("/")}`;
} else {
transcriptUrl = `/${slugArray.join("/")}`;
}
let transcriptUrl = `/${slugArray.join("/")}`;

const transcript = allTranscripts.find(transcript => transcript.url === transcriptUrl)
const transcript = allTranscripts.find(
(transcript) => transcript.languageURL === transcriptUrl
);

if(!transcript) {
if (!transcript) {
return notFound();
}

let data: any = allSources;

const breadCrumbRoutes = transcript.slugAsParams.map(
(crumb: string, index: number) => {
let title = "";
const languageNumber = transcript.slugAsParams.length - (index + 1);
data = data[crumb as keyof typeof allSources];
if (index === 0) {
title = data[transcript.language]?.metadata.title;
data = data[transcript.language]?.data;
} else if (index === transcript.slugAsParams.length - 1) {
title = transcript.title;
} else {
title = data?.metadata.title;
data = data?.data;
}

return {
name: title,
link: transcript.languageURL
.split("/")
.slice(0, languageNumber === 0 ? undefined : -languageNumber)
.join("/"),
isActive: index === transcript.slugAsParams.length - 1,
};
}
);

return (
<IndividualTranscript transcript={transcript}/>
<IndividualTranscript
breadCrumbs={[...breadCrumbRoutes]}
transcript={transcript}
/>
);
};

Expand Down
Loading

0 comments on commit a5b37f2

Please sign in to comment.