From 1a86e77c37109f6a6006d6acb00eb52b4a54683c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Seery?= Date: Mon, 25 Apr 2022 13:38:46 -0300 Subject: [PATCH] fix(markdown): file.url fixes (#3198) * fix(markdown): file.url fixes * Added tests * Changed the default of `base` from `./` to `/` * Make the url work with subpath * Changeset * Forgot to change this comparison --- .changeset/large-birds-repair.md | 5 + packages/astro/src/core/build/generate.ts | 2 +- packages/astro/src/core/config.ts | 4 +- .../src/vite-plugin-astro-server/index.ts | 3 +- .../astro/src/vite-plugin-markdown/index.ts | 15 ++- .../astro/test/astro-markdown-url.test.js | 100 ++++++++++++++++++ .../fixtures/astro-markdown-url/.gitignore | 6 ++ .../fixtures/astro-markdown-url/package.json | 8 ++ .../pages/docs/pages/how-to-make-a-page.md | 5 + .../astro-markdown-url/src/pages/index.astro | 5 + pnpm-lock.yaml | 6 ++ 11 files changed, 149 insertions(+), 10 deletions(-) create mode 100644 .changeset/large-birds-repair.md create mode 100644 packages/astro/test/astro-markdown-url.test.js create mode 100644 packages/astro/test/fixtures/astro-markdown-url/.gitignore create mode 100644 packages/astro/test/fixtures/astro-markdown-url/package.json create mode 100644 packages/astro/test/fixtures/astro-markdown-url/src/pages/docs/pages/how-to-make-a-page.md create mode 100644 packages/astro/test/fixtures/astro-markdown-url/src/pages/index.astro diff --git a/.changeset/large-birds-repair.md b/.changeset/large-birds-repair.md new file mode 100644 index 000000000000..669ab6b86a10 --- /dev/null +++ b/.changeset/large-birds-repair.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Markdown file.url now respects `trailingSlash` and `base` diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index e76d51ca38f7..d68f8faf30d4 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -179,7 +179,7 @@ async function generatePath( // If a base path was provided, append it to the site URL. This ensures that // all injected scripts and links are referenced relative to the site and subpath. const site = - astroConfig.base && astroConfig.base !== './' + astroConfig.base !== '/' ? joinPaths(astroConfig.site?.toString() || 'http://localhost/', astroConfig.base) : astroConfig.site; const links = createLinkStylesheetElementSet(linkIds.reverse(), site); diff --git a/packages/astro/src/core/config.ts b/packages/astro/src/core/config.ts index 57b45ef1b42d..de947530d888 100644 --- a/packages/astro/src/core/config.ts +++ b/packages/astro/src/core/config.ts @@ -94,8 +94,8 @@ export const AstroConfigSchema = z.object({ base: z .string() .optional() - .default('./') - .transform((val) => (val ? appendForwardSlash(trimSlashes(val)) : val)), + .default('/') + .transform((val) => appendForwardSlash(trimSlashes(val))), trailingSlash: z .union([z.literal('always'), z.literal('never'), z.literal('ignore')]) .optional() diff --git a/packages/astro/src/vite-plugin-astro-server/index.ts b/packages/astro/src/vite-plugin-astro-server/index.ts index d21fa7e8eb89..0fdb8705b778 100644 --- a/packages/astro/src/vite-plugin-astro-server/index.ts +++ b/packages/astro/src/vite-plugin-astro-server/index.ts @@ -114,8 +114,7 @@ async function handle404Response( // HACK: redirect without the base path for assets in publicDir const redirectTo = req.method === 'GET' && - config.base && - config.base !== './' && + config.base !== '/' && pathname.startsWith(config.base) && pathname.replace(config.base, '/'); diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 231977002a36..105ff47e093c 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -78,13 +78,18 @@ export default function markdown({ config }: AstroPluginOptions): Plugin { // Return the file's JS representation, including all Markdown // frontmatter and a deferred `import() of the compiled markdown content. if (id.endsWith(`.md${MARKDOWN_IMPORT_FLAG}`)) { - const sitePathname = config.site - ? appendForwardSlash(new URL(config.base, config.site).pathname) - : '/'; + const sitePathname = appendForwardSlash( + config.site ? new URL(config.base, config.site).pathname : config.base + ); + const fileId = id.replace(MARKDOWN_IMPORT_FLAG, ''); - const fileUrl = fileId.includes('/pages/') - ? fileId.replace(/^.*\/pages\//, sitePathname).replace(/(\/index)?\.md$/, '') + let fileUrl = fileId.includes('/pages/') + ? fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.md$/, '') : undefined; + if (fileUrl && config.trailingSlash === 'always') { + fileUrl = appendForwardSlash(fileUrl); + } + const source = await fs.promises.readFile(fileId, 'utf8'); const { data: frontmatter } = matter(source); return { diff --git a/packages/astro/test/astro-markdown-url.test.js b/packages/astro/test/astro-markdown-url.test.js new file mode 100644 index 000000000000..54ce8bfba496 --- /dev/null +++ b/packages/astro/test/astro-markdown-url.test.js @@ -0,0 +1,100 @@ +import { expect } from 'chai'; +import cheerio from 'cheerio'; +import { loadFixture } from './test-utils.js'; + +describe('Astro Markdown URL', () => { + describe('With subpath', () => { + const baseUrl = `/my-cool-base/docs/pages/how-to-make-a-page`; + + it('trailingSlash: always', async () => { + let fixture = await loadFixture({ + root: './fixtures/astro-markdown-url/', + outDir: new URL('./fixtures/astro-markdown-url/with-subpath-always/', import.meta.url), + base: '/my-cool-base', + trailingSlash: 'always', + }); + await fixture.build(); + + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + + expect($('#url').attr('href')).to.equal(baseUrl + '/'); + }); + + it('trailingSlash: never', async () => { + let fixture = await loadFixture({ + root: './fixtures/astro-markdown-url/', + outDir: new URL('./fixtures/astro-markdown-url/with-subpath-never/', import.meta.url), + base: '/my-cool-base', + trailingSlash: 'never', + }); + await fixture.build(); + + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + + expect($('#url').attr('href')).to.equal(baseUrl); + }); + + it('trailingSlash: ignore', async () => { + let fixture = await loadFixture({ + root: './fixtures/astro-markdown-url/', + outDir: new URL('./fixtures/astro-markdown-url/with-subpath-ignore/', import.meta.url), + base: '/my-cool-base', + trailingSlash: 'ignore', + }); + await fixture.build(); + + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + + expect($('#url').attr('href')).to.equal(baseUrl); + }); + }); + + describe('Without subpath', () => { + const baseUrl = `/docs/pages/how-to-make-a-page`; + + it('trailingSlash: always', async () => { + let fixture = await loadFixture({ + root: './fixtures/astro-markdown-url/', + outDir: new URL('./fixtures/astro-markdown-url/without-subpath-always/', import.meta.url), + trailingSlash: 'always', + }); + await fixture.build(); + + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + + expect($('#url').attr('href')).to.equal(baseUrl + '/'); + }); + + it('trailingSlash: never', async () => { + let fixture = await loadFixture({ + root: './fixtures/astro-markdown-url/', + outDir: new URL('./fixtures/astro-markdown-url/without-subpath-never/', import.meta.url), + trailingSlash: 'never', + }); + await fixture.build(); + + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + + expect($('#url').attr('href')).to.equal(baseUrl); + }); + + it('trailingSlash: ignore', async () => { + let fixture = await loadFixture({ + root: './fixtures/astro-markdown-url/', + outDir: new URL('./fixtures/astro-markdown-url/without-subpath-ignore/', import.meta.url), + trailingSlash: 'ignore', + }); + await fixture.build(); + + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + + expect($('#url').attr('href')).to.equal(baseUrl); + }); + }); +}); diff --git a/packages/astro/test/fixtures/astro-markdown-url/.gitignore b/packages/astro/test/fixtures/astro-markdown-url/.gitignore new file mode 100644 index 000000000000..dad8532948d7 --- /dev/null +++ b/packages/astro/test/fixtures/astro-markdown-url/.gitignore @@ -0,0 +1,6 @@ +with-subpath-always/ +with-subpath-never/ +with-subpath-ignore/ +without-subpath-always/ +without-subpath-never/ +without-subpath-ignore/ diff --git a/packages/astro/test/fixtures/astro-markdown-url/package.json b/packages/astro/test/fixtures/astro-markdown-url/package.json new file mode 100644 index 000000000000..341814951a03 --- /dev/null +++ b/packages/astro/test/fixtures/astro-markdown-url/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/astro-markdown-url", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/astro-markdown-url/src/pages/docs/pages/how-to-make-a-page.md b/packages/astro/test/fixtures/astro-markdown-url/src/pages/docs/pages/how-to-make-a-page.md new file mode 100644 index 000000000000..901c35955aec --- /dev/null +++ b/packages/astro/test/fixtures/astro-markdown-url/src/pages/docs/pages/how-to-make-a-page.md @@ -0,0 +1,5 @@ +--- +title: How to make a page +--- + +Test diff --git a/packages/astro/test/fixtures/astro-markdown-url/src/pages/index.astro b/packages/astro/test/fixtures/astro-markdown-url/src/pages/index.astro new file mode 100644 index 000000000000..c6a77b0e1a59 --- /dev/null +++ b/packages/astro/test/fixtures/astro-markdown-url/src/pages/index.astro @@ -0,0 +1,5 @@ +--- +import * as page from './docs/pages/how-to-make-a-page.md' +--- + +{page.frontmatter.title} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4a470892db1e..baa7a59ca2d4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -860,6 +860,12 @@ importers: dependencies: astro: link:../../../.. + packages/astro/test/fixtures/astro-markdown-url: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/astro-page-directory-url: specifiers: astro: workspace:*