|
8 | 8 | * [How to exclude some components from style guide?](#how-to-exclude-some-components-from-style-guide)
|
9 | 9 | * [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)
|
10 | 10 | * [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) |
11 | 12 | * [How to set global styles for user components?](#how-to-set-global-styles-for-user-components)
|
12 | 13 | * [How to add custom JavaScript and CSS or polyfills?](#how-to-add-custom-javascript-and-css-or-polyfills)
|
13 | 14 | * [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 => {
|
119 | 120 | ;<div>{iconElements}</div>
|
120 | 121 | ```
|
121 | 122 |
|
| 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 | + |
122 | 158 | ## How to set global styles for user components?
|
123 | 159 |
|
124 | 160 | Using the [jss-global](https://github.com/cssinjs/jss-global) API you can set global styles in your config:
|
|
0 commit comments