latex.spec.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { removeMath, replaceMath } from '../src';
  5. describe('jupyter-ui', () => {
  6. describe('removeMath()', () => {
  7. it('should split the text into text and math', () => {
  8. const input = 'hello, $ /alpha $, there';
  9. const { text, math } = removeMath(input);
  10. expect(text).toBe('hello, @@0@@, there');
  11. expect(math).toEqual(['$ /alpha $']);
  12. });
  13. it('should handle code spans', () => {
  14. const input = '`$foo` and `$bar` are variables';
  15. const { text, math } = removeMath(input);
  16. expect(text).toBe(input);
  17. expect(math).toEqual([]);
  18. });
  19. it('should handle math markers', () => {
  20. const input = ' @@0@@ hello, $ /alpha $, there';
  21. const { text, math } = removeMath(input);
  22. expect(text).toBe(' @@0@@ hello, @@1@@, there');
  23. expect(math).toEqual(['@@0@@', '$ /alpha $']);
  24. });
  25. it('should handle unbalanced braces', () => {
  26. const input = 'hello, $ /alpha { $, there';
  27. const { text, math } = removeMath(input);
  28. expect(text).toBe('hello, @@0@@, there');
  29. expect(math).toEqual(['$ /alpha { $']);
  30. });
  31. it('should handle balanced braces', () => {
  32. const input = 'hello, $ /alpha { } $, there';
  33. const { text, math } = removeMath(input);
  34. expect(text).toBe('hello, @@0@@, there');
  35. expect(math).toEqual(['$ /alpha { } $']);
  36. });
  37. it('should handle math blocks', () => {
  38. const input = 'hello, $$\nfoo\n$$, there';
  39. const { text, math } = removeMath(input);
  40. expect(text).toBe('hello, @@0@@, there');
  41. expect(math).toEqual(['$$\nfoo\n$$']);
  42. });
  43. it('should handle begin statements', () => {
  44. const input = 'hello, \\begin{align} \\end{align}, there';
  45. const { text, math } = removeMath(input);
  46. expect(text).toBe('hello, @@0@@, there');
  47. expect(math).toEqual(['\\begin{align} \\end{align}']);
  48. });
  49. it('should handle `\\(` delimiters in GFM', () => {
  50. const input = `
  51. \`\`\`
  52. Some \\(text
  53. \'\'\'
  54. **bold**
  55. ## header
  56. `;
  57. const { text, math } = removeMath(input);
  58. expect(text).toBe(input);
  59. expect(math).toEqual([]);
  60. });
  61. it('should handle `\\\\(` delimiters for math', () => {
  62. const input = `hello, \\\\\(
  63. /alpha
  64. \\\\\), there`;
  65. const { text, math } = removeMath(input);
  66. expect(text).toBe('hello, @@0@@, there');
  67. expect(math).toEqual(['\\\\(\n /alpha\n \\\\)']);
  68. });
  69. it('should handle `\\\\[` delimiters for math', () => {
  70. const input = `hello, \\\\\[
  71. /alpha
  72. \\\\\], there`;
  73. const { text, math } = removeMath(input);
  74. expect(text).toBe('hello, @@0@@, there');
  75. expect(math).toEqual(['\\\\[\n /alpha\n \\\\]']);
  76. });
  77. });
  78. describe('replaceMath()', () => {
  79. it('should recombine text split with removeMath', () => {
  80. const input = 'hello, $ /alpha $, there';
  81. const { text, math } = removeMath(input);
  82. expect(replaceMath(text, math)).toBe(input);
  83. });
  84. });
  85. });