MCPcopy Create free account
hub / github.com/github/docs / getPopularPages

Function getPopularPages

script/search/popular-pages.js:5–40  ·  view source on GitHub ↗
(redirects)

Source from the content-addressed store, hash-verified

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

Callers 1

buildRecordsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected