mdx-math.test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import mdx from '@astrojs/mdx';
  2. import { describe, it } from 'node:test';
  3. import * as assert from 'node:assert/strict';
  4. import { parseHTML } from 'linkedom';
  5. import { loadFixture } from '../../../astro/test/test-utils.js';
  6. import remarkMath from 'remark-math';
  7. import rehypeMathjaxSvg from 'rehype-mathjax';
  8. import rehypeMathjaxChtml from 'rehype-mathjax/chtml';
  9. const FIXTURE_ROOT = new URL('./fixtures/mdx-math/', import.meta.url);
  10. describe('MDX math', () => {
  11. describe('mathjax', () => {
  12. it('works with svg', async () => {
  13. const fixture = await loadFixture({
  14. root: FIXTURE_ROOT,
  15. markdown: {
  16. remarkPlugins: [remarkMath],
  17. rehypePlugins: [rehypeMathjaxSvg],
  18. },
  19. integrations: [mdx()],
  20. });
  21. await fixture.build();
  22. const html = await fixture.readFile('/mathjax/index.html');
  23. const { document } = parseHTML(html);
  24. const mjxContainer = document.querySelector('mjx-container[jax="SVG"]');
  25. assert.notEqual(mjxContainer, null);
  26. const mjxStyle = document.querySelector('style').innerHTML;
  27. assert.equal(
  28. mjxStyle.includes('mjx-container[jax="SVG"]'),
  29. true,
  30. 'style should not be html-escaped'
  31. );
  32. });
  33. it('works with chtml', async () => {
  34. const fixture = await loadFixture({
  35. root: FIXTURE_ROOT,
  36. markdown: {
  37. remarkPlugins: [remarkMath],
  38. rehypePlugins: [
  39. [
  40. rehypeMathjaxChtml,
  41. {
  42. chtml: {
  43. fontURL: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2',
  44. },
  45. },
  46. ],
  47. ],
  48. },
  49. integrations: [mdx()],
  50. });
  51. await fixture.build();
  52. const html = await fixture.readFile('/mathjax/index.html');
  53. const { document } = parseHTML(html);
  54. const mjxContainer = document.querySelector('mjx-container[jax="CHTML"]');
  55. assert.notEqual(mjxContainer, null);
  56. const mjxStyle = document.querySelector('style').innerHTML;
  57. assert.equal(
  58. mjxStyle.includes('mjx-container[jax="CHTML"]'),
  59. true,
  60. 'style should not be html-escaped'
  61. );
  62. });
  63. });
  64. });