Skip to content

Commit

Permalink
Merge pull request #230 from jaredwray/adding-in-sitemap
Browse files Browse the repository at this point in the history
adding in sitemap
  • Loading branch information
jaredwray authored Jan 5, 2024
2 parents 07f6baa + 3a9411d commit 213b01b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
38 changes: 34 additions & 4 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {type GithubData, Github, type GithubOptions} from './github.js';
export type WritrData = {
github?: GithubData;
templates?: WritrTemplates;
options: WritrOptions;
};

export type WritrTemplates = {
Expand All @@ -29,12 +30,14 @@ export class WritrBuilder {
public async build(): Promise<void> {
// Validate the options
this.validateOptions(this.options);
// Set the site options
const writrData: WritrData = {
options: this.options,
};
// Get data from github
const githubData = await this.getGithubData(this.options.githubPath);
// Get data of the site
const writrData: WritrData = {
github: githubData,
};
writrData.github = githubData;
// Get the templates to use
writrData.templates = await this.getTemplates(this.options);

Expand All @@ -45,8 +48,9 @@ export class WritrBuilder {
// build the rss feed (/rss.xml)

// build the sitemap (/sitemap.xml)
await this.buildSiteMapPage(writrData);

// build the robots.txt (/robots.txt)
// Build the robots.txt (/robots.txt)
await this.buildRobotsPage(this.options);

console.log('build');
Expand Down Expand Up @@ -126,4 +130,30 @@ export class WritrBuilder {

await (await fs.pathExists(`${sitePath}/robots.txt`) ? fs.copy(`${sitePath}/robots.txt`, robotsPath) : fs.writeFile(robotsPath, 'User-agent: *\nDisallow:'));
}

public async buildSiteMapPage(data: WritrData): Promise<void> {
const {siteUrl} = data.options;
const {outputPath} = data.options;

const sitemapPath = `${outputPath}/sitemap.xml`;
const urls = [
{url: siteUrl},
{url: `${siteUrl}/releases`},
];

let xml = '<?xml version="1.0" encoding="UTF-8"?>';
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

for (const {url} of urls) {
xml += '<url>';
xml += `<loc>${url}</loc>`;
xml += '</url>';
}

xml += '</urlset>';

await fs.ensureDir(outputPath);

await fs.writeFile(sitemapPath, xml, 'utf8');
}
}
20 changes: 19 additions & 1 deletion test/builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect, it, describe} from 'vitest';
import * as fs from 'fs-extra';
import {WritrBuilder} from '../src/builder.js';
import {WritrBuilder, type WritrData} from '../src/builder.js';
import {WritrOptions} from '../src/options.js';

describe('WritrBuilder', () => {
Expand Down Expand Up @@ -136,4 +136,22 @@ describe('WritrBuilder', () => {
await fs.remove(options.outputPath);
}
});
it('should build the sitemap.xml (/sitemap.xml)', async () => {
const builder = new WritrBuilder();
const data: WritrData = {
options: new WritrOptions(),
};
data.options.sitePath = 'test/fixtures/single-page-site';
data.options.outputPath = 'test/temp-sitemap-test';
data.options.siteUrl = 'http://foo.com';

await fs.remove(data.options.outputPath);
try {
await builder.buildSiteMapPage(data);
const sitemap = await fs.readFile(`${data.options.outputPath}/sitemap.xml`, 'utf8');
expect(sitemap).toContain('<loc>http://foo.com</loc>');
} finally {
await fs.remove(data.options.outputPath);
}
});
});

0 comments on commit 213b01b

Please sign in to comment.