-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: component styles within imported markdown files (#3116)
* fix: replace markdown path prefix with suffix flag * fix: avoid non-encoded colons for flag * fix: remove needless ? * fix: dev server load order * fix: production build crawl dynamic imports * fix: remove unused virtual_module_id const * fix: remove unsafe "!" on getmodbyid * fix: remove needless @id path check * fix: add list of SSR-able file extensions * docs: virtual_mod_id change * fix: support id prefix on resolved ids * fix: switch to ?mdImport flag to resolve glob imports * tests: imported md styles for dev and build * chore: changeset
- Loading branch information
1 parent
dfa1042
commit 44bacd2
Showing
13 changed files
with
162 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fix: Astro components used in dynamically imported markdown (ex. Astro.glob('\*.md') will now retain their CSS styles in dev and production builds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { expect } from 'chai'; | ||
import cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
let fixture; | ||
const IMPORTED_ASTRO_COMPONENT_ID = 'imported-astro-component' | ||
|
||
describe('Imported markdown CSS', function () { | ||
before(async () => { | ||
fixture = await loadFixture({ root: './fixtures/astro-markdown-css/' }); | ||
}); | ||
describe('build', () => { | ||
let $; | ||
let bundledCSS; | ||
|
||
before(async () => { | ||
this.timeout(45000); // test needs a little more time in CI | ||
await fixture.build(); | ||
|
||
// get bundled CSS (will be hashed, hence DOM query) | ||
const html = await fixture.readFile('/index.html'); | ||
$ = cheerio.load(html); | ||
const bundledCSSHREF = $('link[rel=stylesheet][href^=/assets/]').attr('href'); | ||
bundledCSS = await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/')); | ||
}); | ||
|
||
it('Compiles styles for Astro components within imported markdown', () => { | ||
const importedAstroComponent = $(`#${IMPORTED_ASTRO_COMPONENT_ID}`)?.[0] | ||
expect(importedAstroComponent?.name).to.equal('h2') | ||
const cssClass = $(importedAstroComponent).attr('class')?.split(/\s+/)?.[0] | ||
|
||
expect(bundledCSS).to.match(new RegExp(`h2.${cssClass}{color:#00f}`)) | ||
}); | ||
}); | ||
describe('dev', () => { | ||
let devServer; | ||
let $; | ||
|
||
before(async () => { | ||
devServer = await fixture.startDevServer(); | ||
const html = await fixture.fetch('/').then((res) => res.text()); | ||
$ = cheerio.load(html); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
it('Compiles styles for Astro components within imported markdown', async () => { | ||
const importedAstroComponent = $(`#${IMPORTED_ASTRO_COMPONENT_ID}`)?.[0] | ||
expect(importedAstroComponent?.name).to.equal('h2') | ||
const cssClass = $(importedAstroComponent).attr('class')?.split(/\s+/)?.[0] | ||
|
||
const astroCSSHREF = $('link[rel=stylesheet][href^=/src/components/Visual.astro]').attr('href'); | ||
const css = await fixture.fetch(astroCSSHREF.replace(/^\/?/, '/')).then((res) => res.text()); | ||
expect(css).to.match(new RegExp(`h2.${cssClass}{color:#00f}`)); | ||
}); | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/astro/test/fixtures/astro-markdown-css/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [] | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/astro/test/fixtures/astro-markdown-css/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@test/astro-markdown-css", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"build": "astro build", | ||
"dev": "astro dev" | ||
}, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/astro/test/fixtures/astro-markdown-css/src/components/Visual.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<h2 id="imported-astro-component">I'm a visual!</h2> | ||
|
||
<style> | ||
h2 { | ||
color: #00f; | ||
} | ||
</style> |
9 changes: 9 additions & 0 deletions
9
packages/astro/test/fixtures/astro-markdown-css/src/markdown/article.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
setup: import Visual from '../components/Visual.astro' | ||
--- | ||
|
||
# Example markdown document, with a Visual | ||
|
||
<Visual /> | ||
<Visual /> | ||
<Visual /> |
9 changes: 9 additions & 0 deletions
9
packages/astro/test/fixtures/astro-markdown-css/src/markdown/article2.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
setup: import Visual from '../components/Visual.astro' | ||
--- | ||
|
||
# Example markdown document, with a more Visuals | ||
|
||
<Visual /> | ||
<Visual /> | ||
<Visual /> |
15 changes: 15 additions & 0 deletions
15
packages/astro/test/fixtures/astro-markdown-css/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
const markdownDocs = await Astro.glob('../markdown/*.md') | ||
const article2 = await import('../markdown/article2.md') | ||
--- | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>Astro</title> | ||
</head> | ||
<body> | ||
{markdownDocs.map(markdownDoc => <><h2>{markdownDoc.url}</h2><markdownDoc.Content /></>)} | ||
<article2.Content /> | ||
</body> | ||
</html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.