Skip to content

Commit 696e421

Browse files
authored
fix: ignore HTML comments (#283)
1 parent 1240c32 commit 696e421

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

cypress/e2e/comments.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Comments
2+
3+
<!-- fiddle The selector should be escaped -->
4+
5+
<!-- prettier-ignore-start -->
6+
7+
```html hide
8+
<div data-cy="info" style="display:none;">
9+
<span data-user-id="user:1"></span>
10+
</div>
11+
<div id="users">
12+
<div id="user-1">John Doe</div>
13+
</div>
14+
<div id="students">
15+
<div id="user-2">Mary Sue</div>
16+
</div>
17+
```
18+
19+
<!-- prettier-ignore-end -->
20+
21+
```js
22+
cy.wrap(42).should('equal', 42)
23+
```
24+
25+
Check the HTML elements
26+
27+
```js
28+
cy.get('[data-user-id="user:1"]').should('have.text', '')
29+
cy.get('#user-2').should('have.text', 'Mary Sue')
30+
```
31+
32+
<!--
33+
multi-line comment
34+
goes here
35+
-->
36+
37+
<!-- fiddle-end -->

src/markdown-utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ const extractFiddleMarkup = (s) => {
3939
return s
4040
}
4141

42+
const isMoreThanAComment = (s) => {
43+
if (!s) {
44+
return false
45+
}
46+
47+
const trimmed = s.trim()
48+
return !(
49+
trimmed.startsWith('<!--') &&
50+
trimmed.endsWith('-->') &&
51+
trimmed.length > 9
52+
)
53+
}
54+
4255
/**
4356
* Checks if the given line starts with "<!-- fiddle" or one of its variations.
4457
*/
@@ -285,14 +298,19 @@ function extractFiddles(md) {
285298
hide: meta.includes('hide'),
286299
}
287300
})
301+
// console.log('html maybe')
288302
// console.log(htmlMaybe)
289303

290304
const htmlLiveBlockMaybe = ast.children.find(
291305
(s) =>
306+
isMoreThanAComment(s.value) &&
292307
isLiveHtml(s) &&
293308
!isFiddleMarkup(s.value) &&
294309
shouldIncludeBlock(s),
295310
)
311+
// console.log('html live block')
312+
// console.log(htmlLiveBlockMaybe)
313+
296314
const htmlMarkup = ast.children.find((s) =>
297315
isFiddleMarkup(s.value),
298316
)

0 commit comments

Comments
 (0)