@@ -30,8 +30,9 @@ const isPlaywright = 'page' in global;
30
30
* @param {String } [options.testURL] URL to set as window.location.href
31
31
* @param {String } [options.waitForSelector='#main'] Element to wait for before returning promsie
32
32
* @param {String } [options._debug] initiate debugger after site is created
33
- * @param {Boolean|Object } [options._logHTML] Logs HTML to console after initialization
33
+ * @param {Boolean|Object|String } [options._logHTML] Logs HTML to console after initialization. Accepts CSS selector.
34
34
* @param {Boolean } [options._logHTML.format=true] Formats HTML output
35
+ * @param {String } [options._logHTML.selector='html'] CSS selector(s) to match and log HTML for
35
36
* @returns {Promise }
36
37
*/
37
38
async function docsifyInit ( options = { } ) {
@@ -311,16 +312,42 @@ async function docsifyInit(options = {}) {
311
312
312
313
// Log HTML to console
313
314
if ( settings . _logHTML ) {
314
- const html = isJSDOM
315
- ? document . documentElement . innerHTML
316
- : await page . content ( ) ;
317
- const output =
318
- settings . _logHTML . format === false
319
- ? html
320
- : prettier . format ( html , { parser : 'html' } ) ;
321
-
322
- // eslint-disable-next-line no-console
323
- console . log ( output ) ;
315
+ const selector =
316
+ typeof settings . _logHTML === 'string'
317
+ ? settings . _logHTML
318
+ : settings . _logHTML . selector ;
319
+
320
+ let htmlArr = [ ] ;
321
+
322
+ if ( selector ) {
323
+ if ( isJSDOM ) {
324
+ htmlArr = [ ...document . querySelectorAll ( selector ) ] . map (
325
+ elm => elm . outerHTML
326
+ ) ;
327
+ } else {
328
+ htmlArr = await page . $$eval ( selector , elms =>
329
+ elms . map ( e => e . outerHTML )
330
+ ) ;
331
+ }
332
+ } else {
333
+ htmlArr = [
334
+ isJSDOM ? document . documentElement . outerHTML : await page . content ( ) ,
335
+ ] ;
336
+ }
337
+
338
+ if ( htmlArr . length ) {
339
+ htmlArr . forEach ( html => {
340
+ if ( settings . _logHTML . format !== false ) {
341
+ html = prettier . format ( html , { parser : 'html' } ) ;
342
+ }
343
+
344
+ // eslint-disable-next-line no-console
345
+ console . log ( html ) ;
346
+ } ) ;
347
+ } else {
348
+ // eslint-disable-next-line no-console
349
+ console . warn ( `docsify-init(): unable to match selector '${ selector } '` ) ;
350
+ }
324
351
}
325
352
326
353
// Debug
0 commit comments