Skip to content

Instantly share code, notes, and snippets.

@trevorblades
Created June 27, 2019 22:40
Show Gist options
  • Select an option

  • Save trevorblades/b01d125449579dc4dd8e1965f48e602d to your computer and use it in GitHub Desktop.

Select an option

Save trevorblades/b01d125449579dc4dd8e1965f48e602d to your computer and use it in GitHub Desktop.
gatsby-remark-transform-typescript
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