Skip to content

Commit

Permalink
Sort out runtime errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ptgott committed Jul 8, 2024
1 parent 4d88466 commit 888fcf6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
37 changes: 25 additions & 12 deletions server/remark-toc.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import * as nodeFS from "fs";
import * as path from "path";
import matter from "gray-matter";

// relativePathToFile takes a filepath and returns a path we can use in links
// to the file in a table of contents page. The link path is a relative path
// to the directory where we are placing the table of contents page.
// @param filepath {string} - the path from which to generate a link path.
const relativePathToFile = (root: string, filepath: string) => {
// Return the filepath without the first segment, removing the first
// slash. This is because the TOC file we are generating is located at
// root.
return filepath.slice(root.length).replace(/^\//, "");
};

export const resolveTOCIncludes = (dirPath: string, fs = nodeFS) => {
const parts = path.parse(dirPath);
const tocIntro = path.join(parts.dir, parts.name, parts.name + ".mdx");
if (!this.fs.existsSync(tocIntro)) {
if (!fs.existsSync(tocIntro)) {
throw `There must be a page called ${tocIntro} to introduce ${dirPath}".`;
}

const files = fs.readdirSync(dirPath, "utf8");

let mdxFiles = new Set();
const dirs = files.reduce((accum, current) => {
const stats = this.fs.statSync(path.join(dirPath, current));
const stats = fs.statSync(path.join(dirPath, current));
if (!stats.isDirectory()) {
mdxFiles.add(path.join(dirPath, current));
return accum;
Expand All @@ -24,7 +36,7 @@ export const resolveTOCIncludes = (dirPath: string, fs = nodeFS) => {
// Add rows to the menu page for non-menu pages.
let entries = [];
mdxFiles.forEach((f, idx) => {
const text = this.fs.readFileSync(f, "utf8");
const text = fs.readFileSync(f, "utf8");
const lines = text.split("\n");

// Skip TOC pages (with the same name as the parent directory) since we
Expand All @@ -34,27 +46,28 @@ export const resolveTOCIncludes = (dirPath: string, fs = nodeFS) => {
return;
}

let relPath = this.relativePathToFile(f);
const fm = this.getFrontmatter(text);
let relPath = relativePathToFile(dirPath, f);
const { data } = matter(text);

entries.push(`- [${fm.title}](${relPath}): ${fm.description}`);
entries.push(`- [${data.title}](${relPath}): ${data.description}`);
});
newText += entries.join("");

// Add rows to the menu page for first-level child menu pages
let menuEntries = [];
dirs.forEach((f, idx) => {
const menuPath = path.join(f, path.parse(f).base + ".mdx");
if (!this.fs.existsSync(menuPath)) {
if (!fs.existsSync(menuPath)) {
throw new Error(
`there must be a page called ${menuPath} that introduces ${f}`
);
}
const text = this.fs.readFileSync(menuPath, "utf8");
let relPath = this.relativePathToFile(menuPath);
const fm = this.getFrontmatter(text);
const text = fs.readFileSync(menuPath, "utf8");
let relPath = relativePathToFile(dirPath, menuPath);
const { data } = matter(text);

entries.push(`- [${fm.title}](${relPath}) (section): ${fm.description}`);
entries.push(
`- [${data.title}](${relPath}) (section): ${data.description}`
);
});
entries.sort();
return entries.join("\n");
Expand Down
4 changes: 2 additions & 2 deletions uvu-tests/remark-toc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ description: "Protecting App 2 with Teleport"

Suite("multiple links to directories", () => {
const expected = `- [Application Access](application-access/application-access.mdx) (section): Guides related to Application Access
- [Database Access](database-access/database-access.mdx): Guides related to Database Access
- [Database Access](database-access/database-access.mdx) (section): Guides related to Database Access.
`;

const vol = Volume.fromJSON(testFilesTwoSections);
Expand Down Expand Up @@ -132,7 +132,7 @@ description: "Guides related to JWTs"
Suite("orders sections correctly", () => {
const expected = `
- [Application Access](application-access/application-access.mdx) (section): Guides related to Application Access
- [Desktop Access](desktop-access/database-access.mdx) (seciton): Guides related to Desktop Access
- [Desktop Access](desktop-access/database-access.mdx) (section): Guides related to Desktop Access
- [API Usage](api.mdx): Using the API.
- [Initial Setup](initial-setup.mdx): How to set up the product for the first time.
- [Kubernetes](kubernetes.mdx): A guide related to Kubernetes.
Expand Down

0 comments on commit 888fcf6

Please sign in to comment.