77cede720b
* fix: make rehypeCollectHeadings a required plugin * docs: update README on rehypePlugins * test: remove collect-headings override test * docs: remove extends from rehype docs * chore: changeset
70 lines
1.8 KiB
JavaScript
70 lines
1.8 KiB
JavaScript
import mdx from '@astrojs/mdx';
|
|
|
|
import getReadingTime from 'reading-time';
|
|
import { toString } from 'mdast-util-to-string';
|
|
import { expect } from 'chai';
|
|
import { parseHTML } from 'linkedom';
|
|
import { jsToTreeNode } from '../dist/utils.js';
|
|
|
|
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
|
|
function rehypeReadingTime() {
|
|
return function (tree, { data }) {
|
|
const readingTime = getReadingTime(toString(tree));
|
|
tree.children.unshift(
|
|
jsToTreeNode(`export const readingTime = ${JSON.stringify(readingTime)}`)
|
|
);
|
|
};
|
|
}
|
|
|
|
const FIXTURE_ROOT = new URL('./fixtures/mdx-rehype-plugins/', import.meta.url);
|
|
|
|
describe('MDX rehype plugins', () => {
|
|
describe('without "extends"', () => {
|
|
let fixture;
|
|
before(async () => {
|
|
fixture = await loadFixture({
|
|
root: FIXTURE_ROOT,
|
|
integrations: [
|
|
mdx({
|
|
rehypePlugins: [rehypeReadingTime],
|
|
}),
|
|
],
|
|
});
|
|
await fixture.build();
|
|
});
|
|
|
|
it('supports custom rehype plugins - reading time', async () => {
|
|
const { readingTime } = JSON.parse(await fixture.readFile('/reading-time.json'));
|
|
|
|
expect(readingTime).to.not.be.null;
|
|
expect(readingTime.text).to.match(/^\d+ min read/);
|
|
});
|
|
});
|
|
|
|
describe('with "extends"', () => {
|
|
let fixture;
|
|
before(async () => {
|
|
fixture = await loadFixture({
|
|
root: FIXTURE_ROOT,
|
|
integrations: [
|
|
mdx({
|
|
rehypePlugins: { extends: [rehypeReadingTime] },
|
|
}),
|
|
],
|
|
});
|
|
await fixture.build();
|
|
});
|
|
|
|
it('preserves default getHeadings', async () => {
|
|
const html = await fixture.readFile('/space-ipsum/index.html');
|
|
const { document } = parseHTML(html);
|
|
|
|
const headings = [...document.querySelectorAll('h1, h2')];
|
|
expect(headings.length).to.be.greaterThan(0);
|
|
for (const heading of headings) {
|
|
expect(heading.id).to.not.be.empty;
|
|
}
|
|
});
|
|
});
|
|
});
|