Skip to content

Commit e55f2d5

Browse files
committed
optimize component-stats
remove JSDOM and instead use the documentsConverted event, before the HTML is added to its frame. Use the lightweight `striptags` module.
1 parent 21550ef commit e55f2d5

3 files changed

Lines changed: 33 additions & 571 deletions

File tree

lib/component-stats.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
const {JSDOM} = require('jsdom')
1+
const striptags = require('striptags');
2+
3+
const output = []
24

35
module.exports.register = function () {
4-
this.once('sitePublished', ({ playbook, contentCatalog }) => {
6+
this.once('documentsConverted', ({ playbook, contentCatalog }) => {
57

68
console.time('get-component-stats')
79

810
contentCatalog.getComponents().forEach((component) => {
911
component.versions.forEach((componentVersion, index) => {
1012
const pages = contentCatalog.getPages(it => it.src.component === componentVersion.name && it.src.version === componentVersion.version)
1113

12-
const wc = pages.map(page => {
13-
const dom = new JSDOM(page.contents.toString())
14-
const node = dom.window.document.querySelector('article')
15-
if (node) {
16-
return wordcount(node.textContent)
17-
}
18-
else { return 0 }
19-
})
20-
console.log({
14+
const wc = pages.map(page => wordcount(striptags(page.contents.toString())))
15+
// const wc = pages.map(page => {
16+
// const dom = new JSDOM(page.contents.toString())
17+
// const node = dom.window.document.querySelector('article')
18+
// if (node) {
19+
// return wordcount(node.textContent)
20+
// }
21+
// else { return 0 }
22+
// })
23+
24+
output.push({
2125
component: componentVersion.name,
2226
version: componentVersion.version,
2327
pages: pages.length,
@@ -27,6 +31,14 @@ module.exports.register = function () {
2731
})
2832

2933
console.timeEnd('get-component-stats')
34+
35+
})
36+
37+
this.once('sitePublished', ({}) => {
38+
console.log(output)
39+
40+
const total_words = sum(output.map(c => c.words))
41+
console.log(`Total words: ${total_words}`)
3042
})
3143
}
3244

0 commit comments

Comments
 (0)