This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
html-to-json.js
95 lines (85 loc) · 2.64 KB
/
html-to-json.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
module.exports = htmlToJson
const cheerio = require('cheerio')
const { URL } = require('url')
const addCodeSamples = require('./add-code-samples')
const addTriggersNotificationFlag = require('./add-triggers-notification-flag')
const applyDeprecations = require('./overrides/deprecations')
const elementToBlock = require('./element-to-block')
const findAcceptHeader = require('./find-accept-header')
const findDescription = require('./find-description')
const findIsGitHubCloud = require('./find-is-github-cloud')
const findParameters = require('./find-parameters')
const findResponse = require('./find-response')
const findRoutes = require('./find-routes')
const findDeprecationNotices = require('./find-deprecation-notices')
const htmlContainsEndpoint = require('./html-contains-endpoint')
const workarounds = require('./overrides/workarounds')
async function htmlToJson (html, { baseUrl, gheVersion, documentationUrl }) {
if (!htmlContainsEndpoint(html)) {
return
}
const $ = cheerio.load(html)
const [pageUrl, pageFragment] = documentationUrl.split('#')
const pageOrigin = pageUrl.replace(new URL(pageUrl).pathname, '')
// TODO: kebab-case scope
const scope = documentationUrl.match(/\/v3\/([^/#]+)/).pop()
const state = {
baseUrl,
gheVersion,
pageOrigin,
pageUrl,
pageFragment,
scope,
routes: [{}],
blocks: $('body > *')
.map((i, el) => {
const block = elementToBlock(el)
if (!block) {
throw new Error('Could not identify block type for:\n' + cheerio.html(el))
}
return block
})
.get()
}
// first block is always the title block, which
// also includes the "enabledForApps" flag
const { text: summary, enabledForApps } = state.blocks[0]
Object.assign(state.routes[0], {
method: null,
path: null,
operation: {
summary,
description: null,
operationId: null,
tags: [ scope ],
externalDocs: {
description: 'API method documentation',
url: documentationUrl
},
parameters: [],
responses: {},
'x-code-samples': [],
'x-github': {
legacy: false,
enabledForApps,
githubCloudOnly: false
},
'x-changes': []
}
})
// remove title block so we can keep track of blocks we haven’t parsed yet
state.blocks.splice(0, 1)
// mutate results
findIsGitHubCloud(state)
findRoutes(state)
findParameters(state)
findAcceptHeader(state)
findDescription(state)
findResponse(state)
findDeprecationNotices(state)
addTriggersNotificationFlag(state)
addCodeSamples(state)
workarounds(state)
applyDeprecations(state)
return state.routes
}