Skip to content

Commit d236989

Browse files
authored
feat: option to add web name into title (#37)
1 parent e269991 commit d236989

File tree

9 files changed

+39
-8
lines changed

9 files changed

+39
-8
lines changed

example/component-docs.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ module.exports = {
3838
pages,
3939
output,
4040
github,
41+
title: '[title] - Component Docs',
4142
};

src/build.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default async function build(o: Options) {
2424
logo,
2525
output,
2626
colors,
27+
title,
2728
} = options;
2829

2930
const pages = typeof getPages === 'function' ? getPages() : getPages;
@@ -49,7 +50,7 @@ export default async function build(o: Options) {
4950

5051
fs.writeFileSync(
5152
path.join(output, 'app.src.js'),
52-
buildEntry({ styles, github, logo })
53+
buildEntry({ styles, github, logo, title })
5354
);
5455

5556
fs.writeFileSync(path.join(output, 'app.data.js'), stringifyData(data));
@@ -65,6 +66,7 @@ export default async function build(o: Options) {
6566
sheets: ['app.css'],
6667
scripts: scripts ? scripts.map(s => `scripts/${path.basename(s)}`) : [],
6768
colors,
69+
title,
6870
})
6971
);
7072
});

src/cli.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ const { argv } = yargs
4848
description: 'Link to github folder to show edit button',
4949
requiresArg: true,
5050
},
51+
title: {
52+
type: 'string',
53+
description: 'Title of a web page',
54+
requiresArg: true,
55+
},
5156
})
5257
.command('serve', 'serve pages for development', y => {
5358
y.options({

src/serve.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default function serve(o: Options) {
3434
port,
3535
open,
3636
colors,
37+
title,
3738
} = options;
3839

3940
const cache: Map<string, Metadata> = new Map();
@@ -63,7 +64,7 @@ export default function serve(o: Options) {
6364

6465
fs.writeFileSync(
6566
path.join(output, 'app.src.js'),
66-
buildEntry({ styles, github, logo })
67+
buildEntry({ styles, github, logo, title })
6768
);
6869

6970
fs.writeFileSync(path.join(output, 'app.data.js'), stringifyData(data));
@@ -78,6 +79,7 @@ export default function serve(o: Options) {
7879
sheets: ['app.css'],
7980
scripts: scripts ? scripts.map(s => `scripts/${path.basename(s)}`) : [],
8081
colors,
82+
title,
8183
});
8284
return acc;
8385
}, {});
@@ -142,6 +144,7 @@ export default function serve(o: Options) {
142144
? scripts.map(s => `scripts/${path.basename(s)}`)
143145
: [],
144146
colors,
147+
title,
145148
});
146149
return acc;
147150
}, {});

src/templates/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ type Props = {
8080
data: Data,
8181
github?: string,
8282
logo?: string,
83+
title?: string,
8384
};
8485

85-
export default function App({ path, data, github, logo }: Props) {
86+
export default function App({ path, data, github, logo, title }: Props) {
8687
const routes = buildRoutes(data, github, logo);
87-
return <Router path={path} routes={routes} />;
88+
return <Router path={path} routes={routes} title={title} />;
8889
}

src/templates/Router.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { Route } from '../types';
77
type Props = {
88
path: string,
99
routes: Array<Route>,
10+
title?: string,
1011
};
1112

1213
type State = {
@@ -41,11 +42,14 @@ export default class Router extends Component<Props, State> {
4142
}
4243

4344
componentDidUpdate(prevProps: Props, prevState: State) {
45+
const { routes, title } = this.props;
4446
if (prevState.path !== this.state.path) {
45-
const route = this.props.routes.find(r => r.link === this.state.path);
47+
const route = routes.find(r => r.link === this.state.path);
4648
if (route) {
4749
// eslint-disable-next-line no-undef
48-
document.title = route.title || '';
50+
document.title = title
51+
? title.replace(/\[title\]/g, route.title)
52+
: route.title || '';
4953
}
5054
}
5155
}

src/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type Options = {
2424
colors?: {
2525
primary?: string,
2626
},
27+
title?: string,
2728
};
2829

2930
export type PageInfo = {

src/utils/buildEntry.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ export default function buildEntry({
66
styles,
77
github,
88
logo,
9+
title,
910
}: {
1011
styles?: string[],
1112
github?: string,
1213
logo?: string,
14+
title?: string,
1315
}) {
1416
return `
1517
import React from 'react';
@@ -37,6 +39,7 @@ const render = () => {
3739
data={data}
3840
github={${github !== undefined ? JSON.stringify(github) : 'undefined'}}
3941
logo={${logo !== undefined ? JSON.stringify(logo) : 'undefined'}}
42+
title={${title !== undefined ? JSON.stringify(title) : 'undefined'}}
4043
/>,
4144
root
4245
);

src/utils/buildHTML.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Options = {
1616
colors?: {
1717
primary?: string,
1818
},
19+
title?: string,
1920
};
2021

2122
export default function buildHTML({
@@ -26,9 +27,16 @@ export default function buildHTML({
2627
sheets,
2728
scripts,
2829
colors = {},
30+
title,
2931
}: Options) {
3032
const html = ReactDOMServer.renderToString(
31-
<App logo={logo} path={info.link} data={data} github={github} />
33+
<App
34+
logo={logo}
35+
path={info.link}
36+
data={data}
37+
github={github}
38+
title={title}
39+
/>
3240
);
3341

3442
let body = `
@@ -81,10 +89,13 @@ export default function buildHTML({
8189
body += `<script src="${s}"></script>`;
8290
});
8391

92+
const pageTitle =
93+
title !== undefined ? title.replace(/\[title\]/g, info.title) : info.title;
94+
8495
return ReactDOMServer.renderToStaticMarkup(
8596
// eslint-disable-next-line react/jsx-pascal-case
8697
<HTML
87-
title={info.title}
98+
title={pageTitle}
8899
description={(info.description || '').split('\n')[0]}
89100
body={body}
90101
sheets={sheets}

0 commit comments

Comments
 (0)