Skip to content

Commit

Permalink
chore: extract path creating functions
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jun 8, 2020
1 parent 8db4a10 commit bf41c47
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
29 changes: 29 additions & 0 deletions core/specification/src/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,32 @@ export interface StoriesStore {
*/
packages: StoryPackages;
}

export const getDocPath = (
doc?: StoriesDoc,
config?: RunConfiguration,
): string => {
const { docsPath = '' } = config || {};
return doc ? doc.route || `/${docsPath}${doc.title?.toLowerCase()}/` : '';
};

export const getBlogPath = (
doc?: StoriesDoc,
config?: RunConfiguration,
): string => {
const { blogsPath = '' } = config || {};
return doc ? doc.route || `/${blogsPath}${doc.title?.toLowerCase()}/` : '';
};
export const getStoryPath = (
story?: Story,
doc?: StoriesDoc,
config?: RunConfiguration,
): string => {
if (!story) {
return '';
}
const docsPath = config?.docsPath || '';
return doc
? doc.route || `/${docsPath}${doc.title?.toLowerCase()}/#${story.id}`
: '';
};
16 changes: 9 additions & 7 deletions core/store/src/Store/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
StoryDocs,
StoriesDoc,
RunConfiguration,
getDocPath,
getStoryPath,
getBlogPath,
} from '@component-controls/specification';
import { BroadcastChannel } from 'broadcast-channel';
import {
Expand Down Expand Up @@ -204,20 +207,19 @@ export class Store implements StoryStore {

getDocPath = (name: string): string => {
const doc = this.getStoryDoc(name);
const docsPath = this.config?.docsPath || '';
return doc ? doc.route || `/${docsPath}${name.toLowerCase()}/` : '';
return getDocPath(doc, this.config);
};
getBlogPath = (name: string): string => {
const doc = this.getStoryDoc(name);
return getBlogPath(doc, this.config);
};

getStoryPath = (storyId: string): string => {
const story = this.getStory(storyId);
if (!story) {
return '';
}
const doc = this.getStoryDoc(story?.doc || '');
const docsPath = this.config?.docsPath || '';
return doc
? doc.route || `/${docsPath}${doc.title.toLowerCase()}/#${story.id}`
: '';
return getStoryPath(story, doc, this.config);
};

/**
Expand Down
1 change: 1 addition & 0 deletions core/store/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface StoryStore {
firstStory: string | undefined;
firstDoc: string | undefined;
getDocPath: (name: string) => string;
getBlogPath: (name: string) => string;
getStoryPath: (storyId: string) => string;
updateStoryProp: (
storyId: string,
Expand Down
6 changes: 4 additions & 2 deletions integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getDocPath, getBlogPath } from '@component-controls/specification';

import {
compile,
watch,
Expand Down Expand Up @@ -28,7 +30,7 @@ exports.createPages = async (
const docs = store.getDocs();
docs.forEach(doc => {
createPage({
path: doc.route || `/${docsPath}${doc.title.toLowerCase()}`,
path: getDocPath(doc, store.buildConfig),
component: docTemplate,
context: {
doc: doc.title,
Expand All @@ -51,7 +53,7 @@ exports.createPages = async (

blogs.forEach(blog => {
createPage({
path: blog.route || `/${blogsPath}${blog.title.toLowerCase()}`,
path: getBlogPath(blog, store.buildConfig),
component: blogTemplate,
context: {
doc: blog.title,
Expand Down

0 comments on commit bf41c47

Please sign in to comment.