Skip to content

Commit

Permalink
Dynamic Sitemap - linting (ProjectEvergreen#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstockdi committed Jul 21, 2024
1 parent e1903c3 commit 3de7f8a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 44 deletions.
16 changes: 6 additions & 10 deletions packages/plugin-adapter-sitemap/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs/promises';


async function sitemapAdapter(compilation) {
try {
const { outputDir, projectDirectory } = compilation.context;
Expand All @@ -27,7 +26,6 @@ const greenwoodPluginAdapterSitemap = (options = {}) => [{
}
}];


/*
*
* Sitemap
Expand All @@ -40,16 +38,14 @@ class SitemapResource extends ResourceInterface {
constructor(compilation, options) {
super(compilation, options);
}

async shouldServe(url) {
return url.pathname.endsWith('sitemap.xml')
return url.pathname.endsWith('sitemap.xml');
}


// eslint-disable-next-line no-unused-vars
async serve(url) {

//TODO: check if module exists

const { projectDirectory } = this.compilation.context;

try {
Expand All @@ -58,10 +54,10 @@ class SitemapResource extends ResourceInterface {
return new Response(sitemap, { headers: { 'Content-Type': 'text/xml' } });

} catch (error) {
console.error('Error loading module: ./sitemap.xml.js', error);
return new Response("<error>Sitemap oops.</error>", { headers: { 'Content-Type': 'text/xml' } });
console.error('Error loading module: ./sitemap.xml.js Does it exist?', error);
return new Response('<error>Sitemap oops.</error>', { headers: { 'Content-Type': 'text/xml' } });
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* User Config
* import { greenwoodPluginAdapterSitemap } from '../../../src/index.js';
*
*
* export default {
* plugins: [
* greenwoodPluginAdapterSitemap()
Expand All @@ -22,12 +22,9 @@
*/
import chai from 'chai';
import fs from 'fs/promises';
import glob from 'glob-promise';
import { JSDOM } from 'jsdom';
import path from 'path';
import { checkResourceExists } from '../../../../cli/src/lib/resource-utils.js';
import { getSetupFiles, getOutputTeardownFiles } from '../../../../../test/utils.js';
import { normalizePathnameForWindows } from '../../../../cli/src/lib/resource-utils.js';
import { getSetupFiles } from '../../../../../test/utils.js';
import { Runner } from 'gallinago';
import { fileURLToPath } from 'url';

Expand All @@ -37,8 +34,8 @@ describe('Build Greenwood With: ', function() {
const LABEL = 'Sitemap Adapter plugin output';
const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js');
const outputPath = fileURLToPath(new URL('.', import.meta.url));
const publicDir = path.join(outputPath, 'public')
const publicDir = path.join(outputPath, 'public');

let runner;

before(function() {
Expand All @@ -56,7 +53,7 @@ describe('Build Greenwood With: ', function() {

describe('sitemap.xml', function() {
it('should be present', async function() {
const sitemapPath = path.join(publicDir, 'sitemap.xml')
const sitemapPath = path.join(publicDir, 'sitemap.xml');

const itExists = await checkResourceExists(new URL(`file://${sitemapPath}`));
expect(itExists).to.be.equal(true);
Expand All @@ -66,15 +63,13 @@ describe('Build Greenwood With: ', function() {
it('should have the correct first element in the list', async function() {
const sitemapPath = path.join(publicDir, 'sitemap.xml');
const text = await fs.readFile(sitemapPath, 'utf8');



const regex = /<loc>(http:\/\/www\.example\.com\/about\/)<\/loc>/;
const match = text.match(regex);

expect(match[1]).to.equal('http://www.example.com/about/');
});


});

});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
async function generateSitemap(compilation){
const urls = compilation.graph.map((page) => {
return ` <url>
<loc>http://www.example.com${page.route}</loc>
async function generateSitemap(compilation) {
const urls = compilation.graph.map((page) => {
return ` <url>
<loc>http://www.example.com${page.route}</loc>
</url>`;
});
return `
});
return `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls.join('\n')}
</urlset>
`;
}
`;
}



export { generateSitemap };
export { generateSitemap };
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const expect = chai.expect;
describe('Develop Sitemap With: ', function() {

const LABEL = 'Sitemap Resource plugin output';

const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js');
const outputPath = fileURLToPath(new URL('.', import.meta.url));
const hostname = 'http://localhost';
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('Develop Sitemap With: ', function() {
const match = text.match(regex);

expect(match[1]).to.equal('http://www.example.com/about/');

});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
async function generateSitemap(compilation){
const urls = compilation.graph.map((page) => {
return ` <url>
<loc>http://www.example.com${page.route}</loc>
async function generateSitemap(compilation) {
const urls = compilation.graph.map((page) => {
return ` <url>
<loc>http://www.example.com${page.route}</loc>
</url>`;
});
return `
});

return `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls.join('\n')}
</urlset>
`;
}


}

export { generateSitemap };
export { generateSitemap };

0 comments on commit 3de7f8a

Please sign in to comment.