-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from github/kh-add-custom-component-support
[spike] Allow custom component to be mapped to element type
- Loading branch information
Showing
6 changed files
with
356 additions
and
4 deletions.
There are no files selected for viewing
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,36 @@ | ||
const {elementType, getProp, getPropValue} = require('jsx-ast-utils') | ||
|
||
/* | ||
Allows custom component to be mapped to an element type. | ||
When a default is set, all instances of the component will be mapped to the default. | ||
If a prop determines the type, it can be specified with `props`. | ||
For now, we only support the mapping of one prop type to an element type, rather than combinations of props. | ||
*/ | ||
function getElementType(context, node) { | ||
const {settings} = context | ||
const rawElement = elementType(node) | ||
if (!settings) return rawElement | ||
|
||
const componentMap = settings.github && settings.github.components | ||
if (!componentMap) return rawElement | ||
const component = componentMap[rawElement] | ||
if (!component) return rawElement | ||
let element = component.default ? component.default : rawElement | ||
|
||
if (component.props) { | ||
const props = Object.entries(component.props) | ||
for (const [key, value] of props) { | ||
const propMap = value | ||
const propValue = getPropValue(getProp(node.attributes, key)) | ||
const mapValue = propMap[propValue] | ||
|
||
if (mapValue) { | ||
element = mapValue | ||
} | ||
} | ||
} | ||
return element | ||
} | ||
|
||
module.exports = {getElementType} |
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
Oops, something went wrong.