-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
340 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from 'react'; | ||
import { camelCase } from 'lodash'; | ||
import Para from 'rsg-components/Para'; | ||
import PlaygroundError from 'rsg-components/PlaygroundError'; | ||
import Playground from 'rsg-components/Playground'; | ||
import { useMdxContext } from '../MdxContext'; | ||
import MdxHighlight from '../MdxHighlight'; | ||
import MdxCodeStatic from '../MdxCodeStatic'; | ||
import * as Rsg from '../../../../typings'; | ||
|
||
interface Props extends Rsg.Modifiers { | ||
id: string; | ||
} | ||
|
||
// XXX: We're assuming that all imported stories are located in a .story.js file | ||
// next to the Mdx file with the same name, so we're ignoring all but the very | ||
// last part of the story ID, which is the name of the exported from the CSF file | ||
// function | ||
const getLocalExampleNameById = (id: string) => camelCase(id.replace(/^.*?--/, '')); | ||
|
||
export default function Story({ id, ...modifiers }: Props) { | ||
const { | ||
componentName, | ||
exampleMode, | ||
documentScope, | ||
exampleScope, | ||
namedExamples, | ||
} = useMdxContext(); | ||
|
||
const code = namedExamples[getLocalExampleNameById(id)]; | ||
if (!code) { | ||
return ( | ||
<Para> | ||
<PlaygroundError message={`Named example \`${id}\` not found in a story file`} /> | ||
</Para> | ||
); | ||
} | ||
|
||
if (modifiers.static) { | ||
return ( | ||
<MdxCodeStatic> | ||
<MdxHighlight>{code}</MdxHighlight> | ||
</MdxCodeStatic> | ||
); | ||
} | ||
|
||
return ( | ||
<Playground | ||
code={code} | ||
componentName={componentName || ''} // TODO | ||
exampleMode={exampleMode} | ||
documentScope={documentScope} | ||
exampleScope={exampleScope} | ||
modifiers={modifiers} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Parent, Node } from 'unist'; | ||
|
||
/** | ||
* Import React to make it available in the Mdx document scope | ||
* | ||
* // ... | ||
* | ||
* -> | ||
* | ||
* | ||
* import React from 'react' | ||
* // ... | ||
*/ | ||
export default () => (treeRaw: Node) => { | ||
const tree = treeRaw as Parent; | ||
|
||
// Generate import | ||
tree.children.push({ | ||
type: 'import', | ||
value: `import React from 'react'`, | ||
}); | ||
|
||
return tree; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { uniq } from 'lodash'; | ||
import { Parent, Node } from 'unist'; | ||
|
||
/** | ||
* Deduplicate import nodes. | ||
* | ||
* import React from "react" | ||
* import Button from './Button'; | ||
* import React from 'react' | ||
* | ||
* -> | ||
* | ||
* import React from 'react' | ||
* import Button from './Button' | ||
* | ||
* XXX: This is a very naïve approach and will only deduplicate completely | ||
* identical statements (ignoring quotes and semicolons). | ||
*/ | ||
export default () => (treeRaw: Node) => { | ||
const tree = treeRaw as Parent; | ||
|
||
const importNodes = tree.children.filter((n) => n.type === 'import'); | ||
|
||
// `import` Mdx node can contain multiple import statements, so join/split | ||
// to have each statement separately | ||
const importStatements = importNodes | ||
.map((x) => x.value) | ||
.join('\n') | ||
.split('\n'); | ||
|
||
// Normalize semicolons and quotes | ||
const normalizedImportStatements = importStatements.map((x) => | ||
x.replace(/;\s*$/, '').replace(/"/g, '/') | ||
); | ||
|
||
// Remove duplicates | ||
const uniqImportStatements = uniq(normalizedImportStatements); | ||
|
||
// Replace all import nodes with deduplicated ones | ||
return { | ||
...tree, | ||
children: [ | ||
...tree.children.filter((n) => n.type !== 'import'), | ||
...uniqImportStatements.map((value) => ({ | ||
type: 'import', | ||
value, | ||
})), | ||
], | ||
}; | ||
}; |
Oops, something went wrong.