Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyse with puppeteer #221

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ The following scripts can be executed manually or as part of a GitHub Action:
- a copy of the common [`styles`](../styles) folder
- a copy of the document-specific `*/images` folder, if this exists.
- [`npm run pdf`](build-pdf.mjs) runs the PDF conversion and writes the PDF document into the [`docs/*`](../docs) folder.
- [`npm run select <CSS selector> [<XPath expression>]`](selector.mjs) selects parts of the generated HTML documents by executing a CSS selector and optionally an XPath expression relative to each match. For example, syntax errors in JSON code snippets can be detected with
```sh
npm run select ".json .er" "self::*[.!='…']/text()"
```
Comment on lines +99 to +102
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not have a postbuild script that calls this validation?

Copy link
Contributor Author

@HeikoTheissen HeikoTheissen Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps later if we find a better XPath expression. For now the result still contains too many false positives.

- [`npm test`](../test) runs a test suite.

## A note on diagrams
Expand Down
51 changes: 51 additions & 0 deletions lib/selector.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import puppeteer from "puppeteer";
import iterator from "./iterator.js";

var docs = [];
iterator(function (srcname, name, variant) {
docs.push(name);
});
var browser = await puppeteer.launch({ headless: "new" });
for (var name of docs) {
var heading = undefined;
console.group(name);
var page = await browser.newPage();
await page.goto(`${import.meta.dirname}/../docs/${name}/${name}.html`, {
waitUntil: "networkidle2",
});
for (var r of await Promise.all(
(
await Promise.all(
(await page.$$(process.argv[2])).map(async function (elem) {
var elems = process.argv[3]
? await elem.$$("xpath/" + process.argv[3])
: [elem];
return elems.map((elem) =>
elem.evaluate(function (e) {
return {
heading: document.evaluate(
"preceding::*[self::h1|self::h2|self::h3|self::h4|self::h5|self::h6][1]",
e,
() => {},
XPathResult.FIRST_ORDERED_NODE_TYPE,
).singleNodeValue.textContent,
match:
e.nodeType === Node.ELEMENT_NODE ? e.outerHTML : e.nodeValue,
};
}),
);
}),
)
).flat(),
)) {
if (r.heading !== heading) {
if (heading) console.groupEnd();
console.group(r.heading);
heading = r.heading;
}
console.log(r.match);
}
if (heading) console.groupEnd();
console.groupEnd();
}
process.exit(0);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "node lib/build.js",
"pdf": "node lib/build-pdf.js",
"start": "node lib/server",
"select": "node lib/selector.mjs",
"test": "c8 -r html -r text mocha",
"clean-xxx": "node lib/clean.mjs odata-xxx/temp odata-xxx-v4.0"
},
Expand Down