latex.spec.ts 3.1 KB

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