11// @ts -check
22const { parse } = require ( '@textlint/markdown-to-ast' )
33const debug = require ( 'debug' ) ( 'cypress-markdown-preprocessor' )
4- const verbose = require ( 'debug' ) ( 'cypress-markdown-preprocessor:verbose' )
4+ const verbose = require ( 'debug' ) (
5+ 'cypress-markdown-preprocessor:verbose' ,
6+ )
57
68/**
79 * Finds optional fiddle name from the comment line
810 * `<!-- fiddle my name -->` returns "my name".
911 */
1012const findFiddleName = ( commentLine ) => {
1113 debug ( 'finding fiddle name from line: "%s"' , commentLine )
12- const matches = / f i d d l e (?: \. o n l y | \. s k i p | \. e x p o r t ) ? ( .+ ) - - > / . exec ( commentLine )
14+ const matches = / f i d d l e (?: \. o n l y | \. s k i p | \. e x p o r t ) ? ( .+ ) - - > / . exec (
15+ commentLine ,
16+ )
1317 if ( matches && matches . length ) {
1418 const testTitle = matches [ 1 ] . trim ( )
1519 debug ( 'test title: "%s"' , testTitle )
@@ -21,7 +25,8 @@ const findFiddleName = (commentLine) => {
2125
2226const isFiddleOnly = ( line ) => line . startsWith ( '<!-- fiddle.only ' )
2327const isFiddleSkip = ( line ) => line . startsWith ( '<!-- fiddle.skip ' )
24- const isFiddleExport = ( line ) => line . startsWith ( '<!-- fiddle.export ' )
28+ const isFiddleExport = ( line ) =>
29+ line . startsWith ( '<!-- fiddle.export ' )
2530
2631const isFiddleMarkup = ( s ) => s && s . startsWith ( '<!-- fiddle-markup' )
2732
@@ -79,7 +84,9 @@ function extractFiddles(md) {
7984 let startLine
8085 do {
8186 debug ( 'start with %d' , start )
82- start = lines . findIndex ( ( line , k ) => k >= start && isFiddleStartLine ( line ) )
87+ start = lines . findIndex (
88+ ( line , k ) => k >= start && isFiddleStartLine ( line ) ,
89+ )
8390 if ( start === - 1 ) {
8491 break
8592 }
@@ -117,28 +124,47 @@ function extractFiddles(md) {
117124 // list of fiddles converted into JavaScript
118125 const testFiddles = [ ]
119126
120- const isHtmlCodeBlock = ( n ) => n . type === 'CodeBlock' && n . lang === 'html'
127+ const shouldIncludeBlock = ( block ) => {
128+ if ( ! block . meta ) {
129+ return true
130+ }
131+ return ! block . meta . includes ( 'skip' )
132+ }
133+
134+ const isHtmlCodeBlock = ( n ) =>
135+ n . type === 'CodeBlock' && n . lang === 'html'
121136 const isLiveHtml = ( n ) => n . type === 'Html'
122137 const isJavaScript = ( n ) =>
123- n . type === 'CodeBlock' && ( n . lang === 'js' || n . lang === 'javascript' )
138+ n . type === 'CodeBlock' &&
139+ ( n . lang === 'js' || n . lang === 'javascript' )
124140
125141 fiddles . forEach ( ( fiddle ) => {
126142 const ast = parse ( fiddle . fiddle )
127143 // console.log('markdown fiddle AST')
128144 // console.log(ast)
129- const htmlCodeBlockMaybe = ast . children . find ( ( s ) => isHtmlCodeBlock ( s ) )
145+ const htmlCodeBlockMaybe = ast . children . find (
146+ ( s ) => isHtmlCodeBlock ( s ) && shouldIncludeBlock ( s ) ,
147+ )
130148 const htmlLiveBlockMaybe = ast . children . find (
131- ( s ) => isLiveHtml ( s ) && ! isFiddleMarkup ( s . value ) ,
149+ ( s ) =>
150+ isLiveHtml ( s ) &&
151+ ! isFiddleMarkup ( s . value ) &&
152+ shouldIncludeBlock ( s ) ,
153+ )
154+ const htmlMarkup = ast . children . find ( ( s ) =>
155+ isFiddleMarkup ( s . value ) ,
132156 )
133- const htmlMarkup = ast . children . find ( ( s ) => isFiddleMarkup ( s . value ) )
134157
135158 // console.log('found html block?', htmlMaybe)
136159
137160 // a single fiddle can have multiple JS blocks
138161 // we want to find them all and merge into a single test
139- const jsMaybe = ast . children . filter ( isJavaScript )
162+ const jsMaybe = ast . children
163+ . filter ( isJavaScript )
164+ . filter ( shouldIncludeBlock )
140165
141166 if ( jsMaybe . length ) {
167+ // console.log(jsMaybe)
142168 const testCode = jsMaybe . map ( ( b ) => b . value ) . join ( '\n' )
143169
144170 const htmlNode = htmlLiveBlockMaybe || htmlCodeBlockMaybe
0 commit comments