|
| 1 | +package plugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + md "github.com/firecrawl/html-to-markdown" |
| 8 | +) |
| 9 | + |
| 10 | +func TestRobustCodeBlock_LanguageDetection(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + html string |
| 14 | + expected string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "language- prefix on code", |
| 18 | + html: `<pre><code class="language-javascript">const x = 1;</code></pre>`, |
| 19 | + expected: "```javascript", |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "lang- prefix on code", |
| 23 | + html: `<pre><code class="lang-python">print("hello")</code></pre>`, |
| 24 | + expected: "```python", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "language on pre element", |
| 28 | + html: `<pre class="language-go"><code>func main() {}</code></pre>`, |
| 29 | + expected: "```go", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "no language class", |
| 33 | + html: `<pre><code>plain code</code></pre>`, |
| 34 | + expected: "```\nplain code", |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + conv := md.NewConverter("", true, nil) |
| 39 | + conv.Use(RobustCodeBlock()) |
| 40 | + |
| 41 | + for _, tt := range tests { |
| 42 | + t.Run(tt.name, func(t *testing.T) { |
| 43 | + markdown, err := conv.ConvertString(tt.html) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("ConvertString failed: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + if !strings.Contains(markdown, tt.expected) { |
| 49 | + t.Errorf("expected output to contain %q\nGot:\n%s", tt.expected, markdown) |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestRobustCodeBlock_GutterStripping(t *testing.T) { |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + html string |
| 59 | + shouldHave []string |
| 60 | + shouldntHave []string |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "strips gutter class", |
| 64 | + html: `<pre><code><table><tr><td class="gutter">1 |
| 65 | +2 |
| 66 | +3</td><td class="code">const a = 1; |
| 67 | +const b = 2; |
| 68 | +const c = 3;</td></tr></table></code></pre>`, |
| 69 | + shouldHave: []string{"const a = 1", "const b = 2", "const c = 3"}, |
| 70 | + shouldntHave: []string{"\n1\n2\n3"}, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "strips line-numbers class", |
| 74 | + html: `<pre><code><div class="line-numbers">1 |
| 75 | +2</div><div class="content">hello |
| 76 | +world</div></code></pre>`, |
| 77 | + shouldHave: []string{"hello", "world"}, |
| 78 | + shouldntHave: []string{}, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + conv := md.NewConverter("", true, nil) |
| 83 | + conv.Use(RobustCodeBlock()) |
| 84 | + |
| 85 | + for _, tt := range tests { |
| 86 | + t.Run(tt.name, func(t *testing.T) { |
| 87 | + markdown, err := conv.ConvertString(tt.html) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("ConvertString failed: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + for _, exp := range tt.shouldHave { |
| 93 | + if !strings.Contains(markdown, exp) { |
| 94 | + t.Errorf("expected output to contain %q\nGot:\n%s", exp, markdown) |
| 95 | + } |
| 96 | + } |
| 97 | + for _, notExp := range tt.shouldntHave { |
| 98 | + if strings.Contains(markdown, notExp) { |
| 99 | + t.Errorf("expected output NOT to contain %q\nGot:\n%s", notExp, markdown) |
| 100 | + } |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestRobustCodeBlock_SyntaxHighlighterDivs(t *testing.T) { |
| 107 | + tests := []struct { |
| 108 | + name string |
| 109 | + html string |
| 110 | + expected []string |
| 111 | + }{ |
| 112 | + { |
| 113 | + name: "highlight.js style", |
| 114 | + html: `<pre><code class="language-json"><div class="hljs">{ |
| 115 | + "key": "value" |
| 116 | +}</div></code></pre>`, |
| 117 | + expected: []string{`"key"`, `"value"`}, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "prism.js style with spans", |
| 121 | + html: `<pre><code class="language-javascript"><span class="token keyword">const</span> <span class="token variable">x</span> <span class="token operator">=</span> <span class="token number">42</span><span class="token punctuation">;</span></code></pre>`, |
| 122 | + expected: []string{"const", "x", "=", "42"}, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "nested divs with token-line", |
| 126 | + html: `<pre><code><div class="token-line">line1</div><div class="token-line">line2</div></code></pre>`, |
| 127 | + expected: []string{"line1", "line2"}, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + conv := md.NewConverter("", true, nil) |
| 132 | + conv.Use(RobustCodeBlock()) |
| 133 | + |
| 134 | + for _, tt := range tests { |
| 135 | + t.Run(tt.name, func(t *testing.T) { |
| 136 | + markdown, err := conv.ConvertString(tt.html) |
| 137 | + if err != nil { |
| 138 | + t.Fatalf("ConvertString failed: %v", err) |
| 139 | + } |
| 140 | + |
| 141 | + for _, exp := range tt.expected { |
| 142 | + if !strings.Contains(markdown, exp) { |
| 143 | + t.Errorf("expected output to contain %q\nGot:\n%s", exp, markdown) |
| 144 | + } |
| 145 | + } |
| 146 | + }) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func TestRobustCodeBlock_InlineCode(t *testing.T) { |
| 151 | + tests := []struct { |
| 152 | + name string |
| 153 | + html string |
| 154 | + expected string |
| 155 | + }{ |
| 156 | + { |
| 157 | + name: "simple inline code", |
| 158 | + html: `<p>Use <code>fmt.Println</code> to print</p>`, |
| 159 | + expected: "`fmt.Println`", |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "inline code with backticks", |
| 163 | + html: `<p>Use <code>echo ` + "`hello`" + `</code> command</p>`, |
| 164 | + expected: "``echo `hello```", |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "inline code not affected by pre rule", |
| 168 | + html: `<p>The <code>main</code> function is important</p>`, |
| 169 | + expected: "`main`", |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + conv := md.NewConverter("", true, nil) |
| 174 | + conv.Use(RobustCodeBlock()) |
| 175 | + |
| 176 | + for _, tt := range tests { |
| 177 | + t.Run(tt.name, func(t *testing.T) { |
| 178 | + markdown, err := conv.ConvertString(tt.html) |
| 179 | + if err != nil { |
| 180 | + t.Fatalf("ConvertString failed: %v", err) |
| 181 | + } |
| 182 | + |
| 183 | + if !strings.Contains(markdown, tt.expected) { |
| 184 | + t.Errorf("expected output to contain %q\nGot:\n%s", tt.expected, markdown) |
| 185 | + } |
| 186 | + }) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +func TestRobustCodeBlock_PreservesNewlines(t *testing.T) { |
| 191 | + html := `<pre><code class="language-python">def hello(): |
| 192 | + print("world") |
| 193 | +
|
| 194 | +hello()</code></pre>` |
| 195 | + |
| 196 | + conv := md.NewConverter("", true, nil) |
| 197 | + conv.Use(RobustCodeBlock()) |
| 198 | + |
| 199 | + markdown, err := conv.ConvertString(html) |
| 200 | + if err != nil { |
| 201 | + t.Fatalf("ConvertString failed: %v", err) |
| 202 | + } |
| 203 | + |
| 204 | + // Check structure is preserved |
| 205 | + if !strings.Contains(markdown, "def hello():") { |
| 206 | + t.Error("missing function definition") |
| 207 | + } |
| 208 | + if !strings.Contains(markdown, `print("world")`) { |
| 209 | + t.Error("missing print statement") |
| 210 | + } |
| 211 | + if !strings.Contains(markdown, "hello()") { |
| 212 | + t.Error("missing function call") |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +func TestRobustCodeBlock_BrTags(t *testing.T) { |
| 217 | + html := `<pre><code>line1<br>line2<br/>line3</code></pre>` |
| 218 | + |
| 219 | + conv := md.NewConverter("", true, nil) |
| 220 | + conv.Use(RobustCodeBlock()) |
| 221 | + |
| 222 | + markdown, err := conv.ConvertString(html) |
| 223 | + if err != nil { |
| 224 | + t.Fatalf("ConvertString failed: %v", err) |
| 225 | + } |
| 226 | + |
| 227 | + if !strings.Contains(markdown, "line1") { |
| 228 | + t.Error("missing line1") |
| 229 | + } |
| 230 | + if !strings.Contains(markdown, "line2") { |
| 231 | + t.Error("missing line2") |
| 232 | + } |
| 233 | + if !strings.Contains(markdown, "line3") { |
| 234 | + t.Error("missing line3") |
| 235 | + } |
| 236 | +} |
0 commit comments