Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

markdown.test.ts 2.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  1. import { sanitizeMarkdown } from '../../site/src/utils/markdown';
  2. describe('sanitizeMarkdown', () => {
  3. const sampleMarkdown = `---
  4. title: Test Document
  5. author: Test Author
  6. ---
  7. # Main Title
  8. This is a paragraph with ![an image](image.jpg) and some text.
  9. <!-- This is a comment -->
  10. <div style="color: red;">Styled content</div>
  11. \`\`\`javascript
  12. console.log('code block');
  13. \`\`\`
  14. Here's some \`inline code\` and a [link](https://example.com).
  15. <img src="test.jpg" alt="HTML image" />
  16. Multiple newlines above.
  17. `;
  18. it('should sanitize markdown with sane defaults', () => {
  19. const result = sanitizeMarkdown(sampleMarkdown);
  20. expect(result).not.toContain('---');
  21. expect(result).not.toContain('title: Test Document');
  22. expect(result).not.toContain('author: Test Author');
  23. expect(result).not.toContain('![an image](image.jpg)');
  24. expect(result).not.toContain('<img src="test.jpg"');
  25. expect(result).not.toContain('<!-- This is a comment -->');
  26. expect(result).not.toContain('<div style="color: red;">');
  27. expect(result).toContain('Styled content');
  28. expect(result).toContain('console.log');
  29. expect(result).toContain('```javascript');
  30. expect(result).toContain('`inline code`');
  31. expect(result).toContain('[link](https://example.com)');
  32. expect(result).not.toMatch(/\n\n\n/);
  33. expect(result).toMatch(/^# Main Title/);
  34. });
  35. it('should handle empty string', () => {
  36. expect(sanitizeMarkdown('')).toBe('');
  37. });
  38. it('should handle null/undefined input', () => {
  39. expect(sanitizeMarkdown(null as any)).toBe('');
  40. expect(sanitizeMarkdown(undefined as any)).toBe('');
  41. });
  42. it('should handle non-string input', () => {
  43. expect(sanitizeMarkdown(123 as any)).toBe('');
  44. });
  45. it('should handle TOML frontmatter', () => {
  46. const tomlMarkdown = `+++
  47. title = "Test"
  48. +++
  49. # Content`;
  50. const result = sanitizeMarkdown(tomlMarkdown);
  51. expect(result).not.toContain('+++');
  52. expect(result).not.toContain('title = "Test"');
  53. expect(result).toContain('# Content');
  54. });
  55. it('should preserve markdown formatting', () => {
  56. const formattedMarkdown = `# Heading
  57. **Bold text** and *italic text*
  58. - List item 1
  59. - List item 2
  60. > Blockquote`;
  61. const result = sanitizeMarkdown(formattedMarkdown);
  62. expect(result).toContain('# Heading');
  63. expect(result).toContain('**Bold text**');
  64. expect(result).toContain('*italic text*');
  65. expect(result).toContain('- List item 1');
  66. expect(result).toContain('> Blockquote');
  67. });
  68. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...