From 3ad0aac8cfc23893a2a82b5da60099648572d839 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Tue, 4 May 2021 17:11:05 -0500 Subject: [PATCH] Fix `fetchContent` for nested files (#171) * fix: fetchContent within nested folders * feat: add `sourceId` to fetchContent * test: update collection tests * fix: windows compat for importPath * chore: add changeset * fix: astroRoot => projectRoot * feat: add `slash` package * chore: remove sourceId prop * chore: update changeset * format --- .changeset/polite-sheep-raise.md | 5 ++++ .../src/pages/project/nested/lunar-eclipse.md | 23 +++++++++++++++++++ packages/astro/package.json | 5 ++-- .../astro/src/compiler/codegen/content.ts | 13 ++++++++--- packages/astro/src/compiler/codegen/index.ts | 4 ++-- .../astro/src/compiler/transform/styles.ts | 3 ++- packages/astro/test/astro-collection.test.js | 2 +- .../astro-collection/src/pages/$posts.astro | 2 +- .../src/pages/post/nested/a.md | 8 +++++++ yarn.lock | 5 ++++ 10 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 .changeset/polite-sheep-raise.md create mode 100644 examples/portfolio/src/pages/project/nested/lunar-eclipse.md create mode 100644 packages/astro/test/fixtures/astro-collection/src/pages/post/nested/a.md diff --git a/.changeset/polite-sheep-raise.md b/.changeset/polite-sheep-raise.md new file mode 100644 index 000000000..8d9fabee2 --- /dev/null +++ b/.changeset/polite-sheep-raise.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix `fetchContent` API bug for nested `.md` files diff --git a/examples/portfolio/src/pages/project/nested/lunar-eclipse.md b/examples/portfolio/src/pages/project/nested/lunar-eclipse.md new file mode 100644 index 000000000..43a016e15 --- /dev/null +++ b/examples/portfolio/src/pages/project/nested/lunar-eclipse.md @@ -0,0 +1,23 @@ +--- +layout: ../../../layouts/project.astro +title: Lunar Eclipse +client: Self +published_at: 2020-03-04 00:00:00 +img: https://images.unsplash.com/photo-1548391350-1a529f6ea42d?fit=crop&w=1400&h=700&q=75 +description: | + We took some cool pictures of the moon and made a website about it. +tags: + - design + - dev + - branding +--- + +Rubber cheese mascarpone cut the cheese. Jarlsberg parmesan cheesy grin cream cheese port-salut stinking bishop ricotta brie. Roquefort when the cheese comes out everybody's happy goat cheese triangles stilton cheese and biscuits goat babybel. Bocconcini roquefort queso danish fontina pecorino. + +Smelly cheese stinking bishop roquefort. Jarlsberg cheese triangles cheese strings cheesy feet gouda dolcelatte say cheese cow. Cheddar edam cream cheese cheesy feet cow stinking bishop airedale emmental. Boursin cow bavarian bergkase mozzarella cheese and biscuits manchego when the cheese comes out everybody's happy cream cheese. Cheese on toast st. agur blue cheese croque monsieur halloumi. + +Fromage frais jarlsberg st. agur blue cheese. Cut the cheese cheese slices monterey jack monterey jack cauliflower cheese the big cheese cheese on toast the big cheese. Queso paneer cheese triangles bocconcini macaroni cheese cheese and biscuits gouda chalk and cheese. Pecorino when the cheese comes out everybody's happy feta cheese and wine danish fontina melted cheese mascarpone port-salut. When the cheese comes out everybody's happy pecorino cottage cheese. + +Caerphilly parmesan manchego. Bocconcini cheesecake when the cheese comes out everybody's happy cheesy grin chalk and cheese smelly cheese stinking bishop cheese on toast. Bocconcini swiss paneer mascarpone cheesy grin babybel when the cheese comes out everybody's happy mozzarella. Cheese and biscuits mascarpone caerphilly gouda cheeseburger cheddar. + +Cheese and biscuits cheesy grin roquefort. Ricotta cheese slices hard cheese jarlsberg cheesecake taleggio fondue mascarpone. Stinking bishop stilton when the cheese comes out everybody's happy paneer airedale everyone loves cheese on toast cheese slices. Ricotta cut the cheese cheese triangles babybel cream cheese ricotta. diff --git a/packages/astro/package.json b/packages/astro/package.json index 1979ebdd1..a19818ecd 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -78,8 +78,8 @@ "rollup-plugin-terser": "^7.0.2", "sass": "^1.32.8", "snowpack": "^3.3.7", - "string-width": "^5.0.0", "source-map-support": "^0.5.19", + "string-width": "^5.0.0", "svelte": "^3.35.0", "unified": "^9.2.1", "vue": "^3.0.10", @@ -98,7 +98,8 @@ "@types/react-dom": "^17.0.2", "@types/sass": "^1.16.0", "@types/yargs-parser": "^20.2.0", - "astro-scripts": "0.0.1" + "astro-scripts": "0.0.1", + "slash": "^4.0.0" }, "engines": { "node": ">=14.0.0", diff --git a/packages/astro/src/compiler/codegen/content.ts b/packages/astro/src/compiler/codegen/content.ts index fb8f9e307..4c542671b 100644 --- a/packages/astro/src/compiler/codegen/content.ts +++ b/packages/astro/src/compiler/codegen/content.ts @@ -1,5 +1,7 @@ import path from 'path'; import { fdir, PathsOutput } from 'fdir'; +import { fileURLToPath } from 'url'; +import slash from 'slash'; /** * Handling for import.meta.glob and import.meta.globEager @@ -8,6 +10,7 @@ import { fdir, PathsOutput } from 'fdir'; interface GlobOptions { namespace: string; filename: string; + projectRoot: URL; } interface GlobResult { @@ -39,13 +42,16 @@ function globSearch(spec: string, { filename }: { filename: string }): string[] } const cwd = path.join(path.dirname(filename), globDir.replace(/\//g, path.sep)); // this must match OS (could be '/' or '\') - let found = crawler.glob(glob).crawl(cwd).sync() as PathsOutput; + let found = crawler + .glob(glob) + .crawlWithOptions(cwd, { includeBasePath: true }) + .sync() as PathsOutput; if (!found.length) { throw new Error(`No files matched "${spec}" from ${filename}`); } - return found.map((importPath) => { + return found.map(importPath => { if (importPath.startsWith('http') || importPath.startsWith('.')) return importPath; - return `./` + globDir + '/' + importPath; + return './' + path.posix.join(globDir, path.posix.relative(slash(cwd), importPath)); }); } catch (err) { throw new Error(`No files matched "${spec}" from ${filename}`); @@ -65,6 +71,7 @@ export function fetchContent(spec: string, { namespace, filename }: GlobOptions) // add URL if this appears within the /pages/ directory (probably can be improved) const fullPath = path.resolve(path.dirname(filename), importPath); + if (fullPath.includes(`${path.sep}pages${path.sep}`)) { const url = importPath.replace(/^\./, '').replace(/\.md$/, ''); imports.add(`${id}.url = '${url}';`); diff --git a/packages/astro/src/compiler/codegen/index.ts b/packages/astro/src/compiler/codegen/index.ts index 6caed85a3..a0f8e6b90 100644 --- a/packages/astro/src/compiler/codegen/index.ts +++ b/packages/astro/src/compiler/codegen/index.ts @@ -477,7 +477,7 @@ function compileModule(module: Script, state: CodegenState, compileOptions: Comp const spec = (init as any).arguments[0].value; if (typeof spec !== 'string') break; - const globResult = fetchContent(spec, { namespace, filename: state.filename }); + const globResult = fetchContent(spec, { namespace, filename: state.filename, projectRoot: compileOptions.astroConfig.projectRoot }); let imports = ''; for (const importStatement of globResult.imports) { @@ -496,7 +496,7 @@ function compileModule(module: Script, state: CodegenState, compileOptions: Comp // Astro.fetchContent() for (const [namespace, { spec }] of contentImports.entries()) { - const globResult = fetchContent(spec, { namespace, filename: state.filename }); + const globResult = fetchContent(spec, { namespace, filename: state.filename, projectRoot: compileOptions.astroConfig.projectRoot }); for (const importStatement of globResult.imports) { state.importExportStatements.add(importStatement); } diff --git a/packages/astro/src/compiler/transform/styles.ts b/packages/astro/src/compiler/transform/styles.ts index efabf11fe..4e665fc61 100644 --- a/packages/astro/src/compiler/transform/styles.ts +++ b/packages/astro/src/compiler/transform/styles.ts @@ -13,6 +13,7 @@ import type { TransformOptions, Transformer } from '../../@types/transformer'; import type { TemplateNode } from 'astro-parser'; import { debug } from '../../logger.js'; import astroScopedStyles, { NEVER_SCOPED_TAGS } from './postcss-scoped-styles/index.js'; +import slash from 'slash'; type StyleType = 'css' | 'scss' | 'sass' | 'postcss'; @@ -40,7 +41,7 @@ const getStyleType: Map = new Map([ function hashFromFilename(filename: string): string { const hash = crypto.createHash('sha256'); return hash - .update(filename.replace(/\\/g, '/')) + .update(slash(filename)) .digest('base64') .toString() .replace(/[^A-Za-z0-9-]/g, '') diff --git a/packages/astro/test/astro-collection.test.js b/packages/astro/test/astro-collection.test.js index 3fdb3b817..72d4f2314 100644 --- a/packages/astro/test/astro-collection.test.js +++ b/packages/astro/test/astro-collection.test.js @@ -15,7 +15,7 @@ Collections('generates list & sorts successfully', async ({ runtime }) => { return $(this).attr('href'); }), ]; - assert.equal(urls, ['/post/three', '/post/two']); + assert.equal(urls, ['/post/nested/a', '/post/three', '/post/two']); }); Collections('generates pagination successfully', async ({ runtime }) => { diff --git a/packages/astro/test/fixtures/astro-collection/src/pages/$posts.astro b/packages/astro/test/fixtures/astro-collection/src/pages/$posts.astro index 4186a9a5c..ccd5d726d 100644 --- a/packages/astro/test/fixtures/astro-collection/src/pages/$posts.astro +++ b/packages/astro/test/fixtures/astro-collection/src/pages/$posts.astro @@ -8,7 +8,7 @@ export async function createCollection() { data.sort((a, b) => new Date(b.date) - new Date(a.date)); return data; }, - pageSize: 2 + pageSize: 3 }; } --- diff --git a/packages/astro/test/fixtures/astro-collection/src/pages/post/nested/a.md b/packages/astro/test/fixtures/astro-collection/src/pages/post/nested/a.md new file mode 100644 index 000000000..10842cae5 --- /dev/null +++ b/packages/astro/test/fixtures/astro-collection/src/pages/post/nested/a.md @@ -0,0 +1,8 @@ +--- +title: Post A +date: 2021-04-16 00:00:00 +--- + +# Post A + +I’m the "A" blog post diff --git a/yarn.lock b/yarn.lock index 515ddf0ca..f10c37dd9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10041,6 +10041,11 @@ slash@^3.0.0: resolved "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz" integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q== +slash@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/slash/-/slash-4.0.0.tgz#2422372176c4c6c5addb5e2ada885af984b396a7" + integrity sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew== + slice-ansi@^2.1.0: version "2.1.0" resolved "https://registry.npmjs.org/slice-ansi/-/slice-ansi-2.1.0.tgz"