forked from ChromeDevTools/debugger-protocol-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-search-index.js
143 lines (129 loc) · 4.62 KB
/
create-search-index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
/**
* Utility command that creates the search index for pages/_data/?.json.
*/
const fs = require('fs');
const VERSIONS_FILE = 'pages/_data/versions.json';
const versionsText = fs.readFileSync(VERSIONS_FILE);
const versions = JSON.parse(versionsText);
versions.forEach(generateSearchIndex);
function generateSearchIndex(version) {
const protocolText = fs.readFileSync(`pages/_data/${version.slug}.json`);
const protocol = JSON.parse(protocolText);
// Set up Keyword bank
// Split up search keywords into primary and secondary matches.
// Primary kewords - event, domain, command names; plus type ids.
// Secondary keywords - command parameter names, event parameter names, type
// properties.
// Reasoning: Primary keyword matches will appear at the top of the search
// result list, while secondary matches, which are likely to have duplicates,
// will appear at the bottom.
var PageRefType = {
DOMAIN: '0',
EVENT: '1',
PARAM: '2',
TYPE_ID: '3',
COMMAND: '4'
};
// Optional root path to be prepended to page reference URLs.
var SITE_ROOT = '';
var MAX_DESCRIPTION_LENGTH = 200;
function getShortDescription(description) {
return description && description.length > MAX_DESCRIPTION_LENGTH ?
description.substr(0, MAX_DESCRIPTION_LENGTH) + '...' : description;
}
// Represents a page reference.
var PageReference = {
init: function(domain, type, description) {
this.domain = domain;
this.type = type;
this.description = description
},
createPageReference: function(title, type, description) {
var ref = Object.create(PageReference);
ref.init(title, type, getShortDescription(description));
return ref;
},
setHrefs: function(href, domainHref) {
this.domainHref = domainHref;
if (href) {
this.href = href;
}
}
};
// Represents a keyword match, which may have many page references.
var KeyRecord = {
init: function(keyword) {
this.keyword = keyword;
this.pageReferences = [];
},
addReference: function(pageRef) {
this.pageReferences.push(pageRef);
},
createKeyRecord: function(keyword, opt_pageRef) {
var keyRecord = Object.create(KeyRecord);
keyRecord.init(keyword);
if (opt_pageRef) {
keyRecord.addReference(opt_pageRef);
}
return keyRecord;
}
};
// Used to store our key records.
var keywordMap = {
// Lazily creates a KeyRecord.
addReferenceForKey: function(keyword, pageRef) {
const key = keyword.toLowerCase();
var record = this[key];
if (record) {
record.addReference(pageRef);
} else {
this[key] = KeyRecord.createKeyRecord(keyword, pageRef);
}
}
};
(protocol.domains).forEach(function (domain, idx) {
var domainName = domain.domain;
var domainPath = SITE_ROOT + version.slug + '/' + domainName + '/';
// Reminder: You may have multiple pages per keyword.
// Store domain name as a page reference under itself as a keyword.
var ref = PageReference.createPageReference(
domainName, PageRefType.DOMAIN, domain.description);
ref.setHrefs('', domainPath);
keywordMap.addReferenceForKey(domainName, ref);
if (domain.commands) {
domain.commands.forEach(function(command) {
var commandName = command.name;
var commandNameHref = '#method-' + commandName;
var ref = PageReference.createPageReference(
domainName, PageRefType.COMMAND, command.description);
ref.setHrefs(commandNameHref, domainPath);
keywordMap.addReferenceForKey(`${domainName}.${commandName}`, ref);
});
}
if (domain.events) {
domain.events.forEach(function(event) {
var eventName = event.name;
var eventNameHref = '#event-' + eventName;
var ref = PageReference.createPageReference(
domainName, PageRefType.EVENT, event.description);
ref.setHrefs(eventNameHref, domainPath);
keywordMap.addReferenceForKey(`${domainName}.${eventName}`, ref);
});
}
if (domain.types) {
domain.types.forEach(function(type) {
var typeName = type.id;
var typeNameHref = '#type-' + typeName;
var ref = PageReference.createPageReference(
domainName, PageRefType.TYPE_ID, type.description);
ref.setHrefs(typeNameHref, domainPath);
keywordMap.addReferenceForKey(`${domainName}.${typeName}`, ref);
});
}
// TODO(ericguzman): Index other keyword types.
});
const fileName = `search_index/${version.slug}.json`;
const content = JSON.stringify(keywordMap);
fs.writeFileSync(fileName, content);
}