Skip to content

Commit

Permalink
ADD: sitemap.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
fileformat committed Sep 11, 2024
1 parent 68b1c27 commit a4298af
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# To Do

- [ ] sitemap.xml
- [ ] search.html
- [ ] copy to clipboard for variations
- [ ] search.html
- [ ] test button for variations
- [ ] comments via utteranc.es
- [ ] homepage: search
Expand Down
4 changes: 2 additions & 2 deletions app/routes/library.tags[.]html.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { json, type MetaFunction, SerializeFrom } from "@remix-run/node";
import { useLoaderData, useSearchParams } from "@remix-run/react";
import { Link as RemixLink, useLoaderData, useSearchParams } from "@remix-run/react";
import { Container } from "react-bootstrap";
import { Footer } from "~/components/Footer";

Expand Down Expand Up @@ -47,7 +47,7 @@ function TagRow(tag:string, currentTag: string, entries: SerializeFrom<TagEntry>
<ul className="mt-1">
{entries?.map((entry) => (
<li key={entry.url}>
<a href={entry.url}>{entry.title}</a>
<RemixLink to={entry.url}>{entry.title}</RemixLink>
</li>
))}
</ul>
Expand Down
4 changes: 1 addition & 3 deletions app/routes/search[.]html.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { MetaFunction } from "@remix-run/node";
import { Welcome } from "~/components/Welcome/Welcome";
import { ColorSchemeToggle } from "~/components/ColorSchemeToggle/ColorSchemeToggle";
import { HeaderSearch } from "~/components/HeaderSearch/HeaderSearch";
import { Badge, Container, Table } from 'react-bootstrap';
import { Container } from 'react-bootstrap';
import { Footer } from "~/components/Footer";

export const meta: MetaFunction = () => {
Expand Down
31 changes: 31 additions & 0 deletions app/routes/sitemap[.]xml.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getAll, initialize } from "~/components/Library";

function urlLine(url:string) {
return `\t<url><loc>https://www.regex.zone${url}</loc></url>`
}

export async function loader() {

const lines:string[] = [];

lines.push("<?xml version='1.0' encoding='UTF-8'?>");
lines.push('<?xml-stylesheet type="text/xsl" href="/sitemap.xslt" ?>');
lines.push('<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9">');
lines.push(urlLine('/'));
lines.push(urlLine('/library/'));
lines.push(urlLine('/library/tags.html'));
lines.push(urlLine('/search.html'));

initialize();
for (const entry of getAll()) {
lines.push(urlLine(`/library/${entry.handle}/`));
}

lines.push('</urlset>')

return new Response(lines.join('\n'), {
headers: {
"Content-Type": "text/xml; charset=utf-8",
},
});
}
42 changes: 42 additions & 0 deletions public/sitemap.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
>
<xsl:output method="html" indent="yes" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:message>Powered by <a href="https://www.sitemap.style/">Sitemap Style</a></xsl:message>


<!-- get the hostname from the first url/loc -->
<xsl:variable name="hostname" select="substring-before(substring-after(/sitemap:urlset/sitemap:url[1]/sitemap:loc, '://'), '/')" />

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="referrer" content="unsafe-url" />
<title>Sitemap for <xsl:value-of select="$hostname"/></title>
<link rel="stylesheet" href="https://www.sitemap.style/css/pico.classless.min.css" />
</head>
<body>
<main class="container">
<h1>Pages on <xsl:value-of select="$hostname"/></h1>
<ul>
<xsl:for-each select="sitemap:urlset/sitemap:url">
<xsl:variable name="sitemap_loc"><xsl:value-of select="sitemap:loc"/></xsl:variable>
<xsl:variable name="sitemap_lastmod"><xsl:value-of select="sitemap:lastmod"/></xsl:variable>
<li>
<a href="{$sitemap_loc}"><xsl:value-of select="sitemap:loc" /></a>
<xsl:if test="$sitemap_lastmod!=''">
(<xsl:value-of select="sitemap:lastmod" />)
</xsl:if>
</li>
</xsl:for-each>
</ul>
<p><xsl:value-of select="count(sitemap:urlset/sitemap:url)"/> pages</p>
</main>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

0 comments on commit a4298af

Please sign in to comment.