Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow images in content collections folder to be used without relative import prefix #9755

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -20,7 +23,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 @@ -471,7 +471,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)
Loading