-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate docs sidebar sections automatically #480
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,12 @@ import type { Redirect } from "next/dist/lib/load-custom-routes"; | |
|
||
import Ajv from "ajv"; | ||
import { validateConfig } from "./config-common"; | ||
import { resolve, join } from "path"; | ||
import { existsSync, readFileSync } from "fs"; | ||
import { dirname, resolve, join } from "path"; | ||
import fs from "fs"; | ||
import { isExternalLink, isHash, splitPath } from "../utils/url"; | ||
import { NavigationCategory, NavigationItem } from "../layouts/DocsPage/types"; | ||
import { loadConfig as loadSiteConfig } from "./config-site"; | ||
import { generateNavPaths } from "./pages-helpers"; | ||
|
||
const { latest } = loadSiteConfig(); | ||
export interface Config { | ||
|
@@ -31,8 +32,8 @@ const getConfigPath = (version: string) => | |
export const load = (version: string) => { | ||
const path = getConfigPath(version); | ||
|
||
if (existsSync(path)) { | ||
const content = readFileSync(path, "utf-8"); | ||
if (fs.existsSync(path)) { | ||
const content = fs.readFileSync(path, "utf-8"); | ||
|
||
return JSON.parse(content) as Config; | ||
} else { | ||
|
@@ -61,6 +62,8 @@ const validator = ajv.compile({ | |
properties: { | ||
icon: { type: "string" }, | ||
title: { type: "string" }, | ||
generateFrom: { type: "string" }, | ||
// Entries must be empty if generateFrom is present. | ||
entries: { | ||
type: "array", | ||
items: { | ||
|
@@ -228,7 +231,7 @@ const correspondingFileExistsForURL = ( | |
|
||
if ( | ||
[docsPagePath, indexPath, introPath].find((p) => { | ||
return existsSync(p); | ||
return fs.existsSync(p); | ||
}) == undefined | ||
) { | ||
return false; | ||
|
@@ -304,8 +307,6 @@ export const normalize = (config: Config, version: string): Config => { | |
return config; | ||
}; | ||
|
||
/* Load, validate and normalize config. */ | ||
|
||
export const loadConfig = (version: string) => { | ||
const config = load(version); | ||
|
||
|
@@ -327,5 +328,18 @@ export const loadConfig = (version: string) => { | |
|
||
validateConfig<Config>(validator, config); | ||
|
||
config.navigation.forEach((item, i) => { | ||
if (!!item.generateFrom && item.entries.length > 0) { | ||
throw "a navigation item cannot contain both generateFrom and entries"; | ||
} | ||
Comment on lines
+332
to
+334
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for the check here! |
||
|
||
if (!!item.generateFrom) { | ||
config.navigation[i].entries = generateNavPaths( | ||
fs, | ||
join("content", version, "docs", "pages", item.generateFrom) | ||
); | ||
} | ||
}); | ||
|
||
return normalize(config, version); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,8 @@ | |
|
||
</CodeLine> | ||
|
||
<br /> | ||
|
||
<CommandComment data-type="descr"> | ||
Create role | ||
</CommandComment> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,9 @@ | |
|
||
import type { MDXPage, MDXPageData, MDXPageFrontmatter } from "./types-unist"; | ||
|
||
import { resolve } from "path"; | ||
import { readSync } from "to-vfile"; | ||
import matter from "gray-matter"; | ||
import { sep, parse, dirname, resolve, join } from "path"; | ||
|
||
export const extensions = ["md", "mdx", "ts", "tsx", "js", "jsx"]; | ||
|
||
|
@@ -61,3 +61,78 @@ export const getPageInfo = <T = MDXPageFrontmatter>( | |
|
||
return result; | ||
}; | ||
|
||
const getEntryForPath = (fs, filePath) => { | ||
const txt = fs.readFileSync(filePath, "utf8"); | ||
const { data } = matter(txt); | ||
const slug = filePath.split("docs/pages")[1].replace(".mdx", "/"); | ||
return { | ||
title: data.title, | ||
slug: slug, | ||
}; | ||
}; | ||
|
||
export const generateNavPaths = (fs, dirPath) => { | ||
const firstLvl = fs.readdirSync(dirPath, "utf8"); | ||
let result = []; | ||
let firstLvlFiles = new Set(); | ||
let firstLvlDirs = new Set(); | ||
Comment on lines
+78
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
firstLvl.forEach((p) => { | ||
const fullPath = join(dirPath, p); | ||
const info = fs.statSync(fullPath); | ||
if (info.isDirectory()) { | ||
firstLvlDirs.add(fullPath); | ||
return; | ||
} | ||
firstLvlFiles.add(fullPath); | ||
}); | ||
let sectionIntros = new Set(); | ||
firstLvlDirs.forEach((d: string) => { | ||
const { name } = parse(d); | ||
const asFile = join(d, name + ".mdx"); | ||
|
||
if (!fs.existsSync(asFile)) { | ||
throw `subdirectory in generated sidebar section ${d} has no category page ${asFile}`; | ||
} | ||
sectionIntros.add(asFile); | ||
return; | ||
}); | ||
|
||
// Add files with no corresponding directory to the navigation first. Section | ||
// introductions, by convention, have a filename that corresponds to the | ||
// subdirectory containing pages in the section, or have the name | ||
// "introduction.mdx". | ||
firstLvlFiles.forEach((f) => { | ||
result.push(getEntryForPath(fs, f)); | ||
}); | ||
|
||
sectionIntros.forEach((si: string) => { | ||
const { slug, title } = getEntryForPath(fs, si); | ||
const section = { | ||
title: title, | ||
slug: slug, | ||
entries: [], | ||
}; | ||
const sectionDir = dirname(si); | ||
const secondLvl = fs.readdirSync(sectionDir, "utf8"); | ||
secondLvl.forEach((f2) => { | ||
const { name } = parse(f2); | ||
|
||
// The directory name is the same as the filename, meaning that we have | ||
// already used this as a category page. | ||
if (sectionDir.endsWith(name)) { | ||
return; | ||
} | ||
|
||
const fullPath2 = join(sectionDir, f2); | ||
const stat = fs.statSync(fullPath2); | ||
if (stat.isDirectory()) { | ||
return; | ||
} | ||
|
||
section.entries.push(getEntryForPath(fs, fullPath2)); | ||
}); | ||
result.push(section); | ||
}); | ||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is fine, but wondering why the change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generateNavPaths
takesfs
as an argument so I can use the in-memoryfs
from thememfs
package in tests!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see now. That's awesome