diff options
-rw-r--r-- | .changeset/young-eyes-film.md | 15 | ||||
-rw-r--r-- | packages/astro/src/vite-plugin-markdown/images.ts | 7 | ||||
-rw-r--r-- | packages/astro/test/core-image.test.js | 9 | ||||
-rw-r--r-- | packages/astro/test/fixtures/core-image/src/content/blog/blogfolder.jpg | bin | 0 -> 11677 bytes | |||
-rw-r--r-- | packages/astro/test/fixtures/core-image/src/content/blog/one.md | 2 |
5 files changed, 30 insertions, 3 deletions
diff --git a/.changeset/young-eyes-film.md b/.changeset/young-eyes-film.md new file mode 100644 index 000000000..092da9882 --- /dev/null +++ b/.changeset/young-eyes-film.md @@ -0,0 +1,15 @@ +--- +"astro": minor +--- + +Fixes an issue where images in Markdown required a relative specifier (e.g. `./`) + +Now, you can use the standard `` syntax in Markdown files for images colocated in the same folder: no relative specifier required! + +There is no need to update your project; your existing images will still continue to work. However, you may wish to remove any relative specifiers from these Markdown images as they are no longer necessary: + +```diff +-  ++  +<!-- This dog lives in the same folder as my article! --> +``` diff --git a/packages/astro/src/vite-plugin-markdown/images.ts b/packages/astro/src/vite-plugin-markdown/images.ts index 1488ef6fc..6077556fa 100644 --- a/packages/astro/src/vite-plugin-markdown/images.ts +++ b/packages/astro/src/vite-plugin-markdown/images.ts @@ -4,7 +4,10 @@ export function getMarkdownCodeForImages(imagePaths: MarkdownImagePath[], html: return ` import { getImage } from "astro:assets"; ${imagePaths - .map((entry) => `import Astro__${entry.safeName} from ${JSON.stringify(entry.raw)};`) + .map((entry) => { + const prefix = entry.raw.includes('/') ? '' : './'; + return `import Astro__${entry.safeName} from ${JSON.stringify(prefix + entry.raw)};`; + }) .join('\n')} const images = async function(html) { @@ -23,7 +26,7 @@ export function getMarkdownCodeForImages(imagePaths: MarkdownImagePath[], html: const matchKey = ${rawUrl} + '_' + occurrenceCounter; const imageProps = JSON.parse(match[1].replace(/"/g, '"')); const { src, ...props } = imageProps; - + imageSources[matchKey] = await getImage({src: Astro__${entry.safeName}, ...props}); occurrenceCounter++; } diff --git a/packages/astro/test/core-image.test.js b/packages/astro/test/core-image.test.js index fbaedd9a1..9682f1f1e 100644 --- a/packages/astro/test/core-image.test.js +++ b/packages/astro/test/core-image.test.js @@ -480,7 +480,14 @@ describe('astro:image', () => { it('Adds the <img> tags', () => { let $img = $('img'); - expect($img).to.have.a.lengthOf(7); + expect($img).to.have.a.lengthOf(8); + }); + + it('image in cc folder is processed', () => { + let $imgs = $('img'); + let $blogfolderimg = $($imgs[7]); + expect($blogfolderimg.attr('src')).to.include("blogfolder.jpg"); + expect($blogfolderimg.attr('src').endsWith('f=webp')).to.equal(true); }); it('has proper source for directly used image', () => { diff --git a/packages/astro/test/fixtures/core-image/src/content/blog/blogfolder.jpg b/packages/astro/test/fixtures/core-image/src/content/blog/blogfolder.jpg Binary files differnew file mode 100644 index 000000000..e859ac3c9 --- /dev/null +++ b/packages/astro/test/fixtures/core-image/src/content/blog/blogfolder.jpg diff --git a/packages/astro/test/fixtures/core-image/src/content/blog/one.md b/packages/astro/test/fixtures/core-image/src/content/blog/one.md index 8c6522534..1ddcce919 100644 --- a/packages/astro/test/fixtures/core-image/src/content/blog/one.md +++ b/packages/astro/test/fixtures/core-image/src/content/blog/one.md @@ -12,3 +12,5 @@ refinedImage: ../../assets/penguin1.jpg # A post text here + + |