Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

Commit

Permalink
fix: beautify sitemap and robots output
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydw committed Feb 21, 2022
1 parent 4aecc2f commit eebd05a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"version": "3.2.1",
"main": "./dist/index.js",
"scripts": {
"build:example": "npx amagaki build example",
"check": "gts check",
"clean": "gts clean",
"compile": "tsc; npx gulp build;",
Expand Down
39 changes: 18 additions & 21 deletions src/sitemap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ test('SitemapPlugin: sitemap.xml', async (t: ExecutionContext) => {
await pod.router.warmup();
const sitemapRoute = await pod.router.resolve('/foo/sitemap.xml') as Route;
const sitemapContent = await sitemapRoute.build();
const sitemapExpected = `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://localhost/pages/foo/</loc>
<xhtml:link href="http://localhost/pages/foo/" hreflang="x-default" rel="alternate" />
<xhtml:link href="http://localhost/pages/foo/" hreflang="en" rel="alternate" />
<xhtml:link href="http://localhost/ja/pages/foo/" hreflang="ja" rel="alternate" />
</url>
<url>
<loc>http://localhost/pages/</loc>
<xhtml:link href="http://localhost/pages/" hreflang="x-default" rel="alternate" />
<xhtml:link href="http://localhost/pages/" hreflang="en" rel="alternate" />
<xhtml:link href="http://localhost/ja/pages/" hreflang="ja" rel="alternate" />
</url>
</urlset>
`.trim();
const sitemapExpected =
`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://localhost/pages/foo/</loc>
<xhtml:link href="http://localhost/pages/foo/" hreflang="x-default" rel="alternate" />
<xhtml:link href="http://localhost/pages/foo/" hreflang="en" rel="alternate" />
<xhtml:link href="http://localhost/ja/pages/foo/" hreflang="ja" rel="alternate" />
</url>
<url>
<loc>http://localhost/pages/</loc>
<xhtml:link href="http://localhost/pages/" hreflang="x-default" rel="alternate" />
<xhtml:link href="http://localhost/pages/" hreflang="en" rel="alternate" />
<xhtml:link href="http://localhost/ja/pages/" hreflang="ja" rel="alternate" />
</url>
</urlset>
`.trim();
t.deepEqual(sitemapContent, sitemapExpected);
});

Expand All @@ -33,9 +33,6 @@ test('SitemapPlugin: robots.txt', async (t: ExecutionContext) => {
await pod.router.warmup();
const robotsRoute = await pod.router.resolve('/bar/robots.txt') as Route;
const robotsContent = await robotsRoute.build();
const robotsExpected = `
User-agent: *
Allow: /
`;
const robotsExpected = 'User-agent: *\nAllow: /';
t.deepEqual(robotsContent, robotsExpected);
});
9 changes: 4 additions & 5 deletions src/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RouteProvider,
Router,
} from '@amagaki/amagaki';
import jsBeautify from 'js-beautify';

export interface SitemapPluginOptions {
sitemapPath?: string;
Expand Down Expand Up @@ -45,10 +46,7 @@ class RobotsTxtRoute extends Route {

async build() {
// TODO: Add Sitemap property, hook into document.
return `
User-agent: *
Allow: /
`;
return 'User-agent: *\nAllow: /';
}
}

Expand Down Expand Up @@ -89,11 +87,12 @@ class SitemapRoute extends Route {
(route as DocumentRoute).locale === this.pod.defaultLocale &&
!route.urlPath.includes('/404/')
);
return (
const text = (
await njk.renderFromString(this.templateSource, {
routes: routes,
pod: this.pod,
})
).replace(/^\s*[\r\n]/gm, '');
return jsBeautify.html(text, {indent_size: 2});
}
}

0 comments on commit eebd05a

Please sign in to comment.