[ci] format

This commit is contained in:
matthewp 2022-08-30 15:47:42 +00:00 committed by fredkbot
parent 2f05f5d307
commit 65cc3f64f6
3 changed files with 29 additions and 22 deletions

View file

@ -240,14 +240,21 @@ interface GeneratePathOptions {
renderers: SSRLoadedRenderer[]; renderers: SSRLoadedRenderer[];
} }
function shouldAppendForwardSlash(trailingSlash: AstroConfig['trailingSlash'], buildFormat: AstroConfig['build']['format']): boolean { function shouldAppendForwardSlash(
switch(trailingSlash) { trailingSlash: AstroConfig['trailingSlash'],
case 'always': return true; buildFormat: AstroConfig['build']['format']
case 'never': return false; ): boolean {
switch (trailingSlash) {
case 'always':
return true;
case 'never':
return false;
case 'ignore': { case 'ignore': {
switch(buildFormat) { switch (buildFormat) {
case 'directory': return true; case 'directory':
case 'file': return false; return true;
case 'file':
return false;
} }
} }
} }
@ -256,9 +263,9 @@ function shouldAppendForwardSlash(trailingSlash: AstroConfig['trailingSlash'], b
function addPageName(pathname: string, opts: StaticBuildOptions): void { function addPageName(pathname: string, opts: StaticBuildOptions): void {
const trailingSlash = opts.astroConfig.trailingSlash; const trailingSlash = opts.astroConfig.trailingSlash;
const buildFormat = opts.astroConfig.build.format; const buildFormat = opts.astroConfig.build.format;
const pageName = shouldAppendForwardSlash(trailingSlash, buildFormat) ? const pageName = shouldAppendForwardSlash(trailingSlash, buildFormat)
pathname.replace(/\/?$/, '/').replace(/^\//, '') : ? pathname.replace(/\/?$/, '/').replace(/^\//, '')
pathname.replace(/^\//, '') : pathname.replace(/^\//, '');
opts.pageNames.push(pageName); opts.pageNames.push(pageName);
} }

View file

@ -19,12 +19,12 @@ export function loadFixture(inlineConfig) {
export function readXML(fileOrPromise) { export function readXML(fileOrPromise) {
const parseString = xml2js.parseString; const parseString = xml2js.parseString;
return Promise.resolve(fileOrPromise).then(xml => { return Promise.resolve(fileOrPromise).then((xml) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
parseString(xml, function (err, result) { parseString(xml, function (err, result) {
if(err) return reject(err); if (err) return reject(err);
resolve(result); resolve(result);
});
}); });
})
}); });
} }

View file

@ -12,12 +12,12 @@ describe('Trailing slash', () => {
root: './fixtures/trailing-slash/', root: './fixtures/trailing-slash/',
trailingSlash: 'ignore', trailingSlash: 'ignore',
build: { build: {
format: 'directory' format: 'directory',
} },
}); });
await fixture.build(); await fixture.build();
}); });
it('URLs end with trailing slash', async () => { it('URLs end with trailing slash', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml')); const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const urls = data.urlset.url; const urls = data.urlset.url;
@ -31,15 +31,15 @@ describe('Trailing slash', () => {
root: './fixtures/trailing-slash/', root: './fixtures/trailing-slash/',
trailingSlash: 'ignore', trailingSlash: 'ignore',
build: { build: {
format: 'file' format: 'file',
} },
}); });
await fixture.build(); await fixture.build();
}); });
it('URLs do not end with trailing slash', async () => { it('URLs do not end with trailing slash', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml')); const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const urls = data.urlset.url; const urls = data.urlset.url;
expect(urls[0].loc[0]).to.equal('http://example.com/one'); expect(urls[0].loc[0]).to.equal('http://example.com/one');
}); });
}); });
@ -53,7 +53,7 @@ describe('Trailing slash', () => {
}); });
await fixture.build(); await fixture.build();
}); });
it('URLs do no end with trailing slash', async () => { it('URLs do no end with trailing slash', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml')); const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const urls = data.urlset.url; const urls = data.urlset.url;
@ -69,7 +69,7 @@ describe('Trailing slash', () => {
}); });
await fixture.build(); await fixture.build();
}); });
it('URLs end with trailing slash', async () => { it('URLs end with trailing slash', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml')); const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const urls = data.urlset.url; const urls = data.urlset.url;