0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-31 23:31:30 -05:00

[ci] format

This commit is contained in:
Oliver Speir 2024-01-17 13:14:53 +00:00 committed by astrobot-houston
parent 165cfc154b
commit bc2edd4339
3 changed files with 52 additions and 44 deletions

View file

@ -53,7 +53,9 @@ describe('astro:image', () => {
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');
expect(new URL($img.attr('src'), 'http://example.com').searchParams.get('w')).to.equal(
'50'
);
});
});
});

View file

@ -3,28 +3,28 @@ import type { MarkdownVFile } from './types.js';
export function rehypeImages() {
return () =>
function (tree: any, file: MarkdownVFile) {
const imageOccurrenceMap = new Map();
function (tree: any, file: MarkdownVFile) {
const imageOccurrenceMap = new Map();
visit(tree, (node) => {
if (node.type !== 'element') return;
if (node.tagName !== 'img') return;
visit(tree, (node) => {
if (node.type !== 'element') return;
if (node.tagName !== 'img') return;
if (node.properties?.src) {
if (file.data.imagePaths?.has(node.properties.src)) {
const { ...props } = node.properties;
if (node.properties?.src) {
if (file.data.imagePaths?.has(node.properties.src)) {
const { ...props } = node.properties;
// Initialize or increment occurrence count for this image
const index = imageOccurrenceMap.get(node.properties.src) || 0;
imageOccurrenceMap.set(node.properties.src, index + 1);
// Initialize or increment occurrence count for this image
const index = imageOccurrenceMap.get(node.properties.src) || 0;
imageOccurrenceMap.set(node.properties.src, index + 1);
node.properties['__ASTRO_IMAGE_'] = JSON.stringify({ ...props, index });
Object.keys(props).forEach((prop) => {
delete node.properties[prop];
});
}
}
});
};
node.properties['__ASTRO_IMAGE_'] = JSON.stringify({ ...props, index });
Object.keys(props).forEach((prop) => {
delete node.properties[prop];
});
}
}
});
};
}

View file

@ -2,32 +2,38 @@ import { createMarkdownProcessor } from '../dist/index.js';
import chai from 'chai';
describe('collect images', async () => {
const processor = await createMarkdownProcessor();
const processor = await createMarkdownProcessor();
it('should collect inline image paths', async () => {
const {
code,
metadata: { imagePaths },
} = await processor.render(`Hello ![inline image url](./img.png)`, {
fileURL: 'file.md',
});
it('should collect inline image paths', async () => {
const {
code,
metadata: { imagePaths },
} = await processor.render(`Hello ![inline image url](./img.png)`, {
fileURL: 'file.md',
});
chai
.expect(code)
.to.equal('<p>Hello <img __ASTRO_IMAGE_="{&#x22;src&#x22;:&#x22;./img.png&#x22;,&#x22;alt&#x22;:&#x22;inline image url&#x22;,&#x22;index&#x22;:0}"></p>');
chai
.expect(code)
.to.equal(
'<p>Hello <img __ASTRO_IMAGE_="{&#x22;src&#x22;:&#x22;./img.png&#x22;,&#x22;alt&#x22;:&#x22;inline image url&#x22;,&#x22;index&#x22;:0}"></p>'
);
chai.expect(Array.from(imagePaths)).to.deep.equal(['./img.png']);
});
chai.expect(Array.from(imagePaths)).to.deep.equal(['./img.png']);
});
it('should add image paths from definition', async () => {
const {
code,
metadata: { imagePaths },
} = await processor.render(`Hello ![image ref][img-ref]\n\n[img-ref]: ./img.webp`, {
fileURL: 'file.md',
});
it('should add image paths from definition', async () => {
const {
code,
metadata: { imagePaths },
} = await processor.render(`Hello ![image ref][img-ref]\n\n[img-ref]: ./img.webp`, {
fileURL: 'file.md',
});
chai.expect(code).to.equal('<p>Hello <img __ASTRO_IMAGE_="{&#x22;src&#x22;:&#x22;./img.webp&#x22;,&#x22;alt&#x22;:&#x22;image ref&#x22;,&#x22;index&#x22;:0}"></p>');
chai.expect(Array.from(imagePaths)).to.deep.equal(['./img.webp']);
});
chai
.expect(code)
.to.equal(
'<p>Hello <img __ASTRO_IMAGE_="{&#x22;src&#x22;:&#x22;./img.webp&#x22;,&#x22;alt&#x22;:&#x22;image ref&#x22;,&#x22;index&#x22;:0}"></p>'
);
chai.expect(Array.from(imagePaths)).to.deep.equal(['./img.webp']);
});
});