-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow remark plugins to affect getImage call for .md files (#9566)
* pass hProperties to getImage for optimized imgs * fix to allow multiple images to have hProps added * update test to reflect new expected result * add comment back in Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com> * add srcset * works on multiple images * fix tests, fix images.ts type and remove console logs * add warning back to images.ts again lol * update changeset to be user oriented * Update calm-socks-shake.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * pass alt through getImage * added fixture and test * update lockfile * fix lockfile again (had installed an extra package during testing and had sharp33 installed) * update test to reflect passing alt through getImage --------- Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
- Loading branch information
1 parent
e9a72d9
commit 165cfc1
Showing
12 changed files
with
215 additions
and
58 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,6 @@ | ||
--- | ||
"@astrojs/markdown-remark": minor | ||
"astro": minor | ||
--- | ||
|
||
Allows remark plugins to pass options specifying how images in `.md` files will be optimized |
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,60 @@ | ||
import { expect } from 'chai'; | ||
import * as cheerio from 'cheerio'; | ||
import { Writable } from 'node:stream'; | ||
|
||
import { Logger } from '../dist/core/logger/core.js'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('astro:image', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
|
||
describe('dev', () => { | ||
/** @type {import('./test-utils').DevServer} */ | ||
let devServer; | ||
/** @type {Array<{ type: any, level: 'error', message: string; }>} */ | ||
let logs = []; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/core-image-remark-imgattr/', | ||
}); | ||
|
||
devServer = await fixture.startDevServer({ | ||
logger: new Logger({ | ||
level: 'error', | ||
dest: new Writable({ | ||
objectMode: true, | ||
write(event, _, callback) { | ||
logs.push(event); | ||
callback(); | ||
}, | ||
}), | ||
}), | ||
}); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
describe('Test image attributes can be added by remark plugins', () => { | ||
let $; | ||
before(async () => { | ||
let res = await fixture.fetch('/'); | ||
let html = await res.text(); | ||
$ = cheerio.load(html); | ||
}); | ||
|
||
it('Image has eager loading meaning getImage passed props it doesnt use through it', async () => { | ||
let $img = $('img'); | ||
expect($img.attr('loading')).to.equal('eager'); | ||
}); | ||
|
||
it('Image src contains w=50 meaning getImage correctly used props added through the remark plugin', async () => { | ||
let $img = $('img'); | ||
expect(new URL($img.attr('src'), 'http://example.com').searchParams.get('w')).to.equal('50'); | ||
}); | ||
}); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/astro/test/fixtures/core-image-remark-imgattr/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,9 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import plugin from "./remarkPlugin" | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
markdown: { | ||
remarkPlugins:[plugin] | ||
} | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/astro/test/fixtures/core-image-remark-imgattr/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,11 @@ | ||
{ | ||
"name": "@test/core-image-remark-imgattr", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
}, | ||
"scripts": { | ||
"dev": "astro dev" | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/astro/test/fixtures/core-image-remark-imgattr/remarkPlugin.js
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,20 @@ | ||
export default function plugin() { | ||
return transformer; | ||
|
||
function transformer(tree) { | ||
function traverse(node) { | ||
if (node.type === "image") { | ||
node.data = node.data || {}; | ||
node.data.hProperties = node.data.hProperties || {}; | ||
node.data.hProperties.loading = "eager"; | ||
node.data.hProperties.width = "50"; | ||
} | ||
|
||
if (node.children) { | ||
node.children.forEach(traverse); | ||
} | ||
} | ||
|
||
traverse(tree); | ||
} | ||
} |
Binary file added
BIN
+11.4 KB
packages/astro/test/fixtures/core-image-remark-imgattr/src/assets/penguin2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/core-image-remark-imgattr/src/pages/index.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 @@ | ||
![alt](../assets/penguin2.jpg) |
6 changes: 6 additions & 0 deletions
6
packages/astro/test/fixtures/core-image-remark-imgattr/tsconfig.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,6 @@ | ||
{ | ||
"extends": "astro/tsconfigs/base", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.