forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popular-pages.js
40 lines (38 loc) · 1.7 KB
/
popular-pages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import fs from 'fs/promises'
const POPULAR_PAGES_JSON = './lib/search/popular-pages.json'
export default async function getPopularPages(redirects) {
const popularPages = {}
try {
const popularPagesRaw = await fs.readFile(POPULAR_PAGES_JSON, 'utf-8')
let biggestCount = 0
for (const line of popularPagesRaw.split('\n')) {
if (!line.trim()) continue
const { path_article: path, path_count: count } = JSON.parse(line)
// The root page or any other potentially dirty record that is empty.
if (!path) continue
// This is safe because the `POPULAR_PAGES_JSON` always lists the
// most popular first.
if (!biggestCount) biggestCount = count
// Don't bother writing massively long floating point numbers
// because reducing it makes the JSON records smaller and we don't
// need any more precision than 7 significant figures.
const ratio = Number((count / biggestCount).toFixed(7))
// The reason we're heeding redirects is because it's very possible
// that the `POPULAR_PAGES_JSON` file is older/"staler" than the
// content itself.
// Imaging our analytics recorded that `/en/foo` had 1,234 pageviews,
// and someone goes and... `git mv content/foo content/bar` plus
// adding `redirect_from: - /foo` into the front-matter.
// Then, by using the redirects first, we can maintain that popularity
// by now "pretending" that it's `/en/bar` that has 1,234 pageviews.
popularPages[redirects[path] || path] = ratio
}
} catch (error) {
if (error.code === 'ENOENT') {
console.warn(`The file ${POPULAR_PAGES_JSON} can not be found.`)
} else {
throw error
}
}
return popularPages
}