feat: Allow modifying docs url prefix (#914)

* Allow other routes than /docs in the URL

siteConfig.js has a new mandatory field named *docsRoute* which default
value is 'docs' and that can be customized by the user.

This change will allow users who uses the library to host guides and
tutorials to customize their websites by assign 'docsRoute' values
like 'tutorials' or 'guides'.

Fixes #879

* Make "docsRoute" field optional

* Isolate docsRoute login in getDocsRoute function

* Rename docsRoute to docsUrl

* Run prettier

* Remove old folders

* fix: Restore docusaurus reference link

* fix: Add `docsUrl` param fallback. Refactor multiple function calls

* Fix linting errors

* Update description for docsUrl field

* Reduce redundant calls to getDocsUrl

* Replace a missed use case for `docsUrl` instead of the function call

* Move `getDocsUrl` out from `server/routing.js` to `server/utils.js`

**Why?**
Because `routing.js` is exporting all router RegEx's, and the
`getDocsUrl` suffices more as a util

* WiP: Align leading slashes and fix routing around `docsUrl`

Checklist:
- [x] Added `removeDuplicateLeadingSlashes` util to make sure there is only
one leading slash
- [-] Fix edge cases for routing:
  - [x] `docsUrl: ''`
  - [ ] `docsUrl: '/'`
  - [ ] make it work with languages
  - [ ] make it work with versioning

* Make leading slashes canonical cross routing and generated links

This ensures correct routing for customized `baseUrl` and `docsUrl`.

- Changed all routing functions to take `siteConfig` instead of
`siteConfig.baseUrl`
- Updated tests accordingly

* Alternative fallback for `docsUrl`

* rework/ fix implementation

* cleanup

* refactor and add docs for config props

* fix typo

* fix broken url
This commit is contained in:
Dom Corvasce 2018-11-28 08:34:16 +01:00 committed by Endilie Yacop Sucipto
parent ff22074ff7
commit 61078e38a9
28 changed files with 423 additions and 319 deletions

View file

@ -58,6 +58,9 @@ SocialFooter.propTypes = {
class Footer extends React.Component {
render() {
const docsPart = `${
this.props.config.docsUrl ? `${this.props.config.docsUrl}/` : ''
}`;
return (
<footer className="nav-footer" id="footer">
<section className="sitemap">
@ -77,28 +80,28 @@ class Footer extends React.Component {
<h5>Docs</h5>
<a
href={`
${this.props.config.baseUrl}docs/${
${this.props.config.baseUrl}${docsPart}${
this.props.language
}/installation`}>
Getting Started
</a>
<a
href={`
${this.props.config.baseUrl}docs/${
${this.props.config.baseUrl}${docsPart}${
this.props.language
}/versioning`}>
Versioning
</a>
<a
href={`
${this.props.config.baseUrl}docs/${
${this.props.config.baseUrl}${docsPart}${
this.props.language
}/translation`}>
Localization
</a>
<a
href={`
${this.props.config.baseUrl}docs/${
${this.props.config.baseUrl}${docsPart}${
this.props.language
}/search`}>
Adding Search

View file

@ -11,11 +11,11 @@ const React = require('react');
const CompLibrary = require('../../core/CompLibrary.js');
const Container = CompLibrary.Container;
const siteConfig = require(`${process.cwd()}/siteConfig.js`);
const translate = require('../../server/translate.js').translate;
class AboutSlash extends React.Component {
render() {
const {config: siteConfig} = this.props;
return (
<div className="pageContainer">
<Container className="mainContainer documentContainer postContainer">

View file

@ -10,17 +10,19 @@ const CompLibrary = require('../../core/CompLibrary.js');
const Container = CompLibrary.Container;
const GridBlock = CompLibrary.GridBlock;
const siteConfig = require(`${process.cwd()}/siteConfig.js`);
const translate = require('../../server/translate.js').translate;
class Help extends React.Component {
render() {
const {config: siteConfig} = this.props;
const supportLinks = [
{
title: <translate>Browse the docs</translate>,
content: `Learn more about Docusaurus using the [official documentation](${
siteConfig.baseUrl
}docs/${this.props.language}/installation).`,
}${siteConfig.docsUrl ? `${siteConfig.docsUrl}/` : ''}${
this.props.language
}/installation).`,
},
{
title: <translate>Discord</translate>,

View file

@ -12,27 +12,20 @@ const CompLibrary = require('../../core/CompLibrary.js');
const Container = CompLibrary.Container;
const GridBlock = CompLibrary.GridBlock;
const Showcase = require(`${process.cwd()}/core/Showcase.js`);
const siteConfig = require(`${process.cwd()}/siteConfig.js`);
const translate = require('../../server/translate.js').translate;
class Button extends React.Component {
render() {
return (
<div className="pluginWrapper buttonWrapper">
<a className="button" href={this.props.href} target={this.props.target}>
{this.props.children}
</a>
</div>
);
}
}
Button.defaultProps = {
target: '_self',
};
class HomeSplash extends React.Component {
render() {
const {siteConfig, language} = this.props;
const Button = props => (
<div className="pluginWrapper buttonWrapper">
<a className="button" href={props.href} target={props.target}>
{props.children}
</a>
</div>
);
return (
<div className="homeContainer">
<div className="homeSplashFade">
@ -53,9 +46,7 @@ class HomeSplash extends React.Component {
<div className="pluginRowBlock">
<Button
href={`
${siteConfig.baseUrl}docs/${
this.props.language
}/installation
${siteConfig.baseUrl}docs/${language}/installation
`}>
<translate>Get Started</translate>
</Button>
@ -75,12 +66,12 @@ class HomeSplash extends React.Component {
class Index extends React.Component {
render() {
const language = this.props.language || 'en';
const {config: siteConfig, language = 'en'} = this.props;
const pinnedUsersToShowcase = siteConfig.users.filter(user => user.pinned);
return (
<div>
<HomeSplash language={language} />
<HomeSplash siteConfig={siteConfig} language={language} />
<div className="mainContainer">
<Container padding={['bottom', 'top']} background="light">
<GridBlock

View file

@ -10,11 +10,11 @@ const CompLibrary = require('../../core/CompLibrary.js');
const Container = CompLibrary.Container;
const Showcase = require(`${process.cwd()}/core/Showcase.js`);
const siteConfig = require(`${process.cwd()}/siteConfig.js`);
const translate = require('../../server/translate.js').translate;
class Users extends React.Component {
render() {
const {config: siteConfig} = this.props;
const fbUsersToShowcase = siteConfig.users.filter(
user => user.fbOpenSource,
);

View file

@ -13,10 +13,10 @@ const Container = CompLibrary.Container;
const CWD = process.cwd();
const siteConfig = require(`${CWD}/siteConfig.js`);
const versions = require(`${CWD}/versions.json`);
function Versions(props) {
const {config: siteConfig} = props;
const latestVersion = versions[0];
const repoUrl = `https://github.com/${siteConfig.organizationName}/${
siteConfig.projectName
@ -36,7 +36,7 @@ function Versions(props) {
<th>{latestVersion}</th>
<td>
<a
href={`${siteConfig.baseUrl}docs/${
href={`${siteConfig.baseUrl}${siteConfig.docsUrl}/${
props.language
}/installation`}>
Documentation
@ -58,7 +58,7 @@ function Versions(props) {
<th>master</th>
<td>
<a
href={`${siteConfig.baseUrl}docs/${
href={`${siteConfig.baseUrl}${siteConfig.docsUrl}/${
props.language
}/next/installation`}>
Documentation
@ -83,7 +83,7 @@ function Versions(props) {
<th>{version}</th>
<td>
<a
href={`${siteConfig.baseUrl}docs/${
href={`${siteConfig.baseUrl}${siteConfig.docsUrl}/${
props.language
}/${version}/installation`}>
Documentation