Skip to content

Commit

Permalink
update generate function
Browse files Browse the repository at this point in the history
  • Loading branch information
thucpn committed Dec 9, 2024
1 parent 915b214 commit c469d47
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
34 changes: 18 additions & 16 deletions apps/next/scripts/generate-docs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,31 @@ void generateFiles({
function transformOutput(filePath, content) {
const fileName = path.basename(filePath);
let title = fileName.split(".")[0];
let pageContent = content;
if (title === "index") title = "LlamaIndex API Reference";

const transformedContent = transformAbsoluteUrl(filePath, content);
return `---\ntitle: ${title}\n---\n\n${transformedContent}`;
return `---\ntitle: ${title}\n---\n\n${transformAbsoluteUrl(pageContent, filePath)}`;
}

/**
* Transforms the content by converting relative MDX links to absolute docs API links
* Example: [text](../type-aliases/TaskHandler.mdx) -> [text](/docs/api/type-aliases/TaskHandler)
* [text](BaseChatEngine.mdx) -> [text](/docs/api/classes/BaseChatEngine)
* [text](BaseVectorStore.mdx#constructors) -> [text](/docs/api/classes/BaseVectorStore#constructors)
* [text](TaskStep.mdx) -> [text](/docs/api/type-aliases/TaskStep)
*/
function transformAbsoluteUrl(filePath, content) {
const currentFileDir = path.dirname(filePath);
return content.replace(/\(([^)]+)\.mdx([^)]*)\)/g, (match, slug, anchor) => {
const absolutePath = path.resolve(currentFileDir, `${slug}.mdx`);
const index = absolutePath.indexOf(["docs", "api"].join(path.sep));
const result = `(/${absolutePath
.slice(index)
.replace(".mdx", "")
.split(path.sep)
.join("/")
.replace(/\/index$/, "")}${anchor || ""})`;
return result;
});
function transformAbsoluteUrl(content, filePath) {
const group = path.dirname(filePath).split(path.sep).pop();
return content.replace(
/\]\(([^)]+)\.mdx([^)]*)\)/g,
(match, slug, anchor) => {
const slugParts = slug.replace("../", "").split("/");

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This replaces only the first occurrence of "../".
if (slugParts.length === 1 && group) slugParts.unshift(group);
const result = ["/docs/api", ...slugParts, anchor]
.filter(Boolean)
.join("/");
return `](${result})`;
},
);
}

// append meta.json for API page
Expand Down
3 changes: 2 additions & 1 deletion apps/next/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"fileExtension": ".mdx",
"hidePageTitle": true,
"hidePageHeader": true,
"hideGroupHeadings": true
"hideGroupHeadings": true,
"hideBreadcrumbs": true
}

0 comments on commit c469d47

Please sign in to comment.