0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/mdx/test/mdx-plugins.test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

316 lines
7.5 KiB
JavaScript
Raw Normal View History

import mdx from '@astrojs/mdx';
2024-01-31 03:37:34 -05:00
import * as assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { visit as estreeVisit } from 'estree-util-visit';
import { parseHTML } from 'linkedom';
import remarkToc from 'remark-toc';
import { loadFixture } from '../../../astro/test/test-utils.js';
const FIXTURE_ROOT = new URL('./fixtures/mdx-plugins/', import.meta.url);
const FILE = '/with-plugins/index.html';
describe('MDX plugins', () => {
it('supports custom remark plugins - TOC', async () => {
const fixture = await buildFixture({
integrations: [
mdx({
remarkPlugins: [remarkToc],
}),
],
});
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
2024-01-31 03:37:34 -05:00
assert.notEqual(selectTocLink(document), null);
});
it('Applies GFM by default', async () => {
const fixture = await buildFixture({
integrations: [mdx()],
});
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
2024-01-31 03:37:34 -05:00
assert.notEqual(selectGfmLink(document), null);
});
it('Applies SmartyPants by default', async () => {
const fixture = await buildFixture({
integrations: [mdx()],
});
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
const quote = selectSmartypantsQuote(document);
2024-01-31 03:37:34 -05:00
assert.notEqual(quote, null);
assert.equal(quote.textContent.includes('“Smartypants” is — awesome'), true);
});
it('supports custom rehype plugins', async () => {
const fixture = await buildFixture({
integrations: [
mdx({
rehypePlugins: [rehypeExamplePlugin],
}),
],
});
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
2024-01-31 03:37:34 -05:00
assert.notEqual(selectRehypeExample(document), null);
});
it('supports custom rehype plugins from integrations', async () => {
const fixture = await buildFixture({
integrations: [
mdx(),
{
name: 'test',
hooks: {
'astro:config:setup': ({ updateConfig }) => {
updateConfig({
markdown: {
rehypePlugins: [rehypeExamplePlugin],
},
});
},
},
},
],
});
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
assert.notEqual(selectRehypeExample(document), null);
});
it('supports custom rehype plugins with namespaced attributes', async () => {
const fixture = await buildFixture({
integrations: [
mdx({
rehypePlugins: [rehypeSvgPlugin],
}),
],
});
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
2024-01-31 03:37:34 -05:00
assert.notEqual(selectRehypeSvg(document), null);
});
it('extends markdown config by default', async () => {
const fixture = await buildFixture({
markdown: {
remarkPlugins: [remarkExamplePlugin],
rehypePlugins: [rehypeExamplePlugin],
},
2022-08-30 12:41:06 -05:00
integrations: [mdx()],
});
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
2024-01-31 03:37:34 -05:00
assert.notEqual(selectRemarkExample(document), null);
assert.notEqual(selectRehypeExample(document), null);
});
it('ignores string-based plugins in markdown config', async () => {
const fixture = await buildFixture({
markdown: {
remarkPlugins: [['remark-toc', {}]],
},
2022-08-30 12:41:06 -05:00
integrations: [mdx()],
});
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
2024-01-31 03:37:34 -05:00
assert.equal(selectTocLink(document), null);
});
for (const extendMarkdownConfig of [true, false]) {
describe(`extendMarkdownConfig = ${extendMarkdownConfig}`, () => {
let fixture;
before(async () => {
fixture = await buildFixture({
markdown: {
remarkPlugins: [remarkToc],
gfm: false,
smartypants: false,
},
integrations: [
mdx({
extendMarkdownConfig,
remarkPlugins: [remarkExamplePlugin],
rehypePlugins: [rehypeExamplePlugin],
}),
],
});
});
it('Handles MDX plugins', async () => {
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
2024-01-31 03:37:34 -05:00
assert.notEqual(selectRemarkExample(document, 'MDX remark plugins not applied.'), null);
assert.notEqual(selectRehypeExample(document, 'MDX rehype plugins not applied.'), null);
});
it('Handles Markdown plugins', async () => {
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
2024-01-31 03:37:34 -05:00
assert.equal(
selectTocLink(
document,
'`remarkToc` plugin applied unexpectedly. Should override Markdown config.'
2024-01-31 03:37:34 -05:00
),
null
);
});
it('Handles gfm', async () => {
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
if (extendMarkdownConfig === true) {
2024-01-31 03:37:34 -05:00
assert.equal(selectGfmLink(document), null, 'Does not respect `markdown.gfm` option.');
} else {
2024-01-31 03:37:34 -05:00
assert.notEqual(selectGfmLink(document), null, 'Respects `markdown.gfm` unexpectedly.');
}
});
it('Handles smartypants', async () => {
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
const quote = selectSmartypantsQuote(document);
if (extendMarkdownConfig === true) {
2024-01-31 03:37:34 -05:00
assert.equal(
quote.textContent.includes('"Smartypants" is -- awesome'),
true,
'Does not respect `markdown.smartypants` option.'
);
} else {
2024-01-31 03:37:34 -05:00
assert.equal(
quote.textContent.includes('“Smartypants” is — awesome'),
2024-01-31 03:37:34 -05:00
true,
'Respects `markdown.smartypants` unexpectedly.'
);
}
});
});
}
it('supports custom recma plugins', async () => {
const fixture = await buildFixture({
integrations: [
mdx({
recmaPlugins: [recmaExamplePlugin],
}),
],
});
const html = await fixture.readFile(FILE);
const { document } = parseHTML(html);
2024-01-31 03:37:34 -05:00
assert.notEqual(selectRecmaExample(document), null);
});
});
async function buildFixture(config) {
const fixture = await loadFixture({
root: FIXTURE_ROOT,
...config,
});
await fixture.build();
return fixture;
}
function remarkExamplePlugin() {
return (tree) => {
tree.children.push({
type: 'html',
value: '<div data-remark-plugin-works="true"></div>',
});
};
}
function rehypeExamplePlugin() {
return (tree) => {
tree.children.push({
type: 'element',
tagName: 'div',
properties: { 'data-rehype-plugin-works': 'true' },
});
};
}
function rehypeSvgPlugin() {
return (tree) => {
tree.children.push({
type: 'element',
tagName: 'svg',
2023-02-15 04:08:55 -05:00
properties: { xmlns: 'http://www.w3.org/2000/svg' },
children: [
{
type: 'element',
tagName: 'use',
2023-02-15 04:08:55 -05:00
properties: { xLinkHref: '#icon' },
},
],
});
};
}
function recmaExamplePlugin() {
return (tree) => {
estreeVisit(tree, (node) => {
if (
node.type === 'VariableDeclarator' &&
node.id.name === 'recmaPluginWorking' &&
node.init?.type === 'Literal'
) {
node.init = {
...(node.init ?? {}),
value: true,
raw: 'true',
};
}
});
};
}
function selectTocLink(document) {
return document.querySelector('ul a[href="#section-1"]');
}
function selectGfmLink(document) {
return document.querySelector('a[href="https://handle-me-gfm.com"]');
}
function selectSmartypantsQuote(document) {
return document.querySelector('blockquote');
}
function selectRemarkExample(document) {
return document.querySelector('div[data-remark-plugin-works]');
}
function selectRehypeExample(document) {
return document.querySelector('div[data-rehype-plugin-works]');
}
function selectRehypeSvg(document) {
return document.querySelector('svg > use[xlink\\:href]');
}
function selectRecmaExample(document) {
return document.querySelector('div[data-recma-plugin-works]');
}