0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00
logto/packages/console/parcel-transformer-mdx2.js
Gao Sun 8fa440579b
refactor: update nuxt guide (#6114)
* refactor: update nuxt guide

* refactor: polish content
2024-06-27 05:06:44 +00:00

59 lines
1.4 KiB
JavaScript

// https://github.com/parcel-bundler/parcel/pull/7922#issuecomment-1750704973
import { compile } from '@mdx-js/mdx';
import { default as ThrowableDiagnostic } from '@parcel/diagnostic';
import { Transformer } from '@parcel/plugin';
import rehypeMdxCodeProps from 'rehype-mdx-code-props';
export default new Transformer({
async transform({ asset }) {
const source = await asset.getCode();
let codeVFile;
try {
codeVFile = await compile(source, {
development: true,
jsx: true,
providerImportSource: '@mdx-js/react',
rehypePlugins: [[rehypeMdxCodeProps, { tagName: 'code' }]],
});
} catch (error) {
const { start, end } = error.position;
const highlight = {
message: error.reason,
start,
end,
};
if (!(end.line && end.column)) {
highlight.end = { ...start };
}
// Adjust for parser and reporter differences
highlight.start.column -= 1;
highlight.end.column -= 1;
throw new ThrowableDiagnostic({
diagnostic: {
message: 'Unable to compile MDX',
codeFrames: [
{
filePath: asset.filePath,
code: source,
codeHighlights: [highlight],
},
],
},
});
}
const code = String(codeVFile);
asset.type = 'jsx';
asset.setCode(code);
return [asset];
},
});