Skip to content

Commit b27bd86

Browse files
authored
feat: allow hiding html code blocks (#113)
1 parent c06bbdb commit b27bd86

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ You can include multiple HTML blocks in each fiddle
9494

9595
<!-- fiddle-end -->
9696

97+
### Hide HTML code block
98+
99+
You can hide the HTML code block from the source view, yet still have it part of the live app by adding "hide" meta after the `html`
100+
101+
<!-- fiddle Hide HTML code block -->
102+
103+
```html hide
104+
<div id="greeting">Hello</div>
105+
```
106+
107+
```js
108+
cy.get('div#greeting').should('have.text', 'Hello')
109+
```
110+
111+
<!-- fiddle-end -->
112+
97113
### Suites
98114

99115
To create suites of tests, use `/` to separate the suite name from the test name. Tests with the same suite name are grouped together automatically.

cypress/integration/extract-fiddles.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ chai.config.truncateThreshold = 1000
88

99
/** @type {string} */
1010
let fiddleCommentMarkdown
11+
/** @type {string} */
12+
let fiddleHtmlBlocksMarkdown
1113

1214
before(() => {
1315
cy.readFile('cypress/integration/fiddle-comment.md').then(
1416
(md) => (fiddleCommentMarkdown = md),
1517
)
18+
19+
cy.readFile('cypress/integration/multiple-html-blocks.md').then(
20+
(md) => (fiddleHtmlBlocksMarkdown = md),
21+
)
1622
})
1723

1824
describe('extractFiddles', () => {
@@ -130,4 +136,55 @@ describe('extractFiddles', () => {
130136
],
131137
})
132138
})
139+
140+
it('extracts html blocks', () => {
141+
const fiddles = extractFiddles(fiddleHtmlBlocksMarkdown)
142+
expect(fiddles)
143+
.to.have.property('Multiple HTML code blocks')
144+
.and.to.be.an('array')
145+
.and.to.have.length(2)
146+
const [fiddle1, fiddle2] = fiddles['Multiple HTML code blocks']
147+
expect(fiddle1, 'first fiddle').to.deep.equal({
148+
name: 'Multiple HTML code blocks',
149+
test: stripIndent`
150+
cy.get('div#greeting').should('have.text', 'Hello')
151+
cy.get('div#name').should('have.text', 'World')
152+
`,
153+
html: [
154+
{
155+
source: '<div id="greeting">Hello</div>',
156+
hide: false,
157+
},
158+
{
159+
source: '<div id="name">World</div>',
160+
hide: false,
161+
},
162+
],
163+
commonHtml: null,
164+
only: false,
165+
skip: false,
166+
export: false,
167+
})
168+
expect(fiddle2, 'second fiddle').to.deep.equal({
169+
name: 'Hide an HTML code block',
170+
test: stripIndent`
171+
cy.get('div#greeting').should('have.text', 'Hello')
172+
cy.get('div#name').should('have.text', 'World')
173+
`,
174+
html: [
175+
{
176+
source: '<div id="greeting">Hello</div>',
177+
hide: false,
178+
},
179+
{
180+
source: '<div id="name">World</div>',
181+
hide: true,
182+
},
183+
],
184+
commonHtml: null,
185+
only: false,
186+
skip: false,
187+
export: false,
188+
})
189+
})
133190
})

cypress/integration/multiple-html-blocks.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,22 @@ cy.get('div#name').should('have.text', 'World')
1818
```
1919

2020
<!-- fiddle-end -->
21+
22+
<!-- fiddle Hide an HTML code block -->
23+
24+
```html
25+
<div id="greeting">Hello</div>
26+
```
27+
28+
Another HTML code block that is live but not in the source view
29+
30+
```html hide
31+
<div id="name">World</div>
32+
```
33+
34+
```js
35+
cy.get('div#greeting').should('have.text', 'Hello')
36+
cy.get('div#name').should('have.text', 'World')
37+
```
38+
39+
<!-- fiddle-end -->

src/markdown-utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,10 @@ function extractFiddles(md) {
270270
.filter(isHtmlCodeBlock)
271271
.filter(shouldIncludeBlock)
272272
.map((htmlCodeBlock) => {
273+
const meta = htmlCodeBlock.meta || ''
273274
return {
274275
source: htmlCodeBlock.value,
275-
hide: false,
276+
hide: meta.includes('hide'),
276277
}
277278
})
278279
// console.log(htmlMaybe)

0 commit comments

Comments
 (0)