Coder Social home page Coder Social logo

benneq / mdx-custom-elements-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from christopherbiscardi/mdx-custom-elements-example

0.0 0.0 0.0 389 KB

An example of using remark plugins to mark out areas for math or code processing, but not re-inject html, deferring processing to react components

License: MIT License

JavaScript 43.32% CSS 56.68%

mdx-custom-elements-example's Introduction

Warning

To run this example, you need to patch @mdx-js/mdx.

MDX Custom Elements Example

This example shows how to use remark plugins to parse sections of markdown into elements that can be replaced by an MDXProvider.

The big examples here are code-block, which successfully replaces trying to deal with <pre><code> blocks and math syntax, which uses inline-math and block-math.

read the blog post

The MDX patch

modify node_modules/@mdx-js/mdx/mdx-ast-to-mdx-hast.js to include the following handlers option.

const toHAST = require('mdast-util-to-hast')
const detab = require('detab')
const u = require('unist-builder')

+function mdxAstToMdxHast({ mdxAstHandlers }) {
  return (tree, _file) => {
    const handlers = {
      // `inlineCode` gets passed as `code` by the HAST transform.
      // This makes sure it ends up being `inlineCode`
      inlineCode(h, node) {
        return Object.assign({}, node, {
          type: 'element',
          tagName: 'inlineCode',
          properties: {},
          children: [
            {
              type: 'text',
              value: node.value
            }
          ]
        })
      },
      code(h, node) {
        const value = node.value ? detab(node.value + '\n') : ''
        const lang = node.lang
        const props = {}

        if (lang) {
          props.className = ['language-' + lang]
        }

        // Mdast sets `node.meta` to `null` instead of `undefined` if
        // not present, which React doesn't like.
        props.metastring = node.meta || undefined

        const meta =
          node.meta &&
          node.meta.split(' ').reduce((acc, cur) => {
            if (cur.split('=').length > 1) {
              const t = cur.split('=')
              acc[t[0]] = t[1]
              return acc
            }
            acc[cur] = true
            return acc
          }, {})

        if (meta) {
          Object.keys(meta).forEach(key => {
            props[key] = meta[key]
          })
        }

        return h(node.position, 'pre', [
          h(node, 'code', props, [u('text', value)])
        ])
      },
      import(h, node) {
        return Object.assign({}, node, {
          type: 'import'
        })
      },
      export(h, node) {
        return Object.assign({}, node, {
          type: 'export'
        })
      },
      comment(h, node) {
        return Object.assign({}, node, {
          type: 'comment'
        })
      },
      jsx(h, node) {
        return Object.assign({}, node, {
          type: 'jsx'
        })
      },
+     ...mdxAstHandlers
    }

    const hast = toHAST(tree, {
      handlers
    })

    return hast
  }
}

module.exports = mdxAstToMdxHast

mdx-custom-elements-example's People

Contributors

christopherbiscardi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.