Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
thucpn committed Dec 16, 2024
1 parent d7ea779 commit 2be1ea7
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions apps/next/scripts/migrate-docs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,37 @@ import * as path from "node:path";
const out = "./src/content/docs/llamaindex";

void generateFiles({
input: ["../docs/docs/modules/**/*.md", "../docs/docs/modules/**/*.mdx"],
output: (file) =>
path.resolve(
path.join(out, path.dirname(file).replace("../docs/docs/", "")),
`${path.basename(file).split(".")[0]}.mdx` // rename .md to .mdx
),
transformOutput,
input: ["../docs/docs/modules/**/*.md", "../docs/docs/modules/**/*.mdx"],
output: (file) =>
path.resolve(
path.join(out, path.dirname(file).replace("../docs/docs/", "")),
`${path.basename(file).split(".")[0]}.mdx`, // rename .md to .mdx
),
transformOutput,
});

// Replace h1 title with frontmatter title, update all links
// Example: # LLM -> --- title: LLM ---
function transformOutput(filePath, content) {
const lines = content.split("\n");
const h1Index = lines.findIndex((line) => /^# /.test(line));
const title = lines[h1Index].replace("# ", "").trim();
const mdxLines = [`---`, `title: ${title}`, `---`, ...lines.slice(h1Index + 1)];
const mdxContent = mdxLines.join("\n");
const lines = content.split("\n");
const h1Index = lines.findIndex((line) => /^# /.test(line));
const title = lines[h1Index].replace("# ", "").trim();
const mdxLines = [
`---`,
`title: ${title}`,
`---`,
...lines.slice(h1Index + 1),
];
const mdxContent = mdxLines.join("\n");

// update all links, remove .md and replace ../../api (or ../api, ./api, ...) with /docs/api
// eg: [SentenceSplitter](../api/classes/SentenceSplitter.md) -> [SentenceSplitter](/docs/api/classes/SentenceSplitter)
const result = mdxContent.replace(
/\]\((\.{0,2}\/)*api\/([^)]+)\.md([^)]*)\)/g,
(match, prefix, path, anchor) => {
return `](/docs/api/${path}${anchor})`;
}
);
const result = mdxContent.replace(
/\]\((\.{0,2}\/)*api\/([^)]+)\.md([^)]*)\)/g,
(match, prefix, path, anchor) => {
return `](/docs/api/${path}${anchor})`;
},
);

return result;
return result;
}

0 comments on commit 2be1ea7

Please sign in to comment.