This repository was archived by the owner on Sep 9, 2022. It is now read-only.
forked from github/docs
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (48 loc) · 1.95 KB
/
Copy pathindex.js
File metadata and controls
55 lines (48 loc) · 1.95 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { fileURLToPath } from 'url'
import path from 'path'
import fs from 'fs'
import walk from 'walk-sync'
import { set } from 'lodash-es'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const staticDir = path.join(__dirname, 'static')
export default function getWebhookPayloads() {
// Compile contents of individual .payload.json files into a single
// object, with versions as top-level keys.
const payloads = {}
// array of versions based on subdirectory names: lib/webhooks/static/<version>
const versions = fs.readdirSync(staticDir)
versions.forEach((version) => {
const payloadsPerVersion = {}
const versionSubdir = path.join(staticDir, version)
walk(versionSubdir, { includeBasePath: true }).forEach((payloadFile) => {
// payload file: /path/to/check_run.completed.payload.json
// payload path: check_run.completed
let payloadPath = path.basename(payloadFile).replace('.payload.json', '')
if (!payloadPath.includes('.') && payloadsPerVersion[payloadPath]) {
// append the key `default` to the payload path to
// prevent overwriting existing object
payloadPath = `${payloadPath}.default`
}
set(
payloadsPerVersion,
payloadPath,
formatAsJsonCodeBlock(JSON.parse(fs.readFileSync(payloadFile)))
)
})
payloads[version] = payloadsPerVersion
})
return payloads
}
function formatAsJsonCodeBlock(payloadObj) {
// Note the use of `data-highlight="json"`. This is important because
// done like this, it tells the rehype processor to NOT bother syntax
// highlight the code snippets on the server. And instead on the client,
// it uses `document.querySelectorAll('[data-highlight]')` to
// A) wake up the client-side highlight.js and, B) know what the language
// should be.
return (
'<div class="height-constrained-code-block" data-highlight="json">\n\n```\n' +
JSON.stringify(payloadObj, null, 2) +
'\n```\n\n</div>'
)
}