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

Handle encoded image paths in markdown #11310

Merged
merged 1 commit into from
Jun 21, 2024
Merged

Handle encoded image paths in markdown #11310

merged 1 commit into from
Jun 21, 2024

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Jun 21, 2024

Changes

fix #11291

The markdown vfile data.imagePaths now contains decoded paths instead of possibly-encoded paths. I'm not sure if this is ideal/intentional for the option itself, but this top-level change helps align all downstream usage:

  1. Printing decoded paths in the imports allow Vite to properly resolve assets (Also a Vite bug)

${imagePaths
.map((entry) => `import Astro__${entry.safeName} from ${JSON.stringify(entry.raw)};`)
.join('\n')}

  1. MDX rehypeImageToComponent currently compares with decoded paths, the generated imports are also decoded:

const src = decodeURI(String(node.properties.src));
if (!file.data.imagePaths.has(src)) return;

  1. rehype-images is also comparing with decoded paths:

node.properties.src = decodeURI(node.properties.src);
if (file.data.imagePaths?.has(node.properties.src)) {

  1. Encoded paths also messes with this regex generation (that I don't complete understand yet):

${imagePaths
.map((entry) => {
const rawUrl = JSON.stringify(entry.raw);
return `{
const regex = new RegExp('__ASTRO_IMAGE_="([^"]*' + ${rawUrl.replace(
/[.*+?^${}()|[\]\\]/g,
'\\\\$&'
)} + '[^"]*)"', 'g');
let match;


So with the above discoveries, I made this PR change instead of keeping imagePaths encoded only.

Testing

Added new tests

Docs

n/a. bug fix.

Copy link

changeset-bot bot commented Jun 21, 2024

🦋 Changeset detected

Latest commit: 93d068a

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added feat: markdown Related to Markdown (scope) pkg: astro Related to the core `astro` package (scope) labels Jun 21, 2024
@ematipico ematipico merged commit b6afe6a into main Jun 21, 2024
13 checks passed
@ematipico ematipico deleted the encode-image-path branch June 21, 2024 16:13
@astrobot-houston astrobot-houston mentioned this pull request Jun 21, 2024
This was referenced Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: markdown Related to Markdown (scope) pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Markdown image not found when file name contains a space
3 participants