Repository: styleguidist/react-styleguidist
Pull request #583 (2017-08-17 22:48:44)

feat(*): pass exampleFilePath to update example
https://github.com/styleguidist/react-styleguidist/issues/579

codecov-io: # [Codecov](https://codecov.io/gh/styleguidist/react-styleguidist/pull/583?src=pr&el=h1) Report
> Merging [#583](https://codecov.io/gh/styleguidist/react-styleguidist/pull/583?src=pr&el=desc) into [master](https://codecov.io/gh/styleguidist/react-styleguidist/commit/b8d68e196ba1c6010e232fd9597d047a740b30a7?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/styleguidist/react-styleguidist/pull/583/graphs/tree.svg?token=Z1XExpSVLu&width=650&src=pr&height=150)](https://codecov.io/gh/styleguidist/react-styleguidist/pull/583?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master    #583      +/-   ##
=========================================
+ Coverage   96.19%   96.2%   +<.01%     
=========================================
  Files          99      99              
  Lines        1315    1318       +3     
  Branches      271     274       +3     
=========================================
+ Hits         1265    1268       +3     
  Misses         49      49              
  Partials        1       1
```


| [Impacted Files](https://codecov.io/gh/styleguidist/react-styleguidist/pull/583?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [loaders/examples-loader.js](https://codecov.io/gh/styleguidist/react-styleguidist/pull/583?src=pr&el=tree#diff-bG9hZGVycy9leGFtcGxlcy1sb2FkZXIuanM=) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/styleguidist/react-styleguidist/pull/583?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/styleguidist/react-styleguidist/pull/583?src=pr&el=footer). Last update [b8d68e1...4b46b21](https://codecov.io/gh/styleguidist/react-styleguidist/pull/583?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/docs/Configuration.md b/docs/Configuration.md
index f6e26348..000c80ef 100644
--- a/docs/Configuration.md
+++ b/docs/Configuration.md
@@ -383,10 +383,10 @@ Function that modifies code example (Markdown fenced code block). For example yo
 
 ```javascript
 module.exports = {
-  updateExample: function(props) {
+  updateExample: function(props, exampleFilePath) {
     const { settings, lang } = props;
     if (typeof settings.file === 'string') {
-      const filepath = settings.file;
+      const filepath = path.resolve(exampleFilePath, settings.file);
       delete settings.file;
       return {
         content: fs.readFileSync(filepath),
diff --git a/loaders/examples-loader.js b/loaders/examples-loader.js
index 0c0783c5..6f29db3d 100644
--- a/loaders/examples-loader.js
+++ b/loaders/examples-loader.js
@@ -27,8 +27,11 @@ function examplesLoader(source) {
 		source = expandDefaultComponent(source, query.componentName);
 	}
 
+	const updateExample = config.updateExample
+		? props => config.updateExample(props, this.resourcePath)
+		: undefined;
 	// Load examples
-	const examples = chunkify(source, config.updateExample);
+	const examples = chunkify(source, updateExample);
 
 	// We're analysing the examples' source code to figure out the require statements. We do it manually with regexes,
 	// because webpack unfortunately doesn't expose its smart logic for rewriting requires


Test patch:
diff --git a/loaders/__tests__/examples-loader.spec.js b/loaders/__tests__/examples-loader.spec.js
index d170e6b1..0b1e9ad0 100644
--- a/loaders/__tests__/examples-loader.spec.js
+++ b/loaders/__tests__/examples-loader.spec.js
@@ -63,15 +63,19 @@ it('should pass updateExample function from config to chunkify', () => {
 	examplesLoader.call(
 		{
 			query: '?componentName=FooComponent',
+			resourcePath: '/path/to/foo/examples/file',
 			_styleguidist: {
 				updateExample,
 			},
 		},
 		exampleMarkdown
 	);
-	expect(updateExample).toBeCalledWith({
-		content: '<h1>Hello world!</h2>',
-		settings: { static: true },
-		lang: 'jsx',
-	});
+	expect(updateExample).toBeCalledWith(
+		{
+			content: '<h1>Hello world!</h2>',
+			settings: { static: true },
+			lang: 'jsx',
+		},
+		'/path/to/foo/examples/file'
+	);
 });