-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstyleguide.config.js
143 lines (137 loc) · 3.23 KB
/
styleguide.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const fs = require('fs');
const path = require('path');
const glob = require('glob');
// styleguidist server --exercises
const isExercises = !!process.argv.find(x => x === '--exercises');
const borderedStyle = {
border: '1px solid #e8e8e8',
borderRadius: 3,
padding: 16,
};
// Prepare Markdown file to use as final result:
// 1. Remove ```js ... ``` markers from Markdown
// 2. Replace <></> with a <div /> that looks like a Styleguidist example,
// but without an editor
const markdownToCodeExample = s =>
s
.replace(/^```\w*$/gm, '')
.replace(/<>/, `<div style={${JSON.stringify(borderedStyle)}}>`)
.replace(/<\/>/, `</div>`);
const config = {
serverPort: isExercises ? 6061 : 6060,
title: isExercises
? 'Design systems for React developers'
: 'Component-driven',
styleguideDir: 'public/styleguide',
assetsDir: 'static',
styleguideComponents: {
Wrapper: path.join(__dirname, 'src/ThemeProvider'),
},
// Read examples from Component.md files only, not from Readme.md
getExampleFilename: x => x.replace(/\.js$/, '.md'),
skipComponentsWithoutExample: true,
pagePerSection: true,
exampleMode: 'expand',
usageMode: 'expand',
styles: {
StyleGuide: {
'@global img': {
maxWidth: '100%',
},
},
},
webpackConfig: {
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
],
},
},
require: [path.join(__dirname, 'styleguide.setup.js')],
updateExample(props, exampleFilePath) {
const { settings, lang } = props;
if (settings && typeof settings.file === 'string') {
const filepath = path.resolve(
path.dirname(exampleFilePath),
settings.file
);
const { file, ...restSettings } = settings;
const rawContent = fs.readFileSync(filepath, 'utf8');
const content =
filepath.endsWith('.md') && lang === 'jsx'
? markdownToCodeExample(rawContent)
: rawContent;
return {
content,
settings: restSettings,
lang,
};
}
return props;
},
};
if (isExercises) {
// Generate sections for all exercises
const exercisesRoot = path.join(__dirname, `src/exercises`);
const exercises = glob.sync(`${exercisesRoot}/*`);
config.sections = exercises.map(folder => ({
name: path
.basename(folder)
.replace(/^(\d+)-/, '$1. ')
.replace(/_/g, ' '),
content: `${folder}/Readme.md`,
sectionDepth: 1,
sections: [
{
name: 'Task',
content: `${folder}/Readme.md`,
},
{
name: 'Exercises',
components: `${folder}/*.js`,
},
],
}));
} else {
// Styleguide
config.sections = [
{
name: 'Foundation',
content: 'docs/Foundation.md',
sections: [
{
name: 'Colors',
content: 'docs/Colors.md',
},
{
name: 'Typography',
content: 'docs/Typography.md',
},
{
name: 'Spacing',
content: 'docs/Spacing.md',
},
],
},
{
name: 'Primitives',
content: 'src/components/primitives/Readme.md',
components: 'src/components/primitives/**/[A-Z]*.js',
},
{
name: 'Layout Primitives',
content: 'src/components/layout/Readme.md',
components: 'src/components/layout/**/[A-Z]*.js',
},
{
name: 'UI Patterns',
content: 'src/components/patterns/Readme.md',
components: 'src/components/patterns/**/[A-Z]*.js',
},
];
}
module.exports = config;