diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 9898a4ab..dd95ff7f 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -19,6 +19,21 @@ type FlyAndScaleParams = { }; +export function convertToSlug(title) { + // Remove punctuation, hyphens, and special characters + const cleanedTitle = title + .replace(/[-.,?!:"]/g, "") // Remove punctuation and hyphens + .replace(/\s+/g, " ") // Replace multiple spaces with a single space + .trim(); // Remove leading and trailing spaces + + // Convert to lowercase, split by spaces, and join with hyphens + const words = cleanedTitle.toLowerCase().split(" "); + const truncatedWords = words; + + // Join with hyphens + const slug = truncatedWords.join("-"); + return slug; +} export function isPWAInstalled() { diff --git a/src/routes/blog/+page.svelte b/src/routes/blog/+page.svelte index 250e0f26..64cd0425 100644 --- a/src/routes/blog/+page.svelte +++ b/src/routes/blog/+page.svelte @@ -1,26 +1,10 @@ diff --git a/src/routes/blog/article/[slug]/+page.server.ts b/src/routes/blog/article/[slug]/+page.server.ts index 970ff430..10e2d08f 100644 --- a/src/routes/blog/article/[slug]/+page.server.ts +++ b/src/routes/blog/article/[slug]/+page.server.ts @@ -1,15 +1,4 @@ -function convertToSlug(title) { - // Remove punctuation and special characters - const cleanedTitle = title - .replace(/[.,?!:"]/g, "") // Remove punctuation - .replace(/\s+/g, " ") // Replace multiple spaces with a single space - .trim(); // Remove leading and trailing spaces - - // Convert to lowercase, split by spaces, and join with hyphens - const words = cleanedTitle.toLowerCase().split(" "); - const slug = words.join("-"); - return slug; -} +import { convertToSlug } from "$lib/utils"; export const load = async ({ locals, params }) => { const { pb } = locals; diff --git a/src/routes/sitemap.xml/+server.ts b/src/routes/sitemap.xml/+server.ts index eae5665a..4394d36c 100644 --- a/src/routes/sitemap.xml/+server.ts +++ b/src/routes/sitemap.xml/+server.ts @@ -1,3 +1,5 @@ +import { convertToSlug } from "$lib/utils"; + const pages = [ { title: "/" }, { title: "/cramer-tracker" }, @@ -66,13 +68,12 @@ const pages = [ { title: "/analysts/top-stocks" }, ]; -const site = "https://stocknear.com"; const website = "https://stocknear.com"; /** @type {import('./$types').RequestHandler} */ export async function GET({ locals }) { //get all posts; - const { apiKey, apiURL } = locals; + const { apiKey, apiURL, pb } = locals; @@ -90,7 +91,12 @@ export async function GET({ locals }) { type: item?.type, })); - const body = sitemap(stocks, pages); + + const articles = await pb.collection("articles").getFullList({ + sort: "-created", + }); + + const body = sitemap(stocks, articles, pages); const response = new Response(body); response.headers.set("Cache-Control", "max-age=0, s-maxage=3600"); response.headers.set("Content-Type", "application/xml"); @@ -100,6 +106,7 @@ export async function GET({ locals }) { // Modified sitemap function const sitemap = ( stocks, + articles, pages, ) => ` ${website}${page.title} + `, + ) + .join("")} + ${articles + .map( + (item) => ` + + ${website}/blog/${convertToSlug(item?.title)} + `, ) .join("")}