Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored and astrobot-houston committed Aug 25, 2022
1 parent cd154e4 commit b680c3e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 48 deletions.
10 changes: 5 additions & 5 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,13 @@ export interface AstroUserConfig {
* }
* }
* ```
*
*
* #### Effect on Astro.url
* Setting `build.format` controls what `Astro.url` is set to during the build. When it is:
* - `directory` - The `Astro.url.pathname` will include a trailing slash to mimic folder behavior; ie `/foo/`.
* - `file` - The `Astro.url.pathname` will include `.html`; ie `/foo.html`.
*
* This means that when you create relative URLs using `new URL('./relative', Astro.url)`, you will get consistent behavior between dev and build.
* - `directory` - The `Astro.url.pathname` will include a trailing slash to mimic folder behavior; ie `/foo/`.
* - `file` - The `Astro.url.pathname` will include `.html`; ie `/foo.html`.
*
* This means that when you create relative URLs using `new URL('./relative', Astro.url)`, you will get consistent behavior between dev and build.
*/
format?: 'file' | 'directory';
};
Expand Down
24 changes: 18 additions & 6 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,21 +244,28 @@ function addPageName(pathname: string, opts: StaticBuildOptions): void {
opts.pageNames.push(pathname.replace(/^\//, ''));
}

function getUrlForPath(pathname: string, base: string, origin: string, format: 'directory' | 'file', routeType: RouteType): URL {
function getUrlForPath(
pathname: string,
base: string,
origin: string,
format: 'directory' | 'file',
routeType: RouteType
): URL {
/**
* Examples:
* pathname: /, /foo
* base: /
*/
const ending = format === 'directory' ? '/' : '.html';
let buildPathname: string;
if(pathname === '/' || pathname === '') {
if (pathname === '/' || pathname === '') {
buildPathname = base;
} else if(routeType === 'endpoint') {
} else if (routeType === 'endpoint') {
const buildPathRelative = removeLeadingForwardSlash(pathname);
buildPathname = base + buildPathRelative;
} else {
const buildPathRelative = removeTrailingForwardSlash(removeLeadingForwardSlash(pathname)) + ending;
const buildPathRelative =
removeTrailingForwardSlash(removeLeadingForwardSlash(pathname)) + ending;
buildPathname = base + buildPathRelative;
}
const url = new URL(buildPathname, origin);
Expand Down Expand Up @@ -312,8 +319,13 @@ async function generatePath(
}

const ssr = opts.astroConfig.output === 'server';
const url = getUrlForPath(pathname, opts.astroConfig.base, origin,
opts.astroConfig.build.format, pageData.route.type);
const url = getUrlForPath(
pathname,
opts.astroConfig.base,
origin,
opts.astroConfig.build.format,
pageData.route.type
);
const options: RenderOptions = {
adapterName: undefined,
links,
Expand Down
74 changes: 37 additions & 37 deletions packages/astro/test/page-format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,50 @@ import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('build.format', () => {
describe('directory', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/page-format/',
});
});
describe('directory', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/page-format/',
});
});

describe('Build', () => {
before(async () => {
await fixture.build();
});
describe('Build', () => {
before(async () => {
await fixture.build();
});

it('relative urls created point to sibling folders', async () => {
let html = await fixture.readFile('/nested/page/index.html');
let $ = cheerio.load(html);
expect($('#another').attr('href')).to.equal('/nested/page/another/');
});
});
});
it('relative urls created point to sibling folders', async () => {
let html = await fixture.readFile('/nested/page/index.html');
let $ = cheerio.load(html);
expect($('#another').attr('href')).to.equal('/nested/page/another/');
});
});
});

describe('file', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/page-format/',
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/page-format/',
build: {
format: 'file',
},
});
});
});
});

describe('Build', () => {
before(async () => {
await fixture.build();
});
describe('Build', () => {
before(async () => {
await fixture.build();
});

it('relative urls created point to sibling folders', async () => {
let html = await fixture.readFile('/nested/page.html');
let $ = cheerio.load(html);
expect($('#another').attr('href')).to.equal('/nested/another/');
});
});
});
it('relative urls created point to sibling folders', async () => {
let html = await fixture.readFile('/nested/page.html');
let $ = cheerio.load(html);
expect($('#another').attr('href')).to.equal('/nested/another/');
});
});
});
});

0 comments on commit b680c3e

Please sign in to comment.