Skip to content

Commit a1fabe4

Browse files
giocodessapegin
authored andcommitted
Docs: Display source code of any file (styleguidist#964)
1 parent 70d3560 commit a1fabe4

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

docs/Configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ module.exports = {
580580
const filepath = path.resolve(exampleFilePath, settings.file)
581581
delete settings.file
582582
return {
583-
content: fs.readFileSync(filepath),
583+
content: fs.readFileSync(filepath, 'utf8'),
584584
settings,
585585
lang
586586
}

docs/Cookbook.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [How to exclude some components from style guide?](#how-to-exclude-some-components-from-style-guide)
99
* [How to hide some components in style guide but make them available in examples?](#how-to-hide-some-components-in-style-guide-but-make-them-available-in-examples)
1010
* [How to dynamically load other components in an example?](#how-to-dynamically-load-other-components-in-an-example)
11+
* [How to display the source code of any file?](#how-to-display-the-source-code-of-any-file)
1112
* [How to set global styles for user components?](#how-to-set-global-styles-for-user-components)
1213
* [How to add custom JavaScript and CSS or polyfills?](#how-to-add-custom-javascript-and-css-or-polyfills)
1314
* [How to use React Styleguidist with Preact?](#how-to-use-react-styleguidist-with-preact)
@@ -119,6 +120,41 @@ const iconElements = Object.keys(icons).map(iconName => {
119120
;<div>{iconElements}</div>
120121
```
121122

123+
## How to display the source code of any file?
124+
125+
First, coode examples can receive [props and settings](Documenting.md#usage-examples-and-readme-files):
126+
127+
```js { "file": "../mySourceCode.js" }
128+
```
129+
130+
The above example adds a setting called `file` with the **relative path** to the file we want to display as value.
131+
132+
Second, use the [updateExample](Configuration#updateexample) config option, to detect the setting and change the content of a fenced code block:
133+
134+
```javascript
135+
module.exports = {
136+
updateExample(props, exampleFilePath) {
137+
// props.settings are passed by any fenced code block, in this case
138+
const { settings, lang } = props
139+
// "../mySourceCode.js"
140+
if (typeof settings.file === 'string') {
141+
// "absolute path to mySourceCode.js"
142+
const filepath = path.resolve(exampleFilePath, settings.file)
143+
// displays the block as static code
144+
settings.static = true
145+
// no longer needed
146+
delete settings.file
147+
return {
148+
content: fs.readFileSync(filepath, 'utf8'),
149+
settings,
150+
lang
151+
}
152+
}
153+
return props
154+
}
155+
}
156+
```
157+
122158
## How to set global styles for user components?
123159

124160
Using the [jss-global](https://github.com/cssinjs/jss-global) API you can set global styles in your config:

0 commit comments

Comments
 (0)