Skip to content
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

Fix title extractor for custom auto generated sidebar #217

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions website/sidebars.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import type { SidebarsConfig } from '@docusaurus/plugin-content-docs';

const SIDEBAR_POSITION_REGEX = /sidebar_position:\s?(\d+)/;
const SIDEBAR_POSITION_REGEX = /sidebar_position:\s?(\d+)/m;
const TITLE_EXTRACTOR = /^#\s?(.*)$/m;

/**
* Looks like Docusaurus has an issue autogenerating docs for multi doc instance
* https://stackoverflow.com/questions/77528478/sidebar-wont-autogenerate-with-docs-multi-instances-on-docusaurus
*/
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
function capitalizeFirstLetter(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1);
}

function autoGenerate(path) {
function autoGenerate(path: string) {
const fs = require('fs');
const files = fs.readdirSync(path);

return files
.map(file => {
.map((file: string) => {
if (!file.endsWith('.md')) {
return null;
}
Expand All @@ -25,10 +27,11 @@ function autoGenerate(path) {
flag: 'r',
});
const position = content.match(SIDEBAR_POSITION_REGEX)?.[1];
const title = content.match(TITLE_EXTRACTOR)?.[1];

return {
type: 'link',
label: capitalizeFirstLetter(name.replace('-', ' ')),
label: title ?? capitalizeFirstLetter(name.replace('-', ' ')),
href: name,
autoAddBaseUrl: true,
position: parseInt(position ?? 0),
Expand Down
Loading