2022-08-30 12:38:35 -05:00
|
|
|
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';
|
2022-10-24 13:27:37 -05:00
|
|
|
import { visit as estreeVisit } from 'estree-util-visit';
|
2022-08-30 12:38:35 -05:00
|
|
|
import { parseHTML } from 'linkedom';
|
|
|
|
import remarkToc from 'remark-toc';
|
2022-10-24 13:27:37 -05:00
|
|
|
import { loadFixture } from '../../../astro/test/test-utils.js';
|
2022-08-30 12:38:35 -05:00
|
|
|
|
|
|
|
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);
|
2022-08-30 12:38:35 -05:00
|
|
|
});
|
|
|
|
|
2022-09-01 11:11:13 -05:00
|
|
|
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);
|
2022-09-01 11:11:13 -05:00
|
|
|
});
|
|
|
|
|
2023-01-06 09:26:02 -05:00
|
|
|
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);
|
2024-07-17 01:48:07 -05:00
|
|
|
assert.equal(quote.textContent.includes('“Smartypants” is — awesome'), true);
|
2023-01-06 09:26:02 -05:00
|
|
|
});
|
|
|
|
|
2022-08-30 12:38:35 -05:00
|
|
|
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);
|
2022-08-30 12:38:35 -05:00
|
|
|
});
|
|
|
|
|
2024-05-08 04:25:27 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2023-02-16 05:53:30 -05:00
|
|
|
it('supports custom rehype plugins with namespaced attributes', async () => {
|
2023-02-15 04:06:33 -05:00
|
|
|
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);
|
2023-02-15 04:06:33 -05:00
|
|
|
});
|
|
|
|
|
2022-08-30 12:38:35 -05:00
|
|
|
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()],
|
2022-08-30 12:38:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2022-08-30 12:38:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('ignores string-based plugins in markdown config', async () => {
|
|
|
|
const fixture = await buildFixture({
|
|
|
|
markdown: {
|
2023-08-01 04:11:26 -05:00
|
|
|
remarkPlugins: [['remark-toc', {}]],
|
2022-08-30 12:38:35 -05:00
|
|
|
},
|
2022-08-30 12:41:06 -05:00
|
|
|
integrations: [mdx()],
|
2022-08-30 12:38:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const html = await fixture.readFile(FILE);
|
|
|
|
const { document } = parseHTML(html);
|
|
|
|
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(selectTocLink(document), null);
|
2022-08-30 12:38:35 -05:00
|
|
|
});
|
|
|
|
|
2023-01-03 17:12:47 -05:00
|
|
|
for (const extendMarkdownConfig of [true, false]) {
|
|
|
|
describe(`extendMarkdownConfig = ${extendMarkdownConfig}`, () => {
|
|
|
|
let fixture;
|
|
|
|
before(async () => {
|
|
|
|
fixture = await buildFixture({
|
|
|
|
markdown: {
|
|
|
|
remarkPlugins: [remarkToc],
|
|
|
|
gfm: false,
|
2023-01-06 09:26:02 -05:00
|
|
|
smartypants: false,
|
2023-01-03 17:12:47 -05:00
|
|
|
},
|
|
|
|
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);
|
2023-01-03 17:12:47 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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(
|
2023-01-03 17:12:47 -05:00
|
|
|
selectTocLink(
|
|
|
|
document,
|
|
|
|
'`remarkToc` plugin applied unexpectedly. Should override Markdown config.'
|
2024-01-31 03:37:34 -05:00
|
|
|
),
|
|
|
|
null
|
|
|
|
);
|
2023-01-03 17:12:47 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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.');
|
2023-01-03 17:12:47 -05:00
|
|
|
} else {
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.notEqual(selectGfmLink(document), null, 'Respects `markdown.gfm` unexpectedly.');
|
2023-01-03 17:12:47 -05:00
|
|
|
}
|
|
|
|
});
|
2023-01-06 09:26:02 -05:00
|
|
|
|
|
|
|
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.'
|
2023-01-06 09:26:02 -05:00
|
|
|
);
|
|
|
|
} else {
|
2024-01-31 03:37:34 -05:00
|
|
|
assert.equal(
|
2024-07-17 01:48:07 -05:00
|
|
|
quote.textContent.includes('“Smartypants” is — awesome'),
|
2024-01-31 03:37:34 -05:00
|
|
|
true,
|
|
|
|
'Respects `markdown.smartypants` unexpectedly.'
|
2023-01-06 09:26:02 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2022-08-30 12:38:35 -05:00
|
|
|
});
|
2023-01-03 17:12:47 -05:00
|
|
|
}
|
2022-10-24 13:27:37 -05:00
|
|
|
|
|
|
|
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);
|
2022-10-24 13:27:37 -05:00
|
|
|
});
|
2022-08-30 12:38:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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' },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-15 04:06:33 -05:00
|
|
|
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' },
|
2023-02-15 04:06:33 -05:00
|
|
|
children: [
|
|
|
|
{
|
|
|
|
type: 'element',
|
|
|
|
tagName: 'use',
|
2023-02-15 04:08:55 -05:00
|
|
|
properties: { xLinkHref: '#icon' },
|
|
|
|
},
|
|
|
|
],
|
2023-02-15 04:06:33 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-24 13:27:37 -05:00
|
|
|
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',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-30 12:38:35 -05:00
|
|
|
function selectTocLink(document) {
|
|
|
|
return document.querySelector('ul a[href="#section-1"]');
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectGfmLink(document) {
|
|
|
|
return document.querySelector('a[href="https://handle-me-gfm.com"]');
|
|
|
|
}
|
|
|
|
|
2023-01-06 09:26:02 -05:00
|
|
|
function selectSmartypantsQuote(document) {
|
|
|
|
return document.querySelector('blockquote');
|
|
|
|
}
|
|
|
|
|
2022-08-30 12:38:35 -05:00
|
|
|
function selectRemarkExample(document) {
|
|
|
|
return document.querySelector('div[data-remark-plugin-works]');
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectRehypeExample(document) {
|
|
|
|
return document.querySelector('div[data-rehype-plugin-works]');
|
|
|
|
}
|
2022-10-24 13:27:37 -05:00
|
|
|
|
2023-02-15 04:06:33 -05:00
|
|
|
function selectRehypeSvg(document) {
|
|
|
|
return document.querySelector('svg > use[xlink\\:href]');
|
|
|
|
}
|
|
|
|
|
2022-10-24 13:27:37 -05:00
|
|
|
function selectRecmaExample(document) {
|
|
|
|
return document.querySelector('div[data-recma-plugin-works]');
|
|
|
|
}
|