refactor: Improve code formatting and readability in Markdown and BlogPost utilities

This commit is contained in:
Nawaz Dhandala
2025-08-19 12:58:17 +01:00
parent 36521ef37c
commit dfc324b099
3 changed files with 78 additions and 38 deletions

View File

@@ -19,8 +19,8 @@ export default class Markdown {
markdown: string,
contentType: MarkdownContentType,
): Promise<string> {
// Basic sanitization: neutralize script tags but preserve markdown syntax like '>' for blockquotes.
markdown = markdown.replace(/<script/gi, "&lt;script");
// Basic sanitization: neutralize script tags but preserve markdown syntax like '>' for blockquotes.
markdown = markdown.replace(/<script/gi, "&lt;script");
let renderer: Renderer | null = null;
@@ -77,7 +77,7 @@ export default class Markdown {
};
renderer.code = function (code, language) {
return `<pre class="language-${language} rounded-xl bg-slate-900/95 text-slate-100 p-5 overflow-x-auto text-sm shadow-md ring-1 ring-slate-900/10"><code class="language-${language}">${code}</code></pre>`;
return `<pre class="language-${language} rounded-xl bg-slate-900/95 text-slate-100 p-5 overflow-x-auto text-sm shadow-md ring-1 ring-slate-900/10"><code class="language-${language}">${code}</code></pre>`;
};
renderer.heading = function (text, level) {
@@ -142,7 +142,8 @@ export default class Markdown {
const cls: string = ordered
? "list-decimal pl-6 my-6 space-y-2 text-gray-700"
: "list-disc pl-6 my-6 space-y-2 text-gray-700";
const startAttr: string = ordered && start !== 1 ? ` start=\"${start}\"` : "";
const startAttr: string =
ordered && start !== 1 ? ` start=\"${start}\"` : "";
return `<${tag}${startAttr} class="${cls}">${body}</${tag}>`;
};
renderer.listitem = function (text) {

View File

@@ -93,24 +93,40 @@ app.get(
async (req: ExpressRequest, res: ExpressResponse) => {
try {
const tagName: string = req.params["tagName"] as string;
const tagSlug: string = tagName; // original slug
const tagSlug: string = tagName; // original slug
// Pagination params
const pageParam: string | undefined = req.query["page"] as string | undefined;
const pageSizeParam: string | undefined = req.query["pageSize"] as string | undefined;
let page: number = pageParam ? parseInt(pageParam, 10) : 1;
let pageSize: number = pageSizeParam ? parseInt(pageSizeParam, 10) : 50;
if (isNaN(page) || page < 1) { page = 1; }
if (isNaN(pageSize) || pageSize < 1) { pageSize = 50; }
if (pageSize > 100) { pageSize = 100; }
// Pagination params
const pageParam: string | undefined = req.query["page"] as
| string
| undefined;
const pageSizeParam: string | undefined = req.query["pageSize"] as
| string
| undefined;
let page: number = pageParam ? parseInt(pageParam, 10) : 1;
let pageSize: number = pageSizeParam ? parseInt(pageSizeParam, 10) : 50;
if (isNaN(page) || page < 1) {
page = 1;
}
if (isNaN(pageSize) || pageSize < 1) {
pageSize = 50;
}
if (pageSize > 100) {
pageSize = 100;
}
const allPosts: Array<BlogPostHeader> = await BlogPostUtil.getBlogPostList(tagName);
const totalPosts: number = allPosts.length;
const totalPages: number = Math.ceil(totalPosts / pageSize) || 1;
if (page > totalPages) { page = totalPages; }
const start: number = (page - 1) * pageSize;
const paginatedPosts: Array<BlogPostHeader> = allPosts.slice(start, start + pageSize);
const allTags: Array<string> = await BlogPostUtil.getTags();
const allPosts: Array<BlogPostHeader> =
await BlogPostUtil.getBlogPostList(tagName);
const totalPosts: number = allPosts.length;
const totalPages: number = Math.ceil(totalPosts / pageSize) || 1;
if (page > totalPages) {
page = totalPages;
}
const start: number = (page - 1) * pageSize;
const paginatedPosts: Array<BlogPostHeader> = allPosts.slice(
start,
start + pageSize,
);
const allTags: Array<string> = await BlogPostUtil.getTags();
res.render(`${ViewsPath}/Blog/ListByTag`, {
support: false,
@@ -121,7 +137,7 @@ app.get(
blogPosts: paginatedPosts,
tagName: Text.fromDashesToPascalCase(tagName),
tagSlug: tagSlug,
allTags: allTags,
allTags: allTags,
page: page,
pageSize: pageSize,
totalPages: totalPages,
@@ -140,20 +156,36 @@ app.get(
app.get("/blog", async (_req: ExpressRequest, res: ExpressResponse) => {
try {
const req: ExpressRequest = _req; // alias for clarity
const pageParam: string | undefined = req.query["page"] as string | undefined;
const pageSizeParam: string | undefined = req.query["pageSize"] as string | undefined;
const pageParam: string | undefined = req.query["page"] as
| string
| undefined;
const pageSizeParam: string | undefined = req.query["pageSize"] as
| string
| undefined;
let page: number = pageParam ? parseInt(pageParam, 10) : 1;
let pageSize: number = pageSizeParam ? parseInt(pageSizeParam, 10) : 50;
if (isNaN(page) || page < 1) { page = 1; }
if (isNaN(pageSize) || pageSize < 1) { pageSize = 50; }
if (pageSize > 100) { pageSize = 100; }
if (isNaN(page) || page < 1) {
page = 1;
}
if (isNaN(pageSize) || pageSize < 1) {
pageSize = 50;
}
if (pageSize > 100) {
pageSize = 100;
}
const allPosts: Array<BlogPostHeader> = await BlogPostUtil.getBlogPostList();
const allPosts: Array<BlogPostHeader> =
await BlogPostUtil.getBlogPostList();
const totalPosts: number = allPosts.length;
const totalPages: number = Math.ceil(totalPosts / pageSize) || 1;
if (page > totalPages) { page = totalPages; }
if (page > totalPages) {
page = totalPages;
}
const start: number = (page - 1) * pageSize;
const paginatedPosts: Array<BlogPostHeader> = allPosts.slice(start, start + pageSize);
const paginatedPosts: Array<BlogPostHeader> = allPosts.slice(
start,
start + pageSize,
);
const allTags: Array<string> = await BlogPostUtil.getTags();
res.render(`${ViewsPath}/Blog/List`, {

View File

@@ -133,7 +133,6 @@ export default class BlogPostUtil {
return blogPost;
}
public static async getTags(): Promise<string[]> {
// check if tags are in cache
@@ -184,15 +183,23 @@ export default class BlogPostUtil {
let blogPostAuthor: BlogPostAuthor | null = null;
try {
const blogsMeta: Array<JSONObject> = await this.getBlogsMeta();
const blogMeta: JSONObject | undefined = blogsMeta.find((b: JSONObject) => {
return (b["post"] as string) === fileName;
});
const username: string | undefined = blogMeta?.["authorGitHubUsername"] as string | undefined;
const blogMeta: JSONObject | undefined = blogsMeta.find(
(b: JSONObject) => {
return (b["post"] as string) === fileName;
},
);
const username: string | undefined = blogMeta?.[
"authorGitHubUsername"
] as string | undefined;
if (username) {
const authorsMeta: JSONObject = await this.getAuthorsMeta();
const authorMeta: JSONObject | undefined = authorsMeta[username] as JSONObject | undefined;
const authorName: string | undefined = (authorMeta?.["authorName"] as string) || undefined;
const authorBio: string | undefined = (authorMeta?.["authorBio"] as string) || undefined;
const authorMeta: JSONObject | undefined = authorsMeta[username] as
| JSONObject
| undefined;
const authorName: string | undefined =
(authorMeta?.["authorName"] as string) || undefined;
const authorBio: string | undefined =
(authorMeta?.["authorBio"] as string) || undefined;
blogPostAuthor = {
username,
githubUrl: `https://github.com/${username}`,
@@ -386,7 +393,7 @@ export default class BlogPostUtil {
githubUrl: authorGitHubUrl,
profileImageUrl: authorProfileImageUrl,
// Do NOT call GitHub; use username as name placeholder.
name: authorUsername,
name: authorUsername,
};
}
}