-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement transformTableData and refactor
- Loading branch information
Showing
5 changed files
with
390 additions
and
45 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 |
---|---|---|
@@ -1,39 +1,67 @@ | ||
const { createElement } = require('react') | ||
const { marksy } = require('marksy') | ||
const { getTables } = require('mdtable2json') | ||
const ReactDOMServer = require('react-dom/server') | ||
const { createElement } = require('react') | ||
|
||
/** | ||
* TODO: Transform table markdown AST (rows and cells) into data | ||
* 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(children) { | ||
return [] // debug | ||
return children | ||
function transformTableData(markdown) { | ||
const tables = getTables(markdown) | ||
|
||
return (tables.length >= 1 && tables[0].json) || [] | ||
} | ||
|
||
const compile = marksy({ | ||
createElement, | ||
elements: { | ||
code({ language, code }) { | ||
return createElement('CodeHighlighter', { | ||
example: escape(code), | ||
source: escape(code), | ||
language: language || 'javascript', | ||
}) | ||
}, | ||
table({ children }) { | ||
return createElement('DataTable', { | ||
caption: 'Props', | ||
data: transformTableData(children), | ||
}) | ||
/** | ||
* 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
Oops, something went wrong.