|
| 1 | +// |
| 2 | +// Note: This example test is leveraging the Mocha test framework. |
| 3 | +// Please refer to their documentation on https://mochajs.org/ for help. |
| 4 | +// |
| 5 | + |
| 6 | +// The module 'assert' provides assertion methods from node |
| 7 | +import * as assert from "assert"; |
| 8 | +import { toMarkdown } from "../../src/toMarkdown"; |
| 9 | + |
| 10 | +// Defines a Mocha test suite to group tests of similar kind together |
| 11 | +suite("toMarkdown Tests", () => { |
| 12 | + // Test that pipe characters in table cells are properly escaped |
| 13 | + test("should escape pipe characters in table cell content", () => { |
| 14 | + const html = ` |
| 15 | + <table> |
| 16 | + <tr> |
| 17 | + <td>Option A | Option B</td> |
| 18 | + </tr> |
| 19 | + </table> |
| 20 | + `; |
| 21 | + |
| 22 | + const result = toMarkdown(html, { |
| 23 | + emDelimiter: "*", |
| 24 | + }); |
| 25 | + |
| 26 | + // The pipe character should be escaped as \| |
| 27 | + // Expected: | Option A \| Option B | |
| 28 | + assert.ok( |
| 29 | + result.includes("Option A \\| Option B"), |
| 30 | + `Expected escaped pipe, but got: ${result}` |
| 31 | + ); |
| 32 | + |
| 33 | + // Should not create extra columns (should be a single cell) |
| 34 | + // Count the number of pipes in the row (excluding escaped ones) |
| 35 | + const rowMatch = result.match(/\n\|[^\n]+\|/); |
| 36 | + assert.ok(rowMatch, "Should contain a table row"); |
| 37 | + if (rowMatch) { |
| 38 | + const row = rowMatch[0]; |
| 39 | + // Count unescaped pipes by replacing escaped pipes temporarily |
| 40 | + const tempRow = row.replace(/\\\|/g, "ESCAPED_PIPE"); |
| 41 | + const unescapedPipes = (tempRow.match(/\|/g) || []).length; |
| 42 | + // Should have exactly 2 pipes (start and end of single cell row) |
| 43 | + assert.strictEqual( |
| 44 | + unescapedPipes, |
| 45 | + 2, |
| 46 | + `Expected 2 unescaped pipes (start and end), but found ${unescapedPipes} in: ${row}` |
| 47 | + ); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + test("should handle multiple pipe characters in table cell", () => { |
| 52 | + const html = ` |
| 53 | + <table> |
| 54 | + <tr> |
| 55 | + <td>a |= y | b</td> |
| 56 | + </tr> |
| 57 | + </table> |
| 58 | + `; |
| 59 | + |
| 60 | + const result = toMarkdown(html, { |
| 61 | + emDelimiter: "*", |
| 62 | + }); |
| 63 | + |
| 64 | + // All pipe characters should be escaped |
| 65 | + assert.ok( |
| 66 | + result.includes("a \\|= y \\| b"), |
| 67 | + `Expected all pipes escaped, but got: ${result}` |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + test("should handle table with multiple cells containing pipes", () => { |
| 72 | + const html = ` |
| 73 | + <table> |
| 74 | + <tr> |
| 75 | + <td>Option A | Option B</td> |
| 76 | + <td>Value | Test</td> |
| 77 | + </tr> |
| 78 | + </table> |
| 79 | + `; |
| 80 | + |
| 81 | + const result = toMarkdown(html, { |
| 82 | + emDelimiter: "*", |
| 83 | + }); |
| 84 | + |
| 85 | + // Both cells should have escaped pipes |
| 86 | + assert.ok( |
| 87 | + result.includes("Option A \\| Option B"), |
| 88 | + `First cell should have escaped pipe: ${result}` |
| 89 | + ); |
| 90 | + assert.ok( |
| 91 | + result.includes("Value \\| Test"), |
| 92 | + `Second cell should have escaped pipe: ${result}` |
| 93 | + ); |
| 94 | + |
| 95 | + // Should have exactly 3 unescaped pipes per row (start, between cells, end) |
| 96 | + const rowMatch = result.match(/\n\|[^\n]+\|/); |
| 97 | + assert.ok(rowMatch, "Should contain a table row"); |
| 98 | + if (rowMatch) { |
| 99 | + const row = rowMatch[0]; |
| 100 | + // Count unescaped pipes by replacing escaped pipes temporarily |
| 101 | + const tempRow = row.replace(/\\\|/g, "ESCAPED_PIPE"); |
| 102 | + const unescapedPipes = (tempRow.match(/\|/g) || []).length; |
| 103 | + assert.strictEqual( |
| 104 | + unescapedPipes, |
| 105 | + 3, |
| 106 | + `Expected 3 unescaped pipes (start, separator, end), but found ${unescapedPipes} in: ${row}` |
| 107 | + ); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + test("should not affect tables without pipe characters", () => { |
| 112 | + const html = ` |
| 113 | + <table> |
| 114 | + <tr> |
| 115 | + <td>Normal Cell Content</td> |
| 116 | + <td>Another Cell</td> |
| 117 | + </tr> |
| 118 | + </table> |
| 119 | + `; |
| 120 | + |
| 121 | + const result = toMarkdown(html, { |
| 122 | + emDelimiter: "*", |
| 123 | + }); |
| 124 | + |
| 125 | + // Should still work correctly for normal tables |
| 126 | + assert.ok( |
| 127 | + result.includes("Normal Cell Content"), |
| 128 | + "Should contain normal content" |
| 129 | + ); |
| 130 | + assert.ok(result.includes("Another Cell"), "Should contain second cell"); |
| 131 | + assert.ok(result.includes("|"), "Should contain table structure"); |
| 132 | + }); |
| 133 | + |
| 134 | + test("should handle table headers with pipe characters", () => { |
| 135 | + const html = ` |
| 136 | + <table> |
| 137 | + <thead> |
| 138 | + <tr> |
| 139 | + <th>Header A | Header B</th> |
| 140 | + </tr> |
| 141 | + </thead> |
| 142 | + <tbody> |
| 143 | + <tr> |
| 144 | + <td>Content</td> |
| 145 | + </tr> |
| 146 | + </tbody> |
| 147 | + </table> |
| 148 | + `; |
| 149 | + |
| 150 | + const result = toMarkdown(html, { |
| 151 | + emDelimiter: "*", |
| 152 | + }); |
| 153 | + |
| 154 | + // Header cell should have escaped pipe |
| 155 | + assert.ok( |
| 156 | + result.includes("Header A \\| Header B"), |
| 157 | + `Header should have escaped pipe: ${result}` |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + // Issue #145: Nested list paste from HTML (e.g. Outlook/calendar) should preserve nesting |
| 162 | + test("should indent nested ordered list items", () => { |
| 163 | + const html = ` |
| 164 | + <p>The agenda is as follows:</p> |
| 165 | + <ol> |
| 166 | + <li>Next week meeting adjustments (if needed)</li> |
| 167 | + <li>Status on assignments (by TAs)</li> |
| 168 | + <li>Issues found in past week with students (by TAs)</li> |
| 169 | + <li>Next week lecture preparation tasks</li> |
| 170 | + <li>Any Other Business |
| 171 | + <ol> |
| 172 | + <li>Kruskal removal: tried it but cannot due to assignment using it.</li> |
| 173 | + </ol> |
| 174 | + </li> |
| 175 | + </ol> |
| 176 | + `; |
| 177 | + |
| 178 | + const result = toMarkdown(html, { |
| 179 | + emDelimiter: "*", |
| 180 | + }); |
| 181 | + |
| 182 | + // Top-level items should be "1. ", "2. ", ... "5. " |
| 183 | + assert.ok( |
| 184 | + result.includes("1. Next week meeting"), |
| 185 | + `Expected top-level item 1: ${result}` |
| 186 | + ); |
| 187 | + assert.ok( |
| 188 | + result.includes("5. Any Other Business"), |
| 189 | + `Expected top-level item 5: ${result}` |
| 190 | + ); |
| 191 | + |
| 192 | + // Nested item under "Any Other Business" must be indented (4 spaces) so it renders as sub-item |
| 193 | + assert.ok( |
| 194 | + result.includes(" 1. Kruskal removal"), |
| 195 | + `Expected nested "1." to be indented with 4 spaces (Issue #145). Got: ${result}` |
| 196 | + ); |
| 197 | + assert.ok( |
| 198 | + !result.match(/\n1\. Kruskal removal/m) || |
| 199 | + result.includes(" 1. Kruskal removal"), |
| 200 | + "Nested item should not appear as top-level 1. without leading spaces" |
| 201 | + ); |
| 202 | + }); |
| 203 | + |
| 204 | + test("should indent nested unordered list items", () => { |
| 205 | + const html = ` |
| 206 | + <ul> |
| 207 | + <li>First |
| 208 | + <ul> |
| 209 | + <li>Nested bullet</li> |
| 210 | + </ul> |
| 211 | + </li> |
| 212 | + </ul> |
| 213 | + `; |
| 214 | + |
| 215 | + const result = toMarkdown(html, { |
| 216 | + emDelimiter: "*", |
| 217 | + bulletListMarker: "-", |
| 218 | + }); |
| 219 | + |
| 220 | + assert.ok( |
| 221 | + result.includes(" - Nested bullet"), |
| 222 | + `Expected nested bullet indented. Got: ${result}` |
| 223 | + ); |
| 224 | + }); |
| 225 | +}); |
0 commit comments