Skip to content

Commit 3af1cfa

Browse files
committed
Add selector option to _logHTML
1 parent a9be6f2 commit 3af1cfa

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

test/helpers/docsify-init.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ const isPlaywright = 'page' in global;
3030
* @param {String} [options.testURL] URL to set as window.location.href
3131
* @param {String} [options.waitForSelector='#main'] Element to wait for before returning promsie
3232
* @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.
3434
* @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
3536
* @returns {Promise}
3637
*/
3738
async function docsifyInit(options = {}) {
@@ -311,16 +312,42 @@ async function docsifyInit(options = {}) {
311312

312313
// Log HTML to console
313314
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+
}
324351
}
325352

326353
// Debug

0 commit comments

Comments
 (0)