Skip to content

Commit

Permalink
feat: base url siteRoot configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Oct 10, 2020
1 parent 9d6e562 commit 4baa386
Show file tree
Hide file tree
Showing 32 changed files with 329 additions and 82 deletions.
26 changes: 14 additions & 12 deletions core/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1067,14 +1067,15 @@ _defined in [@component-controls/core/src/configuration.ts](https://github.com/c
| `ignore` | string\[] | files to ignore. by default \['readme.md', 'changelog.md', 'code_of_conduct.md', 'contributing.md', 'license.md'] |
| `instrument` | any | instrumentation configuration |
| `pages` | [PagesOnlyRoutes](#pagesonlyroutes) | base url path for API documentation pages. Default is "docs/" |
| `siteRoot` | string | the site base url, by default the site starts at / |
| `stories` | string \| string\[] | wild card search string for the stories internally using \`glob\` for the search: https://www.npmjs.com/package/glob example: "./stories/ |
| `webpack` | [WebpackConfig](#webpackconfig) | custom webpack configuration setup. One or the other will be used |

## ControlsConfig

configuration options for the controls module

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L186)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L189)_



Expand Down Expand Up @@ -1105,7 +1106,7 @@ _defined in [@component-controls/core/src/configuration.ts](https://github.com/c
global configuration used at build time
stored in a file named main.js/main.ts

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L198)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L201)_



Expand Down Expand Up @@ -1167,7 +1168,7 @@ _defined in [@component-controls/core/src/configuration.ts](https://github.com/c

## ToolbarConfig

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L166)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L169)_



Expand Down Expand Up @@ -1259,7 +1260,7 @@ Record<
## RunConfiguration

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L304)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L307)_

### properties

Expand All @@ -1272,13 +1273,13 @@ _defined in [@component-controls/core/src/configuration.ts](https://github.com/c

static menu items

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L181)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L184)_

string | **menu**: [StaticMenuItem](#staticmenuitem)\[]**name**: string

## StaticMenuItems

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L182)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L185)_

[StaticMenuItem](#staticmenuitem)\[]

Expand All @@ -1304,7 +1305,7 @@ _defined in [@component-controls/core/src/configuration.ts](https://github.com/c

## convertConfig

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L344)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L347)_

**function** convertConfig(`config`\*: [RunConfiguration](#runconfiguration)): [RunConfiguration](#runconfiguration);

Expand All @@ -1317,7 +1318,7 @@ _defined in [@component-controls/core/src/configuration.ts](https://github.com/c

## defaultBuildConfig

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L353)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L356)_



Expand All @@ -1327,11 +1328,12 @@ _defined in [@component-controls/core/src/configuration.ts](https://github.com/c
| ------------- | --------- | ----------- |
| `categories*` | string\[] | |
| `ignore*` | string\[] | |
| `siteRoot*` | string | |
| `pages*` | object | |

## defaultRunConfig

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L307)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L310)_



Expand Down Expand Up @@ -1397,7 +1399,7 @@ Record<string,
## RunConfiguration

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L304)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L307)_

### properties

Expand Down Expand Up @@ -1559,7 +1561,7 @@ Record<
## StaticMenuItems

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L182)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L185)_

[StaticMenuItem](#staticmenuitem)\[]

Expand Down Expand Up @@ -1627,7 +1629,7 @@ _defined in [@component-controls/core/src/configuration.ts](https://github.com/c

static menu items

_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L181)_
_defined in [@component-controls/core/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/core/src/configuration.ts#L184)_

string | **menu**: [StaticMenuItem](#staticmenuitem)\[]**name**: string

Expand Down
6 changes: 5 additions & 1 deletion core/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ export interface BuildConfiguration {
* alternative naming for docz compatibility
*/
files?: string | string[];

/**
* the site base url, by default the site starts at /
*/
siteRoot?: string;
/**
* files to ignore. by default ['readme.md', 'changelog.md', 'code_of_conduct.md', 'contributing.md', 'license.md']
*/
Expand Down Expand Up @@ -351,6 +354,7 @@ export const convertConfig = (config: RunConfiguration): RunConfiguration => {
};

export const defaultBuildConfig: BuildConfiguration = {
siteRoot: '/',
categories: ['author', 'tags'],
ignore: [
'readme.md',
Expand Down
76 changes: 52 additions & 24 deletions core/core/src/document-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import {
toId,
storyNameFromExport as csfStoryNameFromExport,
} from '@storybook/csf';
import { PagesOnlyRoutes, DocType, PageConfiguration } from './configuration';
import {
PagesOnlyRoutes,
DocType,
PageConfiguration,
BuildConfiguration,
} from './configuration';
import { Document, Story, defDocType, Store } from './document';

export const storyNameFromExport = csfStoryNameFromExport;
Expand All @@ -14,8 +19,21 @@ export const ensureTrailingSlash = (route: string) =>
export const ensureStartingSlash = (route: string) =>
route.startsWith('/') ? route : '/' + route;

export const removeTrailingSlash = (route: string) =>
route.endsWith('/') ? route.substr(0, route.length - 1) : route;
export const removeTrailingSlash = (route: string) => {
let result = route;
while (result.length > 1 && result.endsWith('/')) {
result = result.substr(0, result.length - 1);
}
return result;
};

export const removeStartingSlash = (route: string) => {
let result = route;
while (result.length > 1 && result.startsWith('/')) {
result = result.substr(1);
}
return result;
};

export const getDocPath = (
docType: DocType,
Expand All @@ -27,6 +45,8 @@ export const getDocPath = (
const pagesConfig: PagesOnlyRoutes | undefined = store
? (store.config.pages as PagesOnlyRoutes)
: undefined;
const { siteRoot = '/' } = (store?.config as BuildConfiguration) || {};

const { basePath = '', sideNav = {} } = pagesConfig?.[docType] || {};
const { storyPaths } = sideNav;
const activeTab = doc?.MDXPage ? undefined : tab;
Expand All @@ -35,17 +55,13 @@ export const getDocPath = (
}
const route = doc
? doc.route ||
`${ensureStartingSlash(
ensureTrailingSlash(basePath),
)}${ensureTrailingSlash(strToId(doc.title))}${
activeTab ? ensureTrailingSlash(activeTab) : ''
`${ensureTrailingSlash(basePath)}${strToId(doc.title)}${
activeTab ? ensureStartingSlash(activeTab) : ''
}`
: `${ensureStartingSlash(
ensureTrailingSlash(basePath),
)}${ensureTrailingSlash(strToId(name))}${
activeTab ? ensureTrailingSlash(activeTab) : ''
: `${ensureTrailingSlash(basePath)}${strToId(name)}${
activeTab ? ensureStartingSlash(activeTab) : ''
}`;
return removeTrailingSlash(route);
return encodeURI(`${siteRoot}${removeStartingSlash(route)}`);
};

export const getStoryPath = (
Expand All @@ -57,32 +73,44 @@ export const getStoryPath = (
const pagesConfig: PagesOnlyRoutes | undefined = store
? (store.config.pages as PagesOnlyRoutes)
: undefined;

const { siteRoot = '/' } = (store?.config as BuildConfiguration) || {};
const docType = doc?.type || defDocType;
const activeTab = doc?.MDXPage ? undefined : tab;
if (!storyId) {
return getDocPath(docType, doc, store, undefined, activeTab);
}
const { basePath = '' } = pagesConfig?.[docType] || {};
const docRoute = `${
doc?.route
? ensureStartingSlash(ensureTrailingSlash(doc?.route))
: ensureStartingSlash(ensureTrailingSlash(basePath))
}`;
const docRoute = removeTrailingSlash(doc?.route || basePath);
const story = store?.stories[storyId];
const { dynamicId, name } = story || {};
const id = dynamicId || storyId;
const route = `${docRoute}${id ? ensureTrailingSlash(id) : ''}${
activeTab ? ensureTrailingSlash(activeTab) : ''
const route = `${docRoute}${id ? ensureStartingSlash(id) : ''}${
activeTab ? ensureStartingSlash(activeTab) : ''
}${dynamicId ? `?story=${name}` : ''}`;
return encodeURI(removeTrailingSlash(route));
return encodeURI(`${siteRoot}${route}`);
};

export const getDocTypePath = (type: PageConfiguration) =>
type.basePath
? removeTrailingSlash(ensureStartingSlash(type.basePath))
export const getDocTypePath = (store: Store, type: PageConfiguration) => {
const { siteRoot = '/' } = (store?.config as BuildConfiguration) || {};
return type.basePath
? `${siteRoot}${removeTrailingSlash(type.basePath)}`
: undefined;
};

export const getHomePath = (store: Store) => {
const { siteRoot = '/' } = (store?.config as BuildConfiguration) || {};
return siteRoot.length > 1 ? removeTrailingSlash(siteRoot) : siteRoot;
};

export const getRoutePath = (store: Store, route?: string) => {
const { siteRoot = '/' } = (store?.config as BuildConfiguration) || {};

return route
? removeTrailingSlash(
`${ensureTrailingSlash(siteRoot)}${removeStartingSlash(route)}`,
)
: undefined;
};
export const docStoryToId = (docId: string, storyId: string) =>
toId(docId, storyNameFromExport(storyId));

Expand Down
133 changes: 133 additions & 0 deletions core/core/test/paths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import {
getHomePath,
getRoutePath,
getDocPath,
getStoryPath,
} from '../src/document-utils';
import { getDefaultStore, Store } from '../src/document';
import { defaultBuildConfig } from '../src/configuration';
import { deepMerge } from '../src';
describe('paths', () => {
const storeDefault: Store = deepMerge(getDefaultStore(), {
config: defaultBuildConfig,
stories: {
'api-introducetion--story-mame': {
name: 'StoryName',
},
'api-introducetion--dynamic': {
name: 'Dynamic',
dynamicId: 'api-introducetion--story-mame',
},
},
});
const storeSiteRoot1: Store = deepMerge(storeDefault, {
config: {
siteRoot: '/root',
},
});
const storeSiteRoot2: Store = deepMerge(storeDefault, {
config: {
siteRoot: '/root/',
},
});

it('home path "/"', () => {
expect(getHomePath(storeDefault)).toEqual('/');
});
it('home path "/root"', () => {
expect(getHomePath(storeSiteRoot1)).toEqual('/root');
});
it('home path "/root/"', () => {
expect(getHomePath(storeSiteRoot2)).toEqual('/root');
});

it('route home page "/"', () => {
expect(getRoutePath(storeDefault, '/')).toEqual('/');
});
it('route home page "/root"', () => {
expect(getRoutePath(storeSiteRoot1, '/')).toEqual('/root');
});
it('route home page "/root/"', () => {
expect(getRoutePath(storeSiteRoot2, '/')).toEqual('/root');
});

it('route "/custom/route" with root "/"', () => {
expect(getRoutePath(storeDefault, '/custom/route')).toEqual(
'/custom/route',
);
});
it('route "/custom/route" with root "/root"', () => {
expect(getRoutePath(storeSiteRoot1, '/custom/route')).toEqual(
'/root/custom/route',
);
});
it('route "/custom/route" with root "/root/"', () => {
expect(getRoutePath(storeSiteRoot2, '/custom/route')).toEqual(
'/root/custom/route',
);
});

it('document route with title with root "/"', () => {
expect(
getDocPath(
'story',
{ title: 'API/Introducetion', componentsLookup: {} },
storeDefault,
'doc',
'test',
),
).toEqual('/docs/api-introducetion/test');
});
it('document route with title with root "/root/"', () => {
expect(
getDocPath(
'story',
{ title: 'API/Introducetion', componentsLookup: {} },
storeSiteRoot2,
'doc',
'test',
),
).toEqual('/root/docs/api-introducetion/test');
});
it('document route with name with root "/"', () => {
expect(getDocPath('story', undefined, storeDefault, 'Doc name')).toEqual(
'/docs/doc-name',
);
});
it('document route with name with root "/root/"', () => {
expect(getDocPath('story', undefined, storeSiteRoot2, 'Doc-name')).toEqual(
'/root/docs/doc-name',
);
});

it('story route with title with root "/"', () => {
expect(
getStoryPath(
'api-introducetion--story-mame',
undefined,
storeDefault,
'test',
),
).toEqual('/docs/api-introducetion--story-mame/test');
});
it('story route with title with root "/root/"', () => {
expect(
getStoryPath(
'api-introducetion--story-mame',
undefined,
storeSiteRoot2,
'test',
),
).toEqual('/root/docs/api-introducetion--story-mame/test');
});
it('story route with dynmic id with root "/root/"', () => {
expect(
getStoryPath(
'api-introducetion--dynamic',
undefined,
storeSiteRoot2,
'test',
),
).toEqual('/root/docs/api-introducetion--story-mame/test?story=Dynamic');
});
});
Loading

0 comments on commit 4baa386

Please sign in to comment.