diff --git a/test/doclint/MDOutline.js b/test/doclint/MDOutline.js index 3fba6353..1f37016f 100644 --- a/test/doclint/MDOutline.js +++ b/test/doclint/MDOutline.js @@ -3,29 +3,39 @@ const commonmark = require('commonmark'); const Browser = require('../../lib/Browser'); class MDOutline { - constructor(text) { - this.classes = []; - this.html = MDOutline.compile(text); - } - - static compile(text) { + /** + * @return {!MDOutline} + */ + static async create(text) { + // Render markdown as HTML. const reader = new commonmark.Parser(); const parsed = reader.parse(text); const writer = new commonmark.HtmlRenderer(); const html = writer.render(parsed); - return html; - } - async collectHeadings() { + // Extract headings. const browser = new Browser({args: ['--no-sandbox']}); const page = await browser.newPage(); - await page.setContent(this.html); - this.headings = await page.evaluate(getTOCHeadings); + await page.setContent(html); + const headings = await page.evaluate(() => { + let headings = {}; + let methods = []; + for (let header of document.body.querySelectorAll('h3,h4')) { + if (header.matches('h3')) { + methods = []; + headings[header.textContent] = methods; + } else { + methods.push(header.textContent); + } + } + return headings; + }); await browser.close(); + return new MDOutline(headings); } - buildClasses() { - const headings = this.headings; + constructor(headings) { + this.classes = []; const classHeading = /^class: (\w+)$/; const constructorRegex = /^new (\w+)\((.*)\)$/; const methodRegex = /^(\w+)\.(\w+)\((.*)\)$/; @@ -68,18 +78,4 @@ class MDOutline { } } -function getTOCHeadings(){ - const headings = {}; - document.querySelectorAll('h3').forEach((domainEl, i) => { - const methods = []; - let currElem = domainEl; - while ((currElem = currElem.nextElementSibling) && !currElem.matches('h3')) { - if (currElem.matches('h4')) - methods.push(currElem.textContent); - } - headings[domainEl.textContent] = methods; - }); - return headings; -} - module.exports = MDOutline; diff --git a/test/doclint/lint.js b/test/doclint/lint.js index af087e2e..78a475b9 100644 --- a/test/doclint/lint.js +++ b/test/doclint/lint.js @@ -9,11 +9,12 @@ const PROJECT_DIR = path.join(__dirname, '..', '..'); const apiMdText = fs.readFileSync(path.join(PROJECT_DIR, 'docs', 'api.md'), 'utf8'); let EXCLUDE_CLASSES = new Set([ - 'Helper', + 'Connection', 'FrameManager', + 'Helper', 'Navigator', 'NetworkManager', - 'Connection' + 'ProxyStream' ]); let EXCLUDE_METHODS = new Set([ @@ -55,16 +56,15 @@ let mdClassesArray; beforeAll(SX(async function() { // Build up documentation from MD sources. - let mdOutline = new MDOutline(apiMdText); - await mdOutline.collectHeadings(); - mdOutline.buildClasses(); + let mdOutline = await MDOutline.create(apiMdText); mdClassesArray = mdOutline.classes; })); describe('table of contents', function() { it('should match markdown-toc\'s output', () => { const newApiMdText = markdownToc.insert(apiMdText); - expect(apiMdText === newApiMdText).toBe(true, 'markdown TOC is outdated, run `yarn generate-toc`'); + if (apiMdText !== newApiMdText) + fail('markdown TOC is outdated, run `yarn generate-toc`'); }); });