@chayns-components/format 5.0.0-beta.650 → 5.0.0-beta.674

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/lib/cjs/types/format.js +2 -0
  2. package/lib/cjs/types/format.js.map +1 -0
  3. package/lib/cjs/utils/escape.js +1 -3
  4. package/lib/cjs/utils/escape.js.map +1 -1
  5. package/lib/cjs/utils/formatString/bb-code/findBBCode.js +6 -4
  6. package/lib/cjs/utils/formatString/bb-code/findBBCode.js.map +1 -1
  7. package/lib/cjs/utils/formatString/bb-code/formatBBCode.js +37 -37
  8. package/lib/cjs/utils/formatString/bb-code/formatBBCode.js.map +1 -1
  9. package/lib/cjs/utils/formatString/formatString.js +30 -42
  10. package/lib/cjs/utils/formatString/formatString.js.map +1 -1
  11. package/lib/cjs/utils/formatString/formatString.test.js +549 -0
  12. package/lib/cjs/utils/formatString/formatString.test.js.map +1 -0
  13. package/lib/cjs/utils/formatString/markdown/formatMarkdown.js +145 -29
  14. package/lib/cjs/utils/formatString/markdown/formatMarkdown.js.map +1 -1
  15. package/lib/esm/types/format.js +2 -0
  16. package/lib/esm/types/format.js.map +1 -0
  17. package/lib/esm/utils/escape.js +0 -1
  18. package/lib/esm/utils/escape.js.map +1 -1
  19. package/lib/esm/utils/formatString/bb-code/findBBCode.js +6 -4
  20. package/lib/esm/utils/formatString/bb-code/findBBCode.js.map +1 -1
  21. package/lib/esm/utils/formatString/bb-code/formatBBCode.js +37 -39
  22. package/lib/esm/utils/formatString/bb-code/formatBBCode.js.map +1 -1
  23. package/lib/esm/utils/formatString/formatString.js +33 -45
  24. package/lib/esm/utils/formatString/formatString.js.map +1 -1
  25. package/lib/esm/utils/formatString/formatString.test.js +547 -0
  26. package/lib/esm/utils/formatString/formatString.test.js.map +1 -0
  27. package/lib/esm/utils/formatString/markdown/formatMarkdown.js +141 -26
  28. package/lib/esm/utils/formatString/markdown/formatMarkdown.js.map +1 -1
  29. package/lib/types/types/format.d.ts +5 -0
  30. package/lib/types/utils/escape.d.ts +0 -1
  31. package/lib/types/utils/formatString/bb-code/findBBCode.d.ts +2 -0
  32. package/lib/types/utils/formatString/bb-code/formatBBCode.d.ts +3 -4
  33. package/lib/types/utils/formatString/formatString.d.ts +1 -3
  34. package/lib/types/utils/formatString/formatString.test.d.ts +1 -0
  35. package/lib/types/utils/formatString/markdown/formatMarkdown.d.ts +3 -1
  36. package/package.json +6 -4
  37. package/lib/cjs/utils/formatString/markdown/formatMarkdownTable.js +0 -86
  38. package/lib/cjs/utils/formatString/markdown/formatMarkdownTable.js.map +0 -1
  39. package/lib/esm/utils/formatString/markdown/formatMarkdownTable.js +0 -78
  40. package/lib/esm/utils/formatString/markdown/formatMarkdownTable.js.map +0 -1
  41. package/lib/types/utils/formatString/markdown/formatMarkdownTable.d.ts +0 -9
