Simplify documentation linter
This patch simplifies documentation linter, basically addressing the comments from #47. References #14.
This commit is contained in:
parent
34f043d821
commit
a69bed30d9
@ -3,29 +3,39 @@ const commonmark = require('commonmark');
|
|||||||
const Browser = require('../../lib/Browser');
|
const Browser = require('../../lib/Browser');
|
||||||
|
|
||||||
class MDOutline {
|
class MDOutline {
|
||||||
constructor(text) {
|
/**
|
||||||
this.classes = [];
|
* @return {!MDOutline}
|
||||||
this.html = MDOutline.compile(text);
|
*/
|
||||||
}
|
static async create(text) {
|
||||||
|
// Render markdown as HTML.
|
||||||
static compile(text) {
|
|
||||||
const reader = new commonmark.Parser();
|
const reader = new commonmark.Parser();
|
||||||
const parsed = reader.parse(text);
|
const parsed = reader.parse(text);
|
||||||
const writer = new commonmark.HtmlRenderer();
|
const writer = new commonmark.HtmlRenderer();
|
||||||
const html = writer.render(parsed);
|
const html = writer.render(parsed);
|
||||||
return html;
|
|
||||||
}
|
|
||||||
|
|
||||||
async collectHeadings() {
|
// Extract headings.
|
||||||
const browser = new Browser({args: ['--no-sandbox']});
|
const browser = new Browser({args: ['--no-sandbox']});
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
await page.setContent(this.html);
|
await page.setContent(html);
|
||||||
this.headings = await page.evaluate(getTOCHeadings);
|
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();
|
await browser.close();
|
||||||
|
return new MDOutline(headings);
|
||||||
}
|
}
|
||||||
|
|
||||||
buildClasses() {
|
constructor(headings) {
|
||||||
const headings = this.headings;
|
this.classes = [];
|
||||||
const classHeading = /^class: (\w+)$/;
|
const classHeading = /^class: (\w+)$/;
|
||||||
const constructorRegex = /^new (\w+)\((.*)\)$/;
|
const constructorRegex = /^new (\w+)\((.*)\)$/;
|
||||||
const methodRegex = /^(\w+)\.(\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;
|
module.exports = MDOutline;
|
||||||
|
@ -9,11 +9,12 @@ const PROJECT_DIR = path.join(__dirname, '..', '..');
|
|||||||
const apiMdText = fs.readFileSync(path.join(PROJECT_DIR, 'docs', 'api.md'), 'utf8');
|
const apiMdText = fs.readFileSync(path.join(PROJECT_DIR, 'docs', 'api.md'), 'utf8');
|
||||||
|
|
||||||
let EXCLUDE_CLASSES = new Set([
|
let EXCLUDE_CLASSES = new Set([
|
||||||
'Helper',
|
'Connection',
|
||||||
'FrameManager',
|
'FrameManager',
|
||||||
|
'Helper',
|
||||||
'Navigator',
|
'Navigator',
|
||||||
'NetworkManager',
|
'NetworkManager',
|
||||||
'Connection'
|
'ProxyStream'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let EXCLUDE_METHODS = new Set([
|
let EXCLUDE_METHODS = new Set([
|
||||||
@ -55,16 +56,15 @@ let mdClassesArray;
|
|||||||
|
|
||||||
beforeAll(SX(async function() {
|
beforeAll(SX(async function() {
|
||||||
// Build up documentation from MD sources.
|
// Build up documentation from MD sources.
|
||||||
let mdOutline = new MDOutline(apiMdText);
|
let mdOutline = await MDOutline.create(apiMdText);
|
||||||
await mdOutline.collectHeadings();
|
|
||||||
mdOutline.buildClasses();
|
|
||||||
mdClassesArray = mdOutline.classes;
|
mdClassesArray = mdOutline.classes;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe('table of contents', function() {
|
describe('table of contents', function() {
|
||||||
it('should match markdown-toc\'s output', () => {
|
it('should match markdown-toc\'s output', () => {
|
||||||
const newApiMdText = markdownToc.insert(apiMdText);
|
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`');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user