Auto-expand sidebar category and add sitemap categories
This commit is contained in:
@@ -1,9 +1,23 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { browser } from '$app/environment';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { categories, getCalculatorsByCategory, type CalculatorDef } from '$lib/data/calculators';
|
import { categories, getCalculatorsByCategory, type CalculatorDef } from '$lib/data/calculators';
|
||||||
|
|
||||||
let expandedCategory = '';
|
let expandedCategory = '';
|
||||||
let expandedUnits: Record<string, string> = {};
|
let expandedUnits: Record<string, string> = {};
|
||||||
|
let isDesktop = false;
|
||||||
|
let navBreakpoint: MediaQueryList | null = null;
|
||||||
|
let lastPath = '';
|
||||||
|
let lastDesktop = isDesktop;
|
||||||
|
let autoExpandedCategory = '';
|
||||||
|
|
||||||
|
const categoryPathRegex = /^\/category\/([^/]+)(?:\/|$)/;
|
||||||
|
|
||||||
|
function getCategorySlugFromPath(path: string) {
|
||||||
|
const match = path.match(categoryPathRegex);
|
||||||
|
return match?.[1] ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
$: currentPath = $page.url.pathname;
|
$: currentPath = $page.url.pathname;
|
||||||
|
|
||||||
@@ -61,6 +75,49 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
if (!browser) return;
|
||||||
|
|
||||||
|
navBreakpoint = window.matchMedia('(max-width: 1024px)');
|
||||||
|
const updateDesktop = (event?: MediaQueryListEvent) => {
|
||||||
|
const matches = event?.matches ?? navBreakpoint!.matches;
|
||||||
|
isDesktop = !matches;
|
||||||
|
};
|
||||||
|
|
||||||
|
updateDesktop();
|
||||||
|
|
||||||
|
const handleNavChange = (event: MediaQueryListEvent) => updateDesktop(event);
|
||||||
|
if ('addEventListener' in navBreakpoint) {
|
||||||
|
navBreakpoint.addEventListener('change', handleNavChange);
|
||||||
|
} else {
|
||||||
|
navBreakpoint.addListener(handleNavChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (!navBreakpoint) return;
|
||||||
|
if ('removeEventListener' in navBreakpoint) {
|
||||||
|
navBreakpoint.removeEventListener('change', handleNavChange);
|
||||||
|
} else {
|
||||||
|
navBreakpoint.removeListener(handleNavChange);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
$: if (browser && (currentPath !== lastPath || isDesktop !== lastDesktop)) {
|
||||||
|
const slug = getCategorySlugFromPath(currentPath);
|
||||||
|
if (isDesktop && slug) {
|
||||||
|
expandedCategory = slug;
|
||||||
|
autoExpandedCategory = slug;
|
||||||
|
} else if (autoExpandedCategory && (!isDesktop || !slug)) {
|
||||||
|
if (expandedCategory === autoExpandedCategory) {
|
||||||
|
expandedCategory = '';
|
||||||
|
}
|
||||||
|
autoExpandedCategory = '';
|
||||||
|
}
|
||||||
|
lastPath = currentPath;
|
||||||
|
lastDesktop = isDesktop;
|
||||||
|
}
|
||||||
|
|
||||||
export let open = false;
|
export let open = false;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import type { RequestHandler } from './$types';
|
import type { RequestHandler } from './$types';
|
||||||
import { calculators } from '$lib/data/calculators';
|
import { calculators, categories } from '$lib/data/calculators';
|
||||||
|
|
||||||
export const GET: RequestHandler = async () => {
|
export const GET: RequestHandler = async () => {
|
||||||
const urls = calculators.map(
|
const calculatorUrls = calculators.map(
|
||||||
(calc) => `
|
(calc) => `
|
||||||
<url>
|
<url>
|
||||||
<loc>https://howdoyouconvert.com/${calc.slug}</loc>
|
<loc>https://howdoyouconvert.com/${calc.slug}</loc>
|
||||||
@@ -11,6 +11,15 @@ export const GET: RequestHandler = async () => {
|
|||||||
</url>`
|
</url>`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const categoryUrls = Object.keys(categories).map(
|
||||||
|
(category) => `
|
||||||
|
<url>
|
||||||
|
<loc>https://howdoyouconvert.com/category/${category}</loc>
|
||||||
|
<changefreq>weekly</changefreq>
|
||||||
|
<priority>0.9</priority>
|
||||||
|
</url>`
|
||||||
|
);
|
||||||
|
|
||||||
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
|
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||||
<url>
|
<url>
|
||||||
@@ -18,7 +27,8 @@ export const GET: RequestHandler = async () => {
|
|||||||
<changefreq>weekly</changefreq>
|
<changefreq>weekly</changefreq>
|
||||||
<priority>1.0</priority>
|
<priority>1.0</priority>
|
||||||
</url>
|
</url>
|
||||||
${urls.join('')}
|
${categoryUrls.join('')}
|
||||||
|
${calculatorUrls.join('')}
|
||||||
</urlset>`;
|
</urlset>`;
|
||||||
|
|
||||||
return new Response(sitemap, {
|
return new Response(sitemap, {
|
||||||
|
|||||||
Reference in New Issue
Block a user