mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-18 14:46:39 +00:00

Currently, when we set URLs from JS, we set them only using the protocol and host locations. This works fine when docs are served from the base directory of the site, but if you want to nest it under another directory, our JS fails to set the correct path, leading to broken links. This patch adds a --base option to specify the path prefix to use, which is set in the generated index_json.js file. index.json can then fill in the prefix appropriately when generating links in a browser. This flag has no effect for non HTML output. Given an index hosted at: www.docs.com/base_directory/index.html we used to generate the following link: www.docs.com/file.html Using --base base_directory we now generate: www.docs.com/base_directory/file.html This allows such links to work when hosting pages without using a custom index.js.
64 lines
2.3 KiB
JavaScript
64 lines
2.3 KiB
JavaScript
function genLink(Ref) {
|
|
// we treat the file paths different depending on if we're
|
|
// serving via a http server or viewing from a local
|
|
var Path = window.location.protocol.startsWith("file")
|
|
? `${window.location.protocol}//${RootPath}/${Ref.Path}`
|
|
: `${window.location.protocol}//${window.location.host}/${
|
|
Base}/${Ref.Path}`;
|
|
if (Ref.RefType === "namespace") {
|
|
Path = `${Path}/index.html`
|
|
} else if (Ref.Path === "") {
|
|
Path = `${Path}${Ref.Name}.html`;
|
|
} else {
|
|
Path = `${Path}/${Ref.Name}.html`;
|
|
}
|
|
ANode = document.createElement("a");
|
|
ANode.setAttribute("href", Path);
|
|
var TextNode = document.createTextNode(Ref.Name);
|
|
ANode.appendChild(TextNode);
|
|
return ANode;
|
|
}
|
|
|
|
function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
|
|
// Out will store the HTML elements that Index requires to be generated
|
|
var Out = [];
|
|
if (Index.Name) {
|
|
var SpanNode = document.createElement("span");
|
|
var TextNode = document.createTextNode(Index.Name);
|
|
SpanNode.appendChild(genLink(Index, CurrentDirectory));
|
|
Out.push(SpanNode);
|
|
}
|
|
if (Index.Children.length == 0)
|
|
return Out;
|
|
// Only the outermost list should use ol, the others should use ul
|
|
var ListNodeName = IsOutermostList ? "ol" : "ul";
|
|
var ListNode = document.createElement(ListNodeName);
|
|
for (Child of Index.Children) {
|
|
var LiNode = document.createElement("li");
|
|
ChildNodes = genHTMLOfIndex(Child, CurrentDirectory, false);
|
|
for (Node of ChildNodes)
|
|
LiNode.appendChild(Node);
|
|
ListNode.appendChild(LiNode);
|
|
}
|
|
Out.push(ListNode);
|
|
return Out;
|
|
}
|
|
|
|
function createIndex(Index) {
|
|
// Get the DOM element where the index will be created
|
|
var IndexDiv = document.getElementById("sidebar-left");
|
|
// Get the relative path of this file
|
|
CurrentDirectory = IndexDiv.getAttribute("path");
|
|
var IndexNodes = genHTMLOfIndex(Index, CurrentDirectory, true);
|
|
for (Node of IndexNodes)
|
|
IndexDiv.appendChild(Node);
|
|
}
|
|
|
|
// Runs after DOM loads
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
// LoadIndex is an asynchronous function that will be generated clang-doc.
|
|
// It ensures that the function call will not block as soon the page loads,
|
|
// since the index object are often huge and can contain thousands of lines.
|
|
LoadIndex().then((Index) => { createIndex(Index); });
|
|
});
|