Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added trailing slash option #11

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ deno task test

<!----------------------------------[ Badges ]--------------------------------->

[Badge License]: https://img.shields.io/badge/License-MIT-ac8b11.svg?style=for-the-badge&labelColor=yellow
[Badge License]: https://img.shields.io/badge/License-MIT-ac8b11.svg?style=for-the-badge&labelColor=yellow
26 changes: 21 additions & 5 deletions src/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ import day from "dayjs";
import { basename, extname } from "path";
import { Manifest } from "fresh/server.ts";

interface Options {
leadingSlash?: boolean;
}

export class SitemapContext {
#url: string;
#manifest: Manifest;
#options: Options = {
leadingSlash: false,
};
#globalIgnore = ["sitemap.xml"];
#routes: Array<string> = [];

constructor(url: string, manifest: Manifest) {
constructor(url: string, manifest: Manifest, options?: Options) {
this.#url = url;
this.#manifest = manifest;
this.#options = options || this.#options;

this.#routes = Object.entries(this.#manifest.routes)
.filter(([path]) => {
// const isRootRoute = "./routes" === dirname(path);
Expand All @@ -33,9 +42,9 @@ export class SitemapContext {

return true;
})
.map(([path, route]) => {
.map(([path]) => {
return path
.replace(extname(path), "")
.replace(extname(path), this.#options.leadingSlash ? "/" : "")
.replace("./routes", "")
.replace("index", ""); // We remove index as it's consider a "/" in Fresh
})
Expand All @@ -46,7 +55,10 @@ export class SitemapContext {
}

add(route: string) {
this.#routes.push(route);
this.#routes.push(
this.#options.leadingSlash
? route.endsWith("/") ? route : route + "/"
: route.replace(/\/+$/, ""));
return this;
}

Expand All @@ -60,7 +72,11 @@ export class SitemapContext {
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
${this.routes.map((route) => {
return `<url>
<loc>${this.#url}${route}</loc>
<loc>${this.#url}${route === "/"
? route
: this.#options.leadingSlash
? route.endsWith("/") ? route : route + "/"
: route.replace(/\/+$/, "")}</loc>
<lastmod>${day().format("YYYY-MM-DD")}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
Expand Down
54 changes: 53 additions & 1 deletion tests/sitemap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,59 @@ Deno.test("Remove certain routes", () => {
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">',
);

assertThrows(() => assertStringIncludes(result, "<loc>https://deno.land/gfm.css</loc>"));
assert(!result.includes("<loc>https://deno.land/gfm.css</loc>"));

assertStringIncludes(result, "</urlset>");
});


Deno.test("Add leading slashes", () => {
const manifest: Manifest = {
routes: {
"./routes/dashboard.tsx": { default: () => null }
},
islands: {},
baseUrl: url,
};
const sitemap = new SitemapContext(url, manifest, {
leadingSlash: true
});

sitemap.add("/hello")

const result = sitemap.generate();

assertStringIncludes(result, '<?xml version="1.0" encoding="UTF-8"?>');
assertStringIncludes(
result,
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">',
);

assertStringIncludes(result, "<loc>https://deno.land/dashboard/</loc>")
assertStringIncludes(result, "<loc>https://deno.land/hello/</loc>")

assertStringIncludes(result, "</urlset>");
});

Deno.test("Remove leading slashes", () => {
const manifest: Manifest = {
routes: {},
islands: {},
baseUrl: url,
};
const sitemap = new SitemapContext(url, manifest);

sitemap.add("/hello/")

const result = sitemap.generate();

assertStringIncludes(result, '<?xml version="1.0" encoding="UTF-8"?>');
assertStringIncludes(
result,
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">',
);

assert(!result.includes("<loc>https://deno.land/hello/</loc>"))

assertStringIncludes(result, "</urlset>");
});