Add selector option to _logHTML

This commit is contained in:
John Hildenbiddle 2020-10-26 02:08:51 -05:00
parent a9be6f20d2
commit 3af1cfa84e

View File

@ -30,8 +30,9 @@ const isPlaywright = 'page' in global;
* @param {String} [options.testURL] URL to set as window.location.href
* @param {String} [options.waitForSelector='#main'] Element to wait for before returning promsie
* @param {String} [options._debug] initiate debugger after site is created
* @param {Boolean|Object} [options._logHTML] Logs HTML to console after initialization
* @param {Boolean|Object|String} [options._logHTML] Logs HTML to console after initialization. Accepts CSS selector.
* @param {Boolean} [options._logHTML.format=true] Formats HTML output
* @param {String} [options._logHTML.selector='html'] CSS selector(s) to match and log HTML for
* @returns {Promise}
*/
async function docsifyInit(options = {}) {
@ -311,16 +312,42 @@ async function docsifyInit(options = {}) {
// Log HTML to console
if (settings._logHTML) {
const html = isJSDOM
? document.documentElement.innerHTML
: await page.content();
const output =
settings._logHTML.format === false
? html
: prettier.format(html, { parser: 'html' });
const selector =
typeof settings._logHTML === 'string'
? settings._logHTML
: settings._logHTML.selector;
// eslint-disable-next-line no-console
console.log(output);
let htmlArr = [];
if (selector) {
if (isJSDOM) {
htmlArr = [...document.querySelectorAll(selector)].map(
elm => elm.outerHTML
);
} else {
htmlArr = await page.$$eval(selector, elms =>
elms.map(e => e.outerHTML)
);
}
} else {
htmlArr = [
isJSDOM ? document.documentElement.outerHTML : await page.content(),
];
}
if (htmlArr.length) {
htmlArr.forEach(html => {
if (settings._logHTML.format !== false) {
html = prettier.format(html, { parser: 'html' });
}
// eslint-disable-next-line no-console
console.log(html);
});
} else {
// eslint-disable-next-line no-console
console.warn(`docsify-init(): unable to match selector '${selector}'`);
}
}
// Debug