-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: markdown to custom components #90
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
350bb5f
Base readme-preparser implementation
m7kvqbe1 f5e439d
Merge branch 'develop' into feature/markdown-to-custom-components
m7kvqbe1 fe37e84
Handle no table data gracefully
m7kvqbe1 ffea970
Add temporary layout fix
m7kvqbe1 3d78ecf
Add imports for presentational components
m7kvqbe1 e515bd7
Output CodeHighlighter code source and examples
m7kvqbe1 16a5f1e
Merge branch 'develop' into feature/markdown-to-custom-components
m7kvqbe1 42194a4
Change PropType to string and update test
m7kvqbe1 d712353
Update mdx propType to string
m7kvqbe1 4f541cb
Unescape some more stuff
m7kvqbe1 904ad20
Implement transformTableData and refactor
m7kvqbe1 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,67 @@ | ||
const { marksy } = require('marksy') | ||
const { getTables } = require('mdtable2json') | ||
const ReactDOMServer = require('react-dom/server') | ||
const { createElement } = require('react') | ||
|
||
/** | ||
* Transform table markdown AST (rows and cells) into data | ||
* structure that can be consumed by the `<DataTable />` component | ||
* | ||
* NOTE: The implementation is limited to 1 table per `README.md` | ||
* | ||
* @param {array} children | ||
* @returns {array} | ||
*/ | ||
function transformTableData(markdown) { | ||
const tables = getTables(markdown) | ||
|
||
return (tables.length >= 1 && tables[0].json) || [] | ||
} | ||
|
||
/** | ||
* Base64 encode a prop so that it doesn't confuse the MDX parser | ||
* | ||
* @param {string} string | ||
* @returns {string} | ||
*/ | ||
function encodeProp(string) { | ||
return Buffer.from(string).toString('base64') | ||
} | ||
|
||
/** | ||
* Generates a compiler to create an MDX version of a `README.md` | ||
* | ||
* TODO: Use JSX instead of createElement and transpile at runtime | ||
* | ||
* @param {string} markdown | ||
* @param {object} options | ||
* @returns {function} | ||
*/ | ||
function compile(markdown, options) { | ||
const tableData = transformTableData(markdown) | ||
|
||
return marksy({ | ||
createElement, | ||
elements: { | ||
code({ language, code }) { | ||
return createElement('CodeHighlighter', { | ||
example: encodeProp(code), | ||
source: encodeProp(code), | ||
language: language || 'javascript', | ||
}) | ||
}, | ||
table() { | ||
return createElement('DataTable', { | ||
caption: 'Props', | ||
data: encodeProp(JSON.stringify(tableData)), | ||
}) | ||
}, | ||
}, | ||
})(markdown, options) | ||
} | ||
|
||
exports.convertToMDX = markdown => { | ||
const { tree } = compile(markdown, {}) | ||
|
||
return ReactDOMServer.renderToStaticMarkup(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
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,3 @@ | ||
.post-article { | ||
width: 0; | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a temporary layout fix (hack). The
post-article
component needs styling from the ground up.