latex.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 fenced code blocks', () => {
  19. const input = '```\n$foo\n$bar\n```';
  20. const { text, math } = removeMath(input);
  21. expect(text).toBe(input);
  22. expect(math).toEqual([]);
  23. });
  24. it('should handle tilde fenced code blocks', () => {
  25. const input = '~~~\n$foo\n$bar\n~~~';
  26. const { text, math } = removeMath(input);
  27. expect(text).toBe(input);
  28. expect(math).toEqual([]);
  29. });
  30. it('should handle long fenced code blocks', () => {
  31. const input = '````\n$foo\n$bar\n```\n``````';
  32. const { text, math } = removeMath(input);
  33. expect(text).toBe(input);
  34. expect(math).toEqual([]);
  35. });
  36. it('should handle fenced code blocks with info string', () => {
  37. const input = '```R\ndata[data$foo > 1 & data$bar < 2,]\n```';
  38. const { text, math } = removeMath(input);
  39. expect(text).toBe(input);
  40. expect(math).toEqual([]);
  41. });
  42. it('should handle math markers', () => {
  43. const input = ' @@0@@ hello, $ /alpha $, there';
  44. const { text, math } = removeMath(input);
  45. expect(text).toBe(' @@0@@ hello, @@1@@, there');
  46. expect(math).toEqual(['@@0@@', '$ /alpha $']);
  47. });
  48. it('should handle unbalanced braces', () => {
  49. const input = 'hello, $ /alpha { $, there';
  50. const { text, math } = removeMath(input);
  51. expect(text).toBe('hello, @@0@@, there');
  52. expect(math).toEqual(['$ /alpha { $']);
  53. });
  54. it('should handle balanced braces', () => {
  55. const input = 'hello, $ /alpha { } $, there';
  56. const { text, math } = removeMath(input);
  57. expect(text).toBe('hello, @@0@@, there');
  58. expect(math).toEqual(['$ /alpha { } $']);
  59. });
  60. it('should handle math blocks', () => {
  61. const input = 'hello, $$\nfoo\n$$, there';
  62. const { text, math } = removeMath(input);
  63. expect(text).toBe('hello, @@0@@, there');
  64. expect(math).toEqual(['$$\nfoo\n$$']);
  65. });
  66. it('should handle begin statements', () => {
  67. const input = 'hello, \\begin{align} \\end{align}, there';
  68. const { text, math } = removeMath(input);
  69. expect(text).toBe('hello, @@0@@, there');
  70. expect(math).toEqual(['\\begin{align} \\end{align}']);
  71. });
  72. it('should handle `\\(` delimiters in GFM', () => {
  73. const input = `
  74. \`\`\`
  75. Some \\(text
  76. \'\'\'
  77. **bold**
  78. ## header
  79. `;
  80. const { text, math } = removeMath(input);
  81. expect(text).toBe(input);
  82. expect(math).toEqual([]);
  83. });
  84. it('should handle `\\\\(` delimiters for math', () => {
  85. const input = `hello, \\\\\(
  86. /alpha
  87. \\\\\), there`;
  88. const { text, math } = removeMath(input);
  89. expect(text).toBe('hello, @@0@@, there');
  90. expect(math).toEqual(['\\\\(\n /alpha\n \\\\)']);
  91. });
  92. it('should handle `\\\\[` delimiters for math', () => {
  93. const input = `hello, \\\\\[
  94. /alpha
  95. \\\\\], there`;
  96. const { text, math } = removeMath(input);
  97. expect(text).toBe('hello, @@0@@, there');
  98. expect(math).toEqual(['\\\\[\n /alpha\n \\\\]']);
  99. });
  100. });
  101. describe('replaceMath()', () => {
  102. it('should recombine text split with removeMath', () => {
  103. const input = 'hello, $ /alpha $, there';
  104. const { text, math } = removeMath(input);
  105. expect(replaceMath(text, math)).toBe(input);
  106. });
  107. });
  108. });