generated from LizardByte/template-base
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoxyconfig-readthedocs-search.js
185 lines (160 loc) · 6.91 KB
/
doxyconfig-readthedocs-search.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
class ReadtheDocsSearch {
static searchResultsText=[
"Sorry, no pages matching your query.",
"Search finished, found <b>1</b> page matching the search query.",
"Search finished, found <b>$num</b> pages matching the search query.",
];
static get serverUrl() {
const serverUrlSuffix = '_/api/v3/';
const domainName = window.location.hostname;
console.log(`Domain name: ${domainName}`);
if (domainName === 'localhost') {
let tmpServerUrl = serverUrl;
while (tmpServerUrl.endsWith('/')) {
tmpServerUrl = tmpServerUrl.slice(0, -1);
}
console.warn('Localhost detected, you probably need to bypass CORS');
return `${tmpServerUrl}/${serverUrlSuffix}`;
}
return `https://${domainName}/${serverUrlSuffix}`;
}
static init() {
window.SearchBox = function(name, resultsPath, extension) {
// live search
// TODO
this.DOMSearchField = () => document.getElementById("MSearchField");
this.DOMSearchBox = () => document.getElementById("MSearchBox");
this.OnSearchFieldFocus = isActive => {
if (isActive) {
this.DOMSearchBox().className = 'MSearchBoxActive';
} else {
this.DOMSearchBox().className = 'MSearchBoxInactive';
}
}
}
window.searchFor = function(query, page, count) {
const results = $('#searchresults');
// Get the title
let pageTitle = $('div.title')
const originalTitle = pageTitle.text().toString();
let pageTitleStates = ["Searching", "Searching .", "Searching ..", "Searching ..."];
let pageTitleIndex = 0;
// Function to update the page title
function updatePageTitle() {
pageTitle.text(pageTitleStates[pageTitleIndex]);
pageTitleIndex = (pageTitleIndex + 1) % pageTitleStates.length;
}
// Start the interval to update the page title
let titleInterval = setInterval(updatePageTitle, 500);
// The summary will be displayed at the top of the search results
let resultSummary = document.createElement('p');
resultSummary.className = 'search-summary';
results.append(resultSummary);
// Put all results into an unordered list
let resultList = document.createElement('ul');
resultList.className = 'search';
results.append(resultList);
// readthedocs metadata
let projectSlug = ReadtheDocsSearch.getMetaValue("readthedocs-project-slug") || "doxyconfig";
let projectVersion = ReadtheDocsSearch.getMetaValue("readthedocs-version-slug") || "latest";
// pull requests are not indexed, so use the default version
if (/^\d+$/.test(projectVersion)) {
console.log('Pull request detected, getting default version from ReadTheDocs API');
ReadtheDocsSearch.getReadTheDocsDefaultVersion(projectSlug);
}
let url = `${ReadtheDocsSearch.serverUrl}search/?q=project:${projectSlug}/${projectVersion}+${query}&page=${page + 1}&page_size=${count}`;
console.log(url);
let firstUrl = true;
function fetchResults(url) {
$.ajax({
url: url,
dataType: 'json',
success: function (data) {
// Add the query to the search field
// This seems only be working if applied in the ajax success function...
// maybe the field is not available before this point
$('#MSearchField').val(query);
if (firstUrl) {
if (data.count > 0) {
if (data.count === 1) {
resultSummary.innerHTML = ReadtheDocsSearch.searchResultsText[1];
} else {
resultSummary.innerHTML = ReadtheDocsSearch.searchResultsText[2].replace(/\$num/, data.count);
}
} else {
resultSummary.innerHTML = ReadtheDocsSearch.searchResultsText[0];
}
}
$.each(data.results, function (i, item) {
let resultItem = document.createElement('li');
resultItem.className = 'search-result';
let resultItemUrl = `${item.domain}${item.path}`;
let resultItemTitle = item.title;
let resultItemType = item.type; // todo... we can display results differently based on type
let resultItemTitleLink = document.createElement('a');
let resultItemTitleHeading = document.createElement('h3');
resultItemTitleHeading.appendChild(resultItemTitleLink);
resultItemTitleLink.href = resultItemUrl;
resultItemTitleLink.textContent = resultItemTitle;
resultItem.append(resultItemTitleHeading);
resultList.append(resultItem);
let resultItemParagraph = document.createElement('p');
resultItemParagraph.className = 'context';
for (let i = 0; i < item.blocks.length; i++) {
let blockContent = item.blocks[i].highlights.content.join(', ');
// Find all <span> tags and ensure they are highlighted
blockContent = blockContent.replace(/<span>(.*?)<\/span>/g, '<span class="highlighted">$1</span>');
resultItemParagraph.innerHTML += blockContent;
let blockName = `#${item.blocks[i].title.toLowerCase().replace(' ', '-')}`;
let blockUrl = resultItemUrl + blockName;
let blockLink = document.createElement('a');
blockLink.href = blockUrl;
blockLink.textContent = "More...";
resultItemParagraph.append(document.createTextNode(' '));
resultItemParagraph.append(blockLink);
resultItemParagraph.append(document.createElement('br'));
}
resultItem.append(resultItemParagraph);
});
// Add pagination
firstUrl = false;
if (data.next) {
fetchResults(data.next);
} else {
// Clear the interval when the search is complete
clearInterval(titleInterval);
pageTitle.text(originalTitle);
}
}
});
}
fetchResults(url);
}
}
// Function to extract the value of a specified Read the Docs meta property
static getMetaValue(propertyName) {
const metaTags = document.getElementsByTagName('meta');
for (let meta of metaTags) {
if (meta.name === propertyName) {
return meta.content;
}
}
return null;
}
static getReadTheDocsDefaultVersion(project) {
let url = `${ReadtheDocsSearch.serverUrl}projects/${project}/`;
$.ajax({
url: url,
dataType: 'json',
success: function(data) {
console.log(data);
return data.default_version;
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Error:', textStatus, errorThrown);
console.log(`Cannot determine default version for ${project}, assuming "latest"`);
return "latest";
}
})
}
}