Created
June 27, 2019 22:40
-
-
Save trevorblades/b01d125449579dc4dd8e1965f48e602d to your computer and use it in GitHub Desktop.
gatsby-remark-transform-typescript
This file contains hidden or 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
| const visit = require('unist-util-visit'); | |
| const babel = require('@babel/core'); | |
| const ts = ['ts', 'tsx', 'typescript']; | |
| const babelOptions = { | |
| presets: [ | |
| [ | |
| '@babel/typescript', | |
| { | |
| allExtensions: true, | |
| isTSX: true | |
| } | |
| ] | |
| ] | |
| }; | |
| module.exports = ({markdownAST}) => { | |
| visit(markdownAST, 'code', node => { | |
| if (ts.includes(node.lang)) { | |
| try { | |
| const { code } = babel.transformSync(node.value, babelOptions); | |
| console.log(code); | |
| } catch (error) { | |
| console.log(error.message); | |
| } | |
| } | |
| }); | |
| return markdownAST; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment