facebook / facebook/docusaurus

Add support for versioned sidebars which are `.js` not `.json`

Đang mở
#10,407 3 bình luận 2 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
TypeScript
Star
66.2k
Fork
10k
Merge trung bình
1 ngày 3 giờ
Pull request đã merge (30 ngày)
52

Mô tả

### Have you read the Contributing Guidelines on issues?

- [X] I have read the [Contributing Guidelines on issues](https://github.com/facebook/docusaurus/blob/main/CONTRIBUTING.md#issues).

### Prerequisites

- [X] I'm using the latest version of Docusaurus.
- [X] I have tried the `npm run clear` or `yarn clear` command.
- [X] I have tried `rm -rf node_modules yarn.lock package-lock.json` and re-installing packages.
- [X] I have tried creating a repro with https://new.docusaurus.io.
- [X] I have read the console error message carefully (if applicable).

### Description

[Version sidebars are hard coded to be `.json` ](https://github.com/facebook/docusaurus/blob/ae5328daac080418c485ac9ee3393a4270d431e4/packages/docusaurus-plugin-content-docs/src/versions/files.ts#L53). files.

It doesn't mention this in the documentation on [versioning](https://docusaurus.io/docs/versioning#creating-new-docs) - it just kind of implies it.

Maybe there should be an `alert` warning people about it? or there could be some kind of fix that would make it possible to use either.

Here's my proposed fix:

Make the `getVersionSidebarsPath` function read the user's source code to see if it's `.js` or `.json` before it returns. I'm sure a more elegant way exists but this seems fine, unless I'm missing something.

### Reproducible demo

https://codesandbox.io/p/devbox/priceless-goldwasser-qdeo7?file=%2FREADME.md&privacy=public

### Steps to reproduce

1. Make a new version `npm run docusaurus docs:version 1.1.0

2. Replace the `version-1.1.0-sidebar.json` with an equivalent `.js` file like:
```
const sidebar = {
tutorialSidebar: [
{
type: "autogenerated",
dirName: ".",
},
],
};

module.exports = sidebar;
```
3. Observe a crash

### Expected behavior

Versioned sidebars should behave like normal ones, allowing `js` (or even `ts`)

### Actual behavior

It crashes, it can't find the sidebar

### Your environment

- Public source code:
- Public site URL:
- Docusaurus version used:
- Environment name and version (e.g. Chrome 89, Node.js 16.4):
- Operating system and version (e.g. Ubuntu 20.04.2 LTS):

### Self-service

- [X] I'd be willing to fix this bug myself.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.