tag with text
[...preNode.querySelectorAll("a")].forEach((e) => {
const textContent = e.textContent;
textContent && e.replaceWith(textContent);
});
[...preNode.querySelectorAll("span")].forEach((span) => {
const spanText = span.textContent;
span.replaceWith(spanText);
});
node.innerHTML = `${preNode.outerHTML}`;
});
[...doc.querySelectorAll("a")].forEach((node) => {
!node.textContent.split("\n").join("") && node?.remove();
});
const title = doc.querySelector(".header .title");
title &&
doc
.querySelector(".header")
?.replaceWith(`${title.textContent}
`);
const packageHierarchyLabel = doc.querySelector(
".header .packageHierarchyLabel"
);
// remove packageHierarchyLabel and it's description if exists
if (packageHierarchyLabel) {
doc.querySelector(".header span")?.remove();
doc.querySelector(".header ul")?.remove();
packageHierarchyLabel.remove();
}
// only need article body html
doc = doc.querySelector("body > main").innerHTML;
return { docHTML: doc };
};
handleApiFiles(doc2html, {
from,
version,
category: "milvus-sdk-java",
});
};
/**
* Handle html pages under `${parentPath}/${version}` and fromat them.
* Node docs are generated by typedoc.
* @param {string} parentPath api category dir path, such as "src/pages/APIReference/milvus-sdk-node"
* @param {string} version api version, such as "v1.0.1"
* @param {array} apiFiles pages under `${parentPath}/${version}` will be formatted and pushed to apiFiles
*/
const handleNodeFiles = (from, version) => {
const doc2html = (doc) => {
// Format content for highlight.js
[...doc.querySelectorAll("pre")].forEach((node) => {
const preNode = HTMLParser.parse(node.innerHTML);
[...preNode.querySelectorAll("a")].forEach((e) => {
const textContent = e.textContent;
textContent && e.replaceWith(textContent);
});
[...preNode.querySelectorAll("span")].forEach((span) => {
const spanText = span.textContent;
span.replaceWith(spanText);
});
node.innerHTML = preNode.outerHTML;
});
// Remove hierarchy
doc.querySelector(".tsd-hierarchy")?.remove();
// Remove useless sections
const sections = doc.querySelectorAll(".col-content > section");
[...sections].forEach((node, index) => {
const isIndex = node?.getAttribute("class")?.includes("tsd-index");
const isLastChild = sections?.length === index + 1;
const isValid = isIndex || isLastChild;
if (!isValid) node?.remove();
if (isIndex) {
// Remove toc title "index"
node.querySelector("h2")?.remove();
}
if (isLastChild) {
// Remove source code link and desc elements
const lastChild = HTMLParser.parse(node.innerHTML);
[...lastChild.querySelectorAll("ul.tsd-signatures")].forEach((i) =>
i.remove()
);
[...lastChild.querySelectorAll("aside.tsd-sources")].forEach((i) =>
i.remove()
);
node.innerHTML = lastChild.outerHTML;
}
});
[...doc.querySelectorAll(".tsd-index-section")].forEach((node) => {
const sectionNode = HTMLParser.parse(node.innerHTML);
const title = sectionNode.querySelector("h3");
if (title?.textContent === "Methods") {
title?.remove();
[...sectionNode.querySelectorAll("a")].forEach((aNode) => {
const ahref = aNode?.getAttribute("href")?.split("#")?.pop();
const newHref = `#${ahref}`;
aNode.setAttribute("href", newHref);
});
} else {
node.remove();
}
node.innerHTML = sectionNode.outerHTML;
});
// only need article title & body html
doc.querySelector(".tsd-panel.tsd-typography p")?.remove();
const isDupTitle = doc.querySelectorAll("h1")?.length > 1;
const docTitleHTML = isDupTitle ? "" : doc.querySelector("h1").outerHTML;
doc =
docTitleHTML +
doc.querySelector(".container-main .col-content").innerHTML;
return { docHTML: doc };
};
handleApiFiles(doc2html, {
from,
version,
category: "milvus-sdk-node",
});
};
// handlePyFiles only support docs v1.x
// /**
// * handle html pages under `${parentPath}/${version}` and fromat them
// * @param {string} parentPath api category dir path, such as "src/pages/APIReference/pymilvus"
// * @param {string} version api version, such as "v1.0.1"
// * @param {array} apiFiles pages under `${parentPath}/${version}` will be formatted and pushed to apiFiles
// */
// const handlePyFiles = (parentPath, version, apiFiles) => {
// /**
// * Get the TOC item index as menu item order.
// * @param {array} toctree TOC tree.
// * @returns {number} Current TOC item index, default is the length.
// */
// const calculateOrder = toctree => {
// for (let i = 0; i < toctree.length; i++) {
// const isCurrent = toctree[i]?.getAttribute("class")?.includes("current");
// if (isCurrent) return i;
// }
// return toctree.length;
// };
// const doc2html = doc => {
// // remove useless link
// [...doc.querySelectorAll(".reference.internal .viewcode-link")].forEach(
// node => {
// node.parentNode.parentNode.removeChild(node.parentNode);
// }
// );
// const leftNav = doc.querySelectorAll(".toctree-l1");
// const title = doc?.querySelector("h1")?.textContent?.slice(0, -1);
// const order = calculateOrder([...leftNav]);
// // Add for content.
// [...doc.querySelectorAll("pre")].forEach(node => {
// const preNode = HTMLParser.parse(node.innerHTML);
// // Remove all unescaped HTML, here's , which cause highlight warning.
// // https://github.com/highlightjs/highlight.js/issues/2886
// [...preNode.querySelectorAll("span")].forEach(span => {
// const spanText = span.textContent;
// span.replaceWith(spanText);
// });
// node.innerHTML = `${preNode.outerHTML}`;
// });
// // only need article body html
// doc = doc.querySelector("[itemprop=articleBody] > div").innerHTML;
// return { docHTML: doc, title, order };
// };
// handleApiFiles(doc2html, apiFiles, {
// parentPath,
// version,
// category: "pymilvus",
// });
// };
const args = process.argv.slice(2);
const main = (args) => {
// console.log(args);
if (args.length !== 3)
throw new Error("args should be category, version, srcPath");
const [category, version, from] = args;
const dirs = [
"pymilvus",
"milvus-sdk-go",
"milvus-sdk-java",
"milvus-sdk-node",
];
switch (category) {
case "pymilvus":
handlePyFilesWithOrm(from, version);
break;
case "milvus-sdk-go":
handleGoFiles(from, version);
break;
case "milvus-sdk-java":
handleJavaFiles(from, version);
break;
case "milvus-sdk-node":
handleNodeFiles(from, version);
break;
default:
throw new Error(`Category should only be one of ${dirs}`);
}
};
main(args);