How to use the @mdx-js/mdx.sync function in @mdx-js/mdx

To help you get started, we’ve selected a few @mdx-js/mdx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github openmultiplayer / homepage / next.config.js View on Github external
const extractMdxMeta = (content) => {
  let meta = {};
  mdx.sync(content, {
    remarkPlugins: [
      () => (tree) => {
        visit(tree, 'export', (node) => {
          const ast = parse(node.value, {
            plugins: ['jsx'],
            sourceType: 'module'
          });
          traverse(ast, {
            VariableDeclarator: (path) => {
              if (path.node.id.name === 'meta') {
                // eslint-disable-next-line no-eval
                meta = eval(`module.exports = ${generate(path.node.init).code}`);
              }
            }
          });
        });
github hiroppy / fusuma / packages / mdx-loader / src / index.js View on Github external
function mdxLoader(src) {
  const { math } = getOptions(this);
  const cb = this.async();
  const remarkPlugins = math
    ? [emoji, require('remark-math'), require('remark-html-katex') /* avoid warnings */, mdxPlugin]
    : [emoji, mdxPlugin];
  const result = mdx.sync(src, {
    remarkPlugins
  });

  cb(null, result);
}
github frontarm / mdx-util / packages / mdx.macro / mdx.macro.js View on Github external
)
  }
  let documentPath = path.join(filename, '..', documentFilename);
  let imports = `import React from 'react'\nimport { MDXTag } from '@mdx-js/tag'\n`

  // In development mode, we want to import the original document so that
  // changes will be picked up and cause a re-build.
  // Note: this relies on files with macros *not* being cached by babel.
  if (process.env.NODE_ENV === "development") {
    imports += `import '${documentPath}'\n`
  }

  let source = fs.readFileSync(documentPath, 'utf8');
  let transformedSource =
    babel.transformSync(
      imports+mdx.sync(source),
      {
        presets: [babelPresetReactApp],
        filename: documentPath,
      },
    ).code

  return writeTempFile(documentPath, transformedSource)
}
github frontarm / mdx-util / packages / mdx.macro / mdx.macro.js View on Github external
function inlineMDX({
  babel,
  referencePath,
  state,
}) {
  let { file: { opts: { filename } } } = state
  let program = state.file.path

  let rawCode = referencePath.parent.quasi.quasis[0].value.raw
  let transformedSource = mdx.sync(rawCode).replace('export default', '')

  // Need to parse the transformed source this way instead of
  // with babel.parse or babel.transform, as otherwise the
  // generated code has errors. I'm not sure why.
  let ast = parse(
    transformedSource,
    {
      plugins: ['jsx', 'objectRestSpread'],
      sourceType: "module",
      sourceFilename: filename,
    },
  )

  function visitImport(path) {
    let name = path.node.local.name
    var binding = path.scope.getBinding(name)
github ChristopherBiscardi / gatsby-mdx / ui-extensions / contentful / index.js View on Github external
getJSXStringFromMDX(content) {
    return mdx.sync(content).toString();
  }
github mdx-js / mdx / packages / gatsby-theme-mdx / src / components / playground-editor.js View on Github external
const getOutputs = src => {
  let jsx = ''
  let mdast = {}
  let hast = {}

  try {
    jsx = mdx.sync(src, {
      skipExport: true,
      remarkPlugins: [
        () => ast => {
          mdast = ast
          return ast
        }
      ],
      rehypePlugins: [
        () => ast => {
          hast = ast
          return ast
        }
      ]
    })
  } catch (error) {
    return {error}
github mdx-js / mdx / packages / gatsby-theme-mdx / src / components / playground-editor.js View on Github external
const transformCode = src => {
  let transpiledMDX = ''

  try {
    transpiledMDX = mdx.sync(src, {
      skipExport: true,
      remarkPlugins: [removeImports, removeExports]
    })
  } catch (e) {
    return ''
  }

  return `
    ${transpiledMDX}

    render(
      
        
      
    )
  `
github remoteinterview / zero / packages / handler-react / renderer / mdx-override.js View on Github external
const transform = (code, filename) => {
  let jsxWithMDXTags = mdxTransform(code);

  let jsx = `
    import { MDXTag } from "@mdx-js/tag"

    ${jsxWithMDXTags}
  `;

  let result = babelTransform(jsx, { ...babelConfig, filename });
  return result.code;
};