Skip to content

Commit 77a6f08

Browse files
authored
feat: Add css code block support (#114)
* feat: add CSS code block support * update the extract fiddle spec
1 parent b27bd86 commit 77a6f08

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ You can hide the HTML code block from the source view, yet still have it part of
110110

111111
<!-- fiddle-end -->
112112

113+
### CSS block
114+
115+
You can add CSS to be applied to the HTML application. The CSS block is not shown on the output page.
116+
117+
<!-- fiddle CSS Code block -->
118+
```html
119+
<div id="greeting">Hello</div>
120+
```
121+
```css
122+
#greeting {
123+
color: #f0f;
124+
padding: 1rem;
125+
font-weight: bold;
126+
}
127+
```
128+
```js
129+
cy.get('div#greeting')
130+
.should('have.text', 'Hello')
131+
.and('have.css', 'color', 'rgb(255, 0, 255)')
132+
```
133+
<!-- fiddle-end -->
134+
113135
### Suites
114136

115137
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.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!-- fiddle CSS Code block -->
2+
3+
```html
4+
<div id="greeting">Hello</div>
5+
```
6+
7+
```css
8+
#greeting {
9+
color: #f0f;
10+
padding: 1rem;
11+
font-weight: bold;
12+
}
13+
```
14+
15+
```js
16+
cy.get('div#greeting')
17+
.should('have.text', 'Hello')
18+
.and('have.css', 'color', 'rgb(255, 0, 255)')
19+
```
20+
21+
<!-- fiddle-end -->

cypress/integration/extract-fiddles.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ describe('extractFiddles', () => {
129129
test: "console.log('1')",
130130
html: null,
131131
commonHtml: null,
132+
css: null,
132133
only: false,
133134
skip: false,
134135
export: false,
@@ -161,6 +162,7 @@ describe('extractFiddles', () => {
161162
},
162163
],
163164
commonHtml: null,
165+
css: null,
164166
only: false,
165167
skip: false,
166168
export: false,
@@ -182,6 +184,7 @@ describe('extractFiddles', () => {
182184
},
183185
],
184186
commonHtml: null,
187+
css: null,
185188
only: false,
186189
skip: false,
187190
export: false,

src/markdown-utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,21 @@ function extractFiddles(md) {
260260
const isJavaScript = (n) =>
261261
n.type === 'CodeBlock' &&
262262
(n.lang === 'js' || n.lang === 'javascript')
263+
const isCssCodeBlock = (n) =>
264+
n.type === 'CodeBlock' && n.lang === 'css'
263265

264266
fiddles.forEach((fiddle) => {
265267
const ast = parse(fiddle.fiddle)
266268
// console.log('markdown fiddle AST')
267269
// console.log(ast)
268270

271+
const cssMaybe = ast.children
272+
.filter(isCssCodeBlock)
273+
.filter(shouldIncludeBlock)
274+
.map((codeBlock) => codeBlock.value)
275+
.join('\n')
276+
// console.log(cssMaybe)
277+
269278
const htmlMaybe = ast.children
270279
.filter(isHtmlCodeBlock)
271280
.filter(shouldIncludeBlock)
@@ -313,6 +322,7 @@ function extractFiddles(md) {
313322
? htmlMaybe
314323
: null,
315324
commonHtml,
325+
css: cssMaybe || null,
316326
only: fiddle.only,
317327
skip: fiddle.skip,
318328
export: fiddle.export,

0 commit comments

Comments
 (0)