Skip to content

Commit

Permalink
Allow images in content collections folder to be used without relativ…
Browse files Browse the repository at this point in the history
…e import prefix (#9755)

* adds the ./ prefix to the import statement for user

* remove accidental new line added

* add tests

* add changeset

* Update young-eyes-film.md

Co-authored-by: Sarah Rainsberger <[email protected]>

---------

Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
OliverSpeir and sarah11918 authored Jan 31, 2024
1 parent 7d937c1 commit d4b8861
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
15 changes: 15 additions & 0 deletions .changeset/young-eyes-film.md
Original file line number Diff line number Diff line change
@@ -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 `![](img.png)` 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
- ![A cute dog](./dog.jpg)
+ ![A cute dog](dog.jpg)
<!-- This dog lives in the same folder as my article! -->
```
7 changes: 5 additions & 2 deletions packages/astro/src/vite-plugin-markdown/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -23,7 +26,7 @@ export function getMarkdownCodeForImages(imagePaths: MarkdownImagePath[], html:
const matchKey = ${rawUrl} + '_' + occurrenceCounter;
const imageProps = JSON.parse(match[1].replace(/&#x22;/g, '"'));
const { src, ...props } = imageProps;
imageSources[matchKey] = await getImage({src: Astro__${entry.safeName}, ...props});
occurrenceCounter++;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ refinedImage: ../../assets/penguin1.jpg
# A post

text here

![](blogfolder.jpg)

0 comments on commit d4b8861

Please sign in to comment.