Best JavaScript code snippet using puppeteer
MDBuilder.js
Source:MDBuilder.js
...36 const classes = [];37 const errors = [];38 const headers = document.body.querySelectorAll('h3');39 for (let i = 0; i < headers.length; i++) {40 const fragment = extractSiblingsIntoFragment(41 headers[i],42 headers[i + 1]43 );44 classes.push(parseClass(fragment));45 }46 return { classes, errors };47 /**48 * @param {HTMLLIElement} element49 */50 function parseProperty(element) {51 const clone = element.cloneNode(true);52 const ul = clone.querySelector(':scope > ul');53 const str = parseComment(54 extractSiblingsIntoFragment(clone.firstChild, ul)55 );56 const name = str57 .substring(0, str.indexOf('<'))58 .replace(/\`/g, '')59 .trim();60 const type = findType(str);61 const properties = [];62 const comment = str63 .substring(str.indexOf('<') + type.length + 2)64 .trim();65 // Strings have enum values instead of properties66 if (!type.includes('string')) {67 for (const childElement of element.querySelectorAll(68 ':scope > ul > li'69 )) {70 const property = parseProperty(childElement);71 property.required = property.comment.includes('***required***');72 properties.push(property);73 }74 }75 return {76 name,77 type,78 comment,79 properties,80 };81 }82 /**83 * @param {string} str84 * @returns {string}85 */86 function findType(str) {87 const start = str.indexOf('<') + 1;88 let count = 1;89 for (let i = start; i < str.length; i++) {90 if (str[i] === '<') count++;91 if (str[i] === '>') count--;92 if (!count) return str.substring(start, i);93 }94 return 'unknown';95 }96 /**97 * @param {DocumentFragment} content98 */99 function parseClass(content) {100 const members = [];101 const headers = content.querySelectorAll('h4');102 const name = content.firstChild.textContent;103 let extendsName = null;104 let commentStart = content.firstChild.nextSibling;105 const extendsElement = content.querySelector('ul');106 if (107 extendsElement &&108 extendsElement.textContent.trim().startsWith('extends:')109 ) {110 commentStart = extendsElement.nextSibling;111 extendsName = extendsElement.querySelector('a').textContent;112 }113 const comment = parseComment(114 extractSiblingsIntoFragment(commentStart, headers[0])115 );116 for (let i = 0; i < headers.length; i++) {117 const fragment = extractSiblingsIntoFragment(118 headers[i],119 headers[i + 1]120 );121 members.push(parseMember(fragment));122 }123 return {124 name,125 comment,126 extendsName,127 members,128 };129 }130 /**131 * @param {Node} content132 */133 function parseComment(content) {134 for (const code of content.querySelectorAll('pre > code'))135 code.replaceWith(136 '```' +137 code.className.substring('language-'.length) +138 '\n' +139 code.textContent +140 '```'141 );142 for (const code of content.querySelectorAll('code'))143 code.replaceWith('`' + code.textContent + '`');144 for (const strong of content.querySelectorAll('strong'))145 strong.replaceWith('**' + parseComment(strong) + '**');146 return content.textContent.trim();147 }148 /**149 * @param {string} name150 * @param {DocumentFragment} content151 */152 function parseMember(content) {153 const name = content.firstChild.textContent;154 const args = [];155 let returnType = null;156 const paramRegex = /^\w+\.[\w$]+\((.*)\)$/;157 const matches = paramRegex.exec(name) || ['', ''];158 const parameters = matches[1];159 const optionalStartIndex = parameters.indexOf('[');160 const optinalParamsStr =161 optionalStartIndex !== -1162 ? parameters.substring(optionalStartIndex).replace(/[\[\]]/g, '')163 : '';164 const optionalparams = new Set(165 optinalParamsStr166 .split(',')167 .filter((x) => x)168 .map((x) => x.trim())169 );170 const ul = content.querySelector('ul');171 for (const element of content.querySelectorAll('h4 + ul > li')) {172 if (173 element.matches('li') &&174 element.textContent.trim().startsWith('<')175 ) {176 returnType = parseProperty(element);177 } else if (178 element.matches('li') &&179 element.firstChild.matches &&180 element.firstChild.matches('code')181 ) {182 const property = parseProperty(element);183 property.required = !optionalparams.has(property.name);184 args.push(property);185 } else if (186 element.matches('li') &&187 element.firstChild.nodeType === Element.TEXT_NODE &&188 element.firstChild.textContent.toLowerCase().startsWith('return')189 ) {190 returnType = parseProperty(element);191 const expectedText = 'returns: ';192 let actualText = element.firstChild.textContent;193 let angleIndex = actualText.indexOf('<');194 let spaceIndex = actualText.indexOf(' ');195 angleIndex = angleIndex === -1 ? actualText.length : angleIndex;196 spaceIndex = spaceIndex === -1 ? actualText.length : spaceIndex + 1;197 actualText = actualText.substring(198 0,199 Math.min(angleIndex, spaceIndex)200 );201 if (actualText !== expectedText)202 errors.push(203 `${name} has mistyped 'return' type declaration: expected exactly '${expectedText}', found '${actualText}'.`204 );205 }206 }207 const comment = parseComment(208 extractSiblingsIntoFragment(ul ? ul.nextSibling : content)209 );210 return {211 name,212 args,213 returnType,214 comment,215 };216 }217 /**218 * @param {!Node} fromInclusive219 * @param {!Node} toExclusive220 * @returns {!DocumentFragment}221 */222 function extractSiblingsIntoFragment(fromInclusive, toExclusive) {223 const fragment = document.createDocumentFragment();224 let node = fromInclusive;225 while (node && node !== toExclusive) {226 const next = node.nextSibling;227 fragment.appendChild(node);228 node = next;229 }230 return fragment;231 }232 });233 return new MDOutline(classes, errors);234 }235 constructor(classes, errors) {236 this.classes = [];...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!