The values of the alt and title attributes of img tags will be added as figcaption elements.
npm install rehype-caption-for-img
import { rehype } from 'rehype';
import rehypeCaptionForImg from 'rehype-caption-for-img';
rehype()
.use(rehypeCaptionForImg)
import { rehype } from 'rehype';
import rehypeCaptionForImg from 'rehype-caption-for-img';
const html = `
<h1>header</h1>
<img src="https://example.com/image.jpg" title="awesome title" alt="alt">
`
const output = rehype()
.data('settings', { fragment: true })
.use(rehypeCaptionForImg)
.processSync(html)
.toString();
console.log(output);
output:
<h1>header</h1>
<figure>
<img src="https://example.com/image.jpg" title="awesome title" alt="alt">
<figcaption>awesome title</figcaption>
</figure>
If true, the title attribute of the img tag will be removed.
@tenmihi