@@ -0,0 +1,547 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { formatStringToHtml } from './formatString';
3
+ const removeLinebreaks = text => text.replace(/\n/g, '');
4
+ describe('HTML Formatter Function', () => {
5
+ describe('Format Plain Text', () => {
6
+ describe('Line breaks', () => {
7
+ test('should format line breaks correctly', () => {
8
+ const result = formatStringToHtml('Line 1\nLine 2');
9
+ expect(result.html).toEqual('<p>Line 1\nLine 2</p>');
10
+ expect(result.tables).toEqual([]);
11
+ });
12
+ test('should format multiple line breaks correctly', () => {
13
+ const result = formatStringToHtml('Line 1\n\nLine 2');
14
+ expect(result.html).toEqual('<p>Line 1</p>\n<p>Line 2</p>');
15
+ expect(result.tables).toEqual([]);
16
+ });
17
+ test('should remove trailing and leading new lines', () => {
18
+ const result = formatStringToHtml('\n\n\nLine 1\n\n\n');
19
+ expect(result.html).toEqual('<p>Line 1</p>');
20
+ expect(result.tables).toEqual([]);
21
+ });
22
+ });
23
+ describe('Whitespaces', () => {
24
+ test('should not remove repeated whitespaces', () => {
25
+ const result = formatStringToHtml('Text with spaces');
26
+ expect(result.html).toEqual('<p>Text with spaces</p>');
27
+ expect(result.tables).toEqual([]);
28
+ });
29
+ test('should not remove leading and trailing whitespaces', () => {
30
+ const result = formatStringToHtml(' Text ');
31
+ expect(result.html).toEqual('<p> Text </p>');
32
+ expect(result.tables).toEqual([]);
33
+ });
34
+ });
35
+ describe('HTML', () => {
36
+ test('should escape < and > correctly', () => {
37
+ const resultEscape = formatStringToHtml('<div>Test</div>');
38
+ expect(resultEscape.html).toEqual('<p>&lt;div&gt;Test&lt;/div&gt;</p>');
39
+ expect(resultEscape.tables).toEqual([]);
40
+ });
41
+ test('should not escape &', () => {
42
+ const resultEscape1 = formatStringToHtml('&lt;div&gt;Test&lt;/div&gt;');
43
+ expect(resultEscape1.html).toEqual('<p>&lt;div&gt;Test&lt;/div&gt;</p>');
44
+ expect(resultEscape1.tables).toEqual([]);
45
+ const resultEscape2 = formatStringToHtml('&amp;lt;div&amp;gt;Test&amp;lt;/div&amp;gt;');
46
+ expect(resultEscape2.html).toEqual('<p>&amp;lt;div&amp;gt;Test&amp;lt;/div&amp;gt;</p>');
47
+ expect(resultEscape2.tables).toEqual([]);
48
+ });
49
+
50
+ // TODO Decide if & should be escaped, when they are not part of an HTML entity.
51
+ });
52
+ describe('URLs', () => {
53
+ test('should not format URLs', () => {
54
+ const result = formatStringToHtml('https://example.com');
55
+ expect(result.html).toEqual('<p>https://example.com</p>');
56
+ expect(result.tables).toEqual([]);
57
+ });
58
+ });
59
+ });
60
+ describe('Format Markdown', () => {
61
+ describe('All Elements', () => {
62
+ test('should format text styling correctly', () => {
63
+ const boldResult = formatStringToHtml('**bold**');
64
+ expect(boldResult.html).toEqual('<p><strong>bold</strong></p>');
65
+ expect(boldResult.tables).toEqual([]);
66
+ const italicResult = formatStringToHtml('*italic*');
67
+ expect(italicResult.html).toEqual('<p><em>italic</em></p>');
68
+ expect(italicResult.tables).toEqual([]);
69
+ const inlineCodeResult = formatStringToHtml('`inline code`');
70
+ expect(inlineCodeResult.html).toEqual('<p><code>inline code</code></p>');
71
+ expect(inlineCodeResult.tables).toEqual([]);
72
+ });
73
+ test('should format headings correctly', () => {
74
+ const h1Result = formatStringToHtml('# h1');
75
+ expect(h1Result.html).toEqual('<h1>h1</h1>');
76
+ expect(h1Result.tables).toEqual([]);
77
+ const h2Result = formatStringToHtml('## h2');
78
+ expect(h2Result.html).toEqual('<h2>h2</h2>');
79
+ expect(h2Result.tables).toEqual([]);
80
+ const h3Result = formatStringToHtml('### h3');
81
+ expect(h3Result.html).toEqual('<h3>h3</h3>');
82
+ expect(h3Result.tables).toEqual([]);
83
+ const h4Result = formatStringToHtml('#### h4');
84
+ expect(h4Result.html).toEqual('<h4>h4</h4>');
85
+ expect(h4Result.tables).toEqual([]);
86
+ const h5Result = formatStringToHtml('##### h5');
87
+ expect(h5Result.html).toEqual('<h5>h5</h5>');
88
+ expect(h5Result.tables).toEqual([]);
89
+ const h6Result = formatStringToHtml('###### h6');
90
+ expect(h6Result.html).toEqual('<h6>h6</h6>');
91
+ expect(h6Result.tables).toEqual([]);
92
+ });
93
+ test('should format links correctly', () => {
94
+ const result = formatStringToHtml('[Link](https://example.com)');
95
+ expect(result.html).toEqual('<p><a href="https://example.com">Link</a></p>');
96
+ expect(result.tables).toEqual([]);
97
+ });
98
+ test('should format images correctly', () => {
99
+ const result = formatStringToHtml('![Alt Text](https://example.com/image.jpg)');
100
+ expect(result.html).toEqual('<p><img src="https://example.com/image.jpg" alt="Alt Text"></p>');
101
+ expect(result.tables).toEqual([]);
102
+ });
103
+ test('should format lists correctly', () => {
104
+ const expectedUnorderedListResult = '<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n<li>Item 3</li>\n</ul>';
105
+ const expectedOrderedListResult = '<ol>\n<li>Item 1</li>\n<li>Item 2</li>\n<li>Item 3</li>\n</ol>';
106
+ const unorderedListResult1 = formatStringToHtml('- Item 1\n- Item 2\n- Item 3');
107
+ expect(unorderedListResult1.html).toEqual(expectedUnorderedListResult);
108
+ expect(unorderedListResult1.tables).toEqual([]);
109
+ const unorderedListResult2 = formatStringToHtml('* Item 1\n* Item 2\n* Item 3');
110
+ expect(unorderedListResult2.html).toEqual(expectedUnorderedListResult);
111
+ expect(unorderedListResult2.tables).toEqual([]);
112
+ const orderedListResult = formatStringToHtml('1. Item 1\n2. Item 2\n3. Item 3');
113
+ expect(orderedListResult.html).toEqual(expectedOrderedListResult);
114
+ expect(orderedListResult.tables).toEqual([]);
115
+ const orderedListResult2 = formatStringToHtml('1) Item 1\n2) Item 2\n3) Item 3');
116
+ expect(orderedListResult2.html).toEqual(expectedOrderedListResult);
117
+ expect(orderedListResult2.tables).toEqual([]);
118
+ });
119
+ test('should format thematic breaks correctly', () => {
120
+ const expectedThematicBreakResult = '<hr>';
121
+ const result1 = formatStringToHtml('---');
122
+ expect(result1.html).toEqual(expectedThematicBreakResult);
123
+ expect(result1.tables).toEqual([]);
124
+ const result2 = formatStringToHtml('***');
125
+ expect(result2.html).toEqual(expectedThematicBreakResult);
126
+ expect(result2.tables).toEqual([]);
127
+ });
128
+ test('should format code blocks correctly', () => {
129
+ const resultWithoutLanguage = formatStringToHtml('```\nconst a = 1;\n```');
130
+ expect(resultWithoutLanguage.html).toEqual('<pre><code>const a = 1;</code></pre>');
131
+ expect(resultWithoutLanguage.tables).toEqual([]);
132
+ const resultWithLanguage = formatStringToHtml('```js\nconst a = 1;\n```');
133
+ expect(resultWithLanguage.html).toEqual('<pre><code class="language-js">const a = 1;</code></pre>');
134
+ expect(resultWithLanguage.tables).toEqual([]);
135
+ });
136
+ test('should format tables correctly', () => {
137
+ const inputString = '| Header 1 | Header 2 |\n|----------|----------|\n| Cell 1 | Cell 2 |';
138
+ const result = formatStringToHtml(inputString);
139
+ expect(removeLinebreaks(result.html)).toEqual('<table id="formatted-table-0"><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td>Cell 1</td><td>Cell 2</td></tr></tbody></table>');
140
+ expect(result.tables).toEqual([{
141
+ csv: 'Header 1,Header 2\nCell 1,Cell 2\n',
142
+ raw: inputString,
143
+ id: 'formatted-table-0'
144
+ }]);
145
+ });
146
+ });
147
+ describe('Inline Code', () => {
148
+ describe('HTML In Code', () => {
149
+ test('should escape < and > within inline code correctly', () => {
150
+ const result1 = formatStringToHtml('`<div>Test</div>`');
151
+ expect(result1.html).toEqual('<p><code>&lt;div&gt;Test&lt;/div&gt;</code></p>');
152
+ expect(result1.tables).toEqual([]);
153
+ });
154
+ test('should not escape & within inline code', () => {
155
+ const resultEscape1 = formatStringToHtml('`&lt;div&gt;Test&lt;/div&gt;`');
156
+ expect(resultEscape1.html).toEqual('<p><code>&lt;div&gt;Test&lt;/div&gt;</code></p>');
157
+ expect(resultEscape1.tables).toEqual([]);
158
+ const resultEscape2 = formatStringToHtml('`&amp;lt;div&amp;gt;Test&amp;lt;/div&amp;gt;`');
159
+ expect(resultEscape2.html).toEqual('<p><code>&amp;lt;div&amp;gt;Test&amp;lt;/div&amp;gt;</code></p>');
160
+ expect(resultEscape2.tables).toEqual([]);
161
+ });
162
+ });
163
+ test('should not format markdown within inline code', () => {
164
+ const result = formatStringToHtml('`**bold** *italic*`');
165
+ expect(result.html).toEqual('<p><code>**bold** *italic*</code></p>');
166
+ expect(result.tables).toEqual([]);
167
+ });
168
+ test('should not format bb-code within inline code', () => {
169
+ const result = formatStringToHtml('`[b]bold[/b]`', {
170
+ parseBBCode: true
171
+ });
172
+ expect(result.html).toEqual('<p><code>[b]bold[/b]</code></p>');
173
+ expect(result.tables).toEqual([]);
174
+ });
175
+ });
176
+ describe('Codeblock', () => {
177
+ test('should format code blocks with multiple lines correctly', () => {
178
+ const resultWithoutLanguage = formatStringToHtml('```\nconst a = 1;\nconst b = 2;\n```');
179
+ expect(resultWithoutLanguage.html).toEqual('<pre><code>const a = 1;\nconst b = 2;</code></pre>');
180
+ expect(resultWithoutLanguage.tables).toEqual([]);
181
+ const resultWithLanguage = formatStringToHtml('```js\nconst a = 1;\nconst b = 2;\n```');
182
+ expect(resultWithLanguage.html).toEqual('<pre><code class="language-js">const a = 1;\nconst b = 2;</code></pre>');
183
+ expect(resultWithLanguage.tables).toEqual([]);
184
+ });
185
+ describe('HTML In Code', () => {
186
+ test('should escape < and > within code block', () => {
187
+ const resultWithoutLanguage = formatStringToHtml('```\n<div>Test</div>\n```');
188
+ expect(resultWithoutLanguage.html).toEqual('<pre><code>&lt;div&gt;Test&lt;/div&gt;</code></pre>');
189
+ expect(resultWithoutLanguage.tables).toEqual([]);
190
+ const resultWithLanguage = formatStringToHtml('```html\n<div>Test</div>\n```');
191
+ expect(resultWithLanguage.html).toEqual('<pre><code class="language-html">&lt;div&gt;Test&lt;/div&gt;</code></pre>');
192
+ expect(resultWithLanguage.tables).toEqual([]);
193
+ });
194
+ test('should not escape & within code block', () => {
195
+ const resultEscape1 = formatStringToHtml('```\n&lt;div&gt;Test&lt;/div&gt;\n```');
196
+ expect(resultEscape1.html).toEqual('<pre><code>&lt;div&gt;Test&lt;/div&gt;</code></pre>');
197
+ expect(resultEscape1.tables).toEqual([]);
198
+ const resultEscape2 = formatStringToHtml('```\n&amp;lt;div&amp;gt;Test&amp;lt;/div&amp;gt;\n```');
199
+ expect(resultEscape2.html).toEqual('<pre><code>&amp;lt;div&amp;gt;Test&amp;lt;/div&amp;gt;</code></pre>');
200
+ expect(resultEscape2.tables).toEqual([]);
201
+ });
202
+ });
203
+ test('should not format markdown within code block', () => {
204
+ const resultWithoutLanguage = formatStringToHtml('```\n**Test**\n```');
205
+ expect(resultWithoutLanguage.html).toEqual('<pre><code>**Test**</code></pre>');
206
+ expect(resultWithoutLanguage.tables).toEqual([]);
207
+ const resultWithLanguage = formatStringToHtml('```html\n**Test**\n```');
208
+ expect(resultWithLanguage.html).toEqual('<pre><code class="language-html">**Test**</code></pre>');
209
+ expect(resultWithLanguage.tables).toEqual([]);
210
+ });
211
+ test('should not format bb-code within code block', () => {
212
+ const resultWithoutLanguage = formatStringToHtml('```\n[b]Test[/b]\n```');
213
+ expect(resultWithoutLanguage.html).toEqual('<pre><code>[b]Test[/b]</code></pre>');
214
+ expect(resultWithoutLanguage.tables).toEqual([]);
215
+ const resultWithLanguage = formatStringToHtml('```html\n[b]Test[/b]\n```');
216
+ expect(resultWithLanguage.html).toEqual('<pre><code class="language-html">[b]Test[/b]</code></pre>');
217
+ expect(resultWithLanguage.tables).toEqual([]);
218
+ });
219
+ });
220
+ describe('Table', () => {
221
+ test('should format markdown within table correctly', () => {
222
+ const inputString = '| Header 1 | Header 2 |\n|----------|----------|\n| **Cell 1** | *Cell 2* |';
223
+ const result = formatStringToHtml(inputString);
224
+ expect(removeLinebreaks(result.html)).toEqual('<table id="formatted-table-0"><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td><strong>Cell 1</strong></td><td><em>Cell 2</em></td></tr></tbody></table>');
225
+ expect(result.tables).toEqual([{
226
+ csv: 'Header 1,Header 2\n**Cell 1**,*Cell 2*\n',
227
+ raw: inputString,
228
+ id: 'formatted-table-0'
229
+ }]);
230
+ });
231
+ test('should format bb-code within table correctly', () => {
232
+ const inputString = '| Header 1 | Header 2 |\n|----------|----------|\n| [b]Cell 1[/b] | [i]Cell 2[/i] |';
233
+ const result = formatStringToHtml(inputString, {
234
+ parseBBCode: true
235
+ });
236
+ expect(removeLinebreaks(result.html)).toEqual('<table id="formatted-table-0"><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td><b>Cell 1</b></td><td><i>Cell 2</i></td></tr></tbody></table>');
237
+ expect(result.tables).toEqual([{
238
+ csv: 'Header 1,Header 2\n[b]Cell 1[/b],[i]Cell 2[/i]\n',
239
+ raw: inputString,
240
+ id: 'formatted-table-0'
241
+ }]);
242
+ });
243
+ test('should format html within table correctly', () => {
244
+ const inputString = '| Header 1 | Header 2 | Header 3 |\n|----------|----------|----------|\n| <div>Cell 1</div> | &lt;div&gt;Cell 2&lt;/div&gt; | &amp;lt;div&amp;gt;Cell 3&amp;lt;/div&amp;gt; |';
245
+ const result = formatStringToHtml(inputString, {
246
+ parseBBCode: true
247
+ });
248
+ expect(removeLinebreaks(result.html)).toEqual('<table id="formatted-table-0"><thead><tr><th>Header 1</th><th>Header 2</th><th>Header 3</th></tr></thead><tbody><tr><td>&lt;div&gt;Cell 1&lt;/div&gt;</td><td>&lt;div&gt;Cell 2&lt;/div&gt;</td><td>&amp;lt;div&amp;gt;Cell 3&amp;lt;/div&amp;gt;</td></tr></tbody></table>');
249
+ expect(result.tables).toEqual([{
250
+ csv: 'Header 1,Header 2,Header 3\n<div>Cell 1</div>,<div>Cell 2</div>,&lt;div&gt;Cell 3&lt;/div&gt;\n',
251
+ raw: '| Header 1 | Header 2 | Header 3 |\n|----------|----------|----------|\n| <div>Cell 1</div> | <div>Cell 2</div> | &lt;div&gt;Cell 3&lt;/div&gt; |',
252
+ id: 'formatted-table-0'
253
+ }]);
254
+ });
255
+ test('should format multiple tables correctly', () => {
256
+ const table1 = '| Header 1 | Header 2 |\n|----------|----------|\n| Cell 1 | Cell 2 |';
257
+ const getTable1Result = index => `<table id="formatted-table-${index}"><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td>Cell 1</td><td>Cell 2</td></tr></tbody></table>`;
258
+ const table1Csv = 'Header 1,Header 2\nCell 1,Cell 2\n';
259
+ const table2 = '| Header 3 | Header 4 |\n|----------|----------|\n| Cell 3 | Cell 4 |';
260
+ const getTable2TResult = index => `<table id="formatted-table-${index}"><thead><tr><th>Header 3</th><th>Header 4</th></tr></thead><tbody><tr><td>Cell 3</td><td>Cell 4</td></tr></tbody></table>`;
261
+ const table2Csv = 'Header 3,Header 4\nCell 3,Cell 4\n';
262
+ const result1 = formatStringToHtml(`${table1}\n\n${table2}`);
263
+ expect(removeLinebreaks(result1.html)).toEqual(`${getTable1Result(0)}${getTable2TResult(1)}`);
264
+ expect(result1.tables).toEqual([{
265
+ csv: table1Csv,
266
+ raw: `${table1}\n\n`,
267
+ id: 'formatted-table-0'
268
+ }, {
269
+ csv: table2Csv,
270
+ raw: table2,
271
+ id: 'formatted-table-1'
272
+ }]);
273
+
274
+ // Tables in reverse order.
275
+ const result2 = formatStringToHtml(`${table2}\n\n${table1}`);
276
+ expect(removeLinebreaks(result2.html)).toEqual(`${getTable2TResult(0)}${getTable1Result(1)}`);
277
+ expect(result2.tables).toEqual([{
278
+ csv: table2Csv,
279
+ raw: `${table2}\n\n`,
280
+ id: 'formatted-table-0'
281
+ }, {
282
+ csv: table1Csv,
283
+ raw: table1,
284
+ id: 'formatted-table-1'
285
+ }]);
286
+ });
287
+ });
288
+ describe('List', () => {
289
+ test('should format markdown within list correctly', () => {
290
+ const result = formatStringToHtml('- **Item 1**\n- *Item 2*');
291
+ expect(removeLinebreaks(result.html)).toEqual('<ul><li><strong>Item 1</strong></li><li><em>Item 2</em></li></ul>');
292
+ expect(result.tables).toEqual([]);
293
+ });
294
+ test('should format task lists correctly', () => {
295
+ const result1 = formatStringToHtml('- [ ] 1\n- [x] 2\n- [X] 3');
296
+ expect(removeLinebreaks(result1.html)).toEqual('<ul><li>[ ] 1</li><li>[x] 2</li><li>[x] 3</li></ul>');
297
+ expect(result1.tables).toEqual([]);
298
+ });
299
+ });
300
+ describe('Combined Elements', () => {
301
+ test('should format code block within list correctly', () => {
302
+ const result = formatStringToHtml('* test\n ```\n test\n ```');
303
+ expect(removeLinebreaks(result.html)).toEqual('<ul><li>test<pre><code>test</code></pre></li></ul>');
304
+ expect(result.tables).toEqual([]);
305
+ });
306
+ });
307
+ describe('Conflicts with BB-Code', () => {
308
+ test('should not format bb code tag followed by paranthese to link', () => {
309
+ const result = formatStringToHtml('[b]bold[/b](test)', {
310
+ parseBBCode: true
311
+ });
312
+ expect(result.html).toEqual('<p><b>bold</b>(test)</p>');
313
+ expect(result.tables).toEqual([]);
314
+ });
315
+ });
316
+ });
317
+ describe('Format BB-Code', () => {
318
+ describe('All Elements', () => {
319
+ describe('Inline Elements', () => {
320
+ // b, strong, i, em, u, s, span, img
321
+ test('should format b tag correctly', () => {
322
+ const result = formatStringToHtml('[b]bold[/b]', {
323
+ parseBBCode: true
324
+ });
325
+ expect(result.html).toEqual('<p><b>bold</b></p>');
326
+ expect(result.tables).toEqual([]);
327
+ });
328
+ test('should format strong tag correctly', () => {
329
+ const result = formatStringToHtml('[strong]bold[/strong]', {
330
+ parseBBCode: true
331
+ });
332
+ expect(result.html).toEqual('<p><strong>bold</strong></p>');
333
+ expect(result.tables).toEqual([]);
334
+ });
335
+ test('should format i tag correctly', () => {
336
+ const result = formatStringToHtml('[i]italic[/i]', {
337
+ parseBBCode: true
338
+ });
339
+ expect(result.html).toEqual('<p><i>italic</i></p>');
340
+ expect(result.tables).toEqual([]);
341
+ });
342
+ test('should format em tag correctly', () => {
343
+ const result = formatStringToHtml('[em]italic[/em]', {
344
+ parseBBCode: true
345
+ });
346
+ expect(result.html).toEqual('<p><em>italic</em></p>');
347
+ expect(result.tables).toEqual([]);
348
+ });
349
+ test('should format u tag correctly', () => {
350
+ const result = formatStringToHtml('[u]underline[/u]', {
351
+ parseBBCode: true
352
+ });
353
+ expect(result.html).toEqual('<p><u>underline</u></p>');
354
+ expect(result.tables).toEqual([]);
355
+ });
356
+ test('should format s tag correctly', () => {
357
+ const result = formatStringToHtml('[s]strike[/s]', {
358
+ parseBBCode: true
359
+ });
360
+ expect(result.html).toEqual('<p><s>strike</s></p>');
361
+ expect(result.tables).toEqual([]);
362
+ });
363
+ test('should format span tag correctly', () => {
364
+ const result = formatStringToHtml('[span]span[/span]', {
365
+ parseBBCode: true
366
+ });
367
+ expect(result.html).toEqual('<p><span>span</span></p>');
368
+ expect(result.tables).toEqual([]);
369
+ });
370
+ test('should format img tag correctly', () => {
371
+ const result = formatStringToHtml('[img src="https://example.com/image.jpg"][/img]', {
372
+ parseBBCode: true
373
+ });
374
+ expect(result.html).toEqual('<p><img src="https://example.com/image.jpg"></p>');
375
+ expect(result.tables).toEqual([]);
376
+ });
377
+ });
378
+ describe('Block Level Elements', () => {
379
+ test('should format center tag correctly', () => {
380
+ const result = formatStringToHtml('[center]centered text[/center]', {
381
+ parseBBCode: true
382
+ });
383
+ expect(result.html).toEqual('<p><center>centered text</center></p>');
384
+ expect(result.tables).toEqual([]);
385
+ });
386
+ test('should format ul tag correctly', () => {
387
+ const result = formatStringToHtml('[ul][li]Item 1[/li][li]Item 2[/li][/ul]', {
388
+ parseBBCode: true
389
+ });
390
+ expect(result.html).toEqual('<p><ul><li>Item 1</li><li>Item 2</li></ul></p>');
391
+ expect(result.tables).toEqual([]);
392
+ });
393
+ test('should format ol tag correctly', () => {
394
+ const result = formatStringToHtml('[ol][li]Item 1[/li][li]Item 2[/li][/ol]', {
395
+ parseBBCode: true
396
+ });
397
+ expect(result.html).toEqual('<p><ol><li>Item 1</li><li>Item 2</li></ol></p>');
398
+ expect(result.tables).toEqual([]);
399
+ });
400
+ test('should format li tag correctly', () => {
401
+ const result = formatStringToHtml('[li]Item[/li]', {
402
+ parseBBCode: true
403
+ });
404
+ expect(result.html).toEqual('<p><li>Item</li></p>');
405
+ expect(result.tables).toEqual([]);
406
+ });
407
+ test('should format heading tags correctly', () => {
408
+ const getHeadingInput = tag => `[${tag}]${tag}[/${tag}]`;
409
+ const getHeadingOutput = tag => `<p><${tag}>${tag}</${tag}></p>`;
410
+ const headingTags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
411
+ headingTags.forEach(tag => {
412
+ const result = formatStringToHtml(getHeadingInput(tag), {
413
+ parseBBCode: true
414
+ });
415
+ expect(result.html).toEqual(getHeadingOutput(tag));
416
+ expect(result.tables).toEqual([]);
417
+ });
418
+ });
419
+ test('should format p tag correctly', () => {
420
+ const result = formatStringToHtml('[p]paragraph[/p]', {
421
+ parseBBCode: true
422
+ });
423
+ expect(result.html).toEqual('<p><p>paragraph</p></p>');
424
+ expect(result.tables).toEqual([]);
425
+ });
426
+ });
427
+ describe('Custom Elements', () => {
428
+ test('should format custom tags correctly', () => {
429
+ const input = '[custom attribute1="test1" attribute2="test2"]custom[/custom]';
430
+ const output = '<p><bb-code-custom attribute1="test1" attribute2="test2">custom</bb-code-custom></p>';
431
+ const result1 = formatStringToHtml(input, {
432
+ parseBBCode: true,
433
+ customInlineLevelBBCodeTags: ['custom']
434
+ });
435
+ expect(result1.html).toEqual(output);
436
+ expect(result1.tables).toEqual([]);
437
+ const result2 = formatStringToHtml(input, {
438
+ parseBBCode: true,
439
+ customBlockLevelBBCodeTags: ['custom']
440
+ });
441
+ expect(result2.html).toEqual(output);
442
+ expect(result2.tables).toEqual([]);
443
+ });
444
+ test('should not format unknown custom tags', () => {
445
+ const result = formatStringToHtml('[unknown]unknown[/unknown]', {
446
+ parseBBCode: true
447
+ });
448
+ expect(result.html).toEqual('<p>[unknown]unknown[/unknown]</p>');
449
+ expect(result.tables).toEqual([]);
450
+ });
451
+ });
452
+ describe('Line breaks', () => {
453
+ describe('Between Elements', () => {
454
+ test('should format line breaks between block level elements correctly', () => {
455
+ const result1 = formatStringToHtml('[h1]h1[/h1]\n[h2]h2[/h2]', {
456
+ parseBBCode: true
457
+ });
458
+ expect(result1.html).toEqual('<p><h1>h1</h1><h2>h2</h2></p>');
459
+ expect(result1.tables).toEqual([]);
460
+ const result2 = formatStringToHtml('[h1]h1[/h1]\n\n[h2]h2[/h2]', {
461
+ parseBBCode: true
462
+ });
463
+ expect(result2.html).toEqual('<p><h1>h1</h1></p>\n<p><h2>h2</h2></p>');
464
+ expect(result2.tables).toEqual([]);
465
+ });
466
+ test('should format line breaks between inline elements correctly', () => {
467
+ const result1 = formatStringToHtml('[b]bold[/b]\n[i]italic[/i]', {
468
+ parseBBCode: true
469
+ });
470
+ expect(result1.html).toEqual('<p><b>bold</b>\n<i>italic</i></p>');
471
+ expect(result1.tables).toEqual([]);
472
+ const result2 = formatStringToHtml('[b]bold[/b]\n\n[i]italic[/i]', {
473
+ parseBBCode: true
474
+ });
475
+ expect(result2.html).toEqual('<p><b>bold</b></p>\n<p><i>italic</i></p>');
476
+ expect(result2.tables).toEqual([]);
477
+ });
478
+ });
479
+ describe('Within Elements', () => {
480
+ test('should format line breaks within block level elements correctly', () => {
481
+ const result = formatStringToHtml('[h1]Line 1\nLine 2[/h1]', {
482
+ parseBBCode: true
483
+ });
484
+ expect(result.html).toEqual('<p><h1>Line 1\nLine 2</h1></p>');
485
+ expect(result.tables).toEqual([]);
486
+ });
487
+
488
+ // This is a test that would fail!
489
+ // test('should format multiple line breaks within block level elements correctly', () => {
490
+ // const result = formatStringToHtml('[h1]Line 1\n\nLine 2[/h1]', {
491
+ // parseBBCode: true,
492
+ // });
493
+ // expect(result.html).toEqual('<p><h1>Line 1\nLine 2</h1></p>');
494
+ // expect(result.tables).toEqual([]);
495
+ // });
496
+
497
+ test('should remove trailing and leading new lines within block level elements', () => {
498
+ const result = formatStringToHtml('[h1]\n\n\nLine 1\n\n\n[/h1]', {
499
+ parseBBCode: true
500
+ });
501
+ expect(result.html).toEqual('<p><h1>Line 1</h1></p>');
502
+ expect(result.tables).toEqual([]);
503
+ });
504
+ });
505
+ });
506
+ test('should not format url in tag attributes', () => {
507
+ const result = formatStringToHtml('[link url="https://www.google.com"]Google[/link]', {
508
+ parseBBCode: true,
509
+ customInlineLevelBBCodeTags: ['link']
510
+ });
511
+ expect(result.html).toEqual('<p><bb-code-link url="https://www.google.com">Google</bb-code-link></p>');
512
+ expect(result.tables).toEqual([]);
513
+ });
514
+ test('should apply style attribute correctly', () => {
515
+ const result = formatStringToHtml('[span style="color: red;"]red text[/span]', {
516
+ parseBBCode: true
517
+ });
518
+ expect(result.html).toEqual('<p><span style="color: red;">red text</span></p>');
519
+ expect(result.tables).toEqual([]);
520
+ });
521
+ test('should format nested elements correctly', () => {
522
+ const result = formatStringToHtml('[b][i]bold italic[/i][/b]', {
523
+ parseBBCode: true
524
+ });
525
+ expect(result.html).toEqual('<p><b><i>bold italic</i></b></p>');
526
+ expect(result.tables).toEqual([]);
527
+ });
528
+ });
529
+ });
530
+ describe('Combined Formatting (Markdown + BB-Code)', () => {
531
+ test('should format bb-code within markdown correctly', () => {
532
+ const result = formatStringToHtml('*[b]bold[/b]*', {
533
+ parseBBCode: true
534
+ });
535
+ expect(result.html).toEqual('<p><em><b>bold</b></em></p>');
536
+ expect(result.tables).toEqual([]);
537
+ });
538
+ test('should format markdown within bb-code correctly', () => {
539
+ const result = formatStringToHtml('[b]*bold*[/b]', {
540
+ parseBBCode: true
541
+ });
542
+ expect(result.html).toEqual('<p><b><em>bold</em></b></p>');
543
+ expect(result.tables).toEqual([]);
544
+ });
545
+ });
546
+ });
547
+ //# sourceMappingURL=formatString.test.js.map