GithubHelp home page GithubHelp logo

jxnblk / mdx-go Goto Github PK

View Code? Open in Web Editor NEW
363.0 5.0 28.0 1.52 MB

:zap: Lightning fast MDX-based dev server for progressive documentation

Home Page: https://mdx-go.now.sh

License: MIT License

JavaScript 99.35% Dockerfile 0.65%
react mdx markdown js zero-config development documentation design style-guide design-systems isolated ui testing

mdx-go's Introduction

mdx-go

⚡ Lightning fast MDX-based dev server for progressive documentation

https://mdx-go.now.sh

Build Status Downloads Version MIT License

npm i -g mdx-go
  • 0️⃣ Zero-config dev server
  • 📝 Write in markdown
  • ⚛️ Import and use React components
  • 📁 File-system based routing
  • 📐 Customizable layouts
  • 🌐 Export as static HTML
  • 👩‍🎤 Support for styled-components & emotion
  • 🔓 Avoid lock-in and easily migrate to other MDX-based tools

Getting Started

Create a docs folder and docs/index.mdx file.

import MyComponent from '../src'

# Component Demo

<MyComponent
  beep='boop'
/>

Start the dev server on the docs folder:

mdx-go docs

npm run scripts

Alternatively, mdx-go can be installed as a development dependency and used with run scripts in your package.json.

"scripts": {
  "dev": "mdx-go docs",
  "docs": "mdx-go build docs"
}
npm run dev

Motivation

mdx-go is built with the idea of Progressive Documentation in mind, intended to be used anywhere as a dev server, prototyping tool, or simple static site generator. By embracing the MDX file format, the docs you create with mdx-go can easily be used in other tools. Start your docs with mdx-go and migrate to tools like Next.js and Gatsby when needed. You can even keep mdx-go around to use as a dev tool outside of other React applications.

Using MDX

MDX combines the simplicity of markdown with the ability to import and use React components inline.

Write markdown like you normally would.

# Hello

Import and use React components inline.

import { Box } from 'grid-styled'

# Hello

<Box p={3} bg='tomato'>
  This is a React component!
</Box>

To learn more about using MDX, see the MDX docs.

Routing

Each MDX file in the target directory will become its own route, with index.mdx serving as the base route, i.e. /.

With the following directory structure:

docs/
  index.mdx
  getting-started.mdx
  api.mdx

mdx-go will create routes for /, /getting-started, and /api.

mdx-go also supports using React components as routes for files with the .js extension. Be sure that the .js file exports a default component to render as a route.

Layouts

mdx-go includes a default layout that centers the document in the viewport, but custom layout components can be added both globally and per-route.

To render a custom layout for a single route, export a component as the default from the MDX file. This is a built-in feature of MDX.

import Layout from './Layout'

export default Layout

# Page with layout

To wrap all routes with a custom layout, export a Root component from your index.mdx file. This will completely disable the built-in centered layout. Note: this only works in the index route, not other routes.

export { Root } from './Root'

# Root layout for all routes

Head Content

Head contents can be set per-route by using the Head component.

import { Head } from 'mdx-go'

<Head>
  <title>Page title</title>
</Head>

# Page with title

To set head contents for all routes, use the Head component within a Root component.

Custom MDX Components

To customize the HTML components rendered from MDX, use the ComponentProvider in a Root component.

// example Root component
import React from 'react'
import { ComponentProvider } from 'mdx-go'

const components = {
  h1: props => <h1 {...props} style={{ fontSize: 48 }} />,
}

export const Root = props =>
  <ComponentProvider components={components}>
    {props.children}
  </ComponentProvider>

Ensure the Root component is exported from index.mdx

export { Root } from './Root.js'

Custom File Match Pattern

To specify a custom file pattern for matching against, export a files webpack context from the main index.mdx file.

export const files = require.context('../src', true, /\.example\.js$/, 'lazy')

Theming

By default mdx-go includes virtually no styling. To customize the styles, use components to wrap MDX with a Root component and use the MDXProvider to change the default styles.

Exporting

To export as a static site with HTML and JS bundles, run:

mdx-go build docs

This will export all routes as HTML in a dist folder. See CLI Options for configuration options.

CSS-in-JS

mdx-go does not use any CSS-in-JS libraries internally, and most libraries will work when using the dev server. To extract static CSS when using the build command, ensure you have either styled-components or emotion installed locally in your package.json. For Emotion, be sure that emotion-server is also installed.

When either of these libraries are detected in your package.json dependencies, mdx-go will extract static CSS during the build process.

CLI Options

The following flags can be passed to the CLI.

  -p --port     Port for dev server
  --no-open     Disable opening in default browser
  -d --out-dir  Output directory for static export
  --basename    Base path for routing
  --static      Export HTML without JS bundle
  --webpack     Path to custom webpack config

All CLI options can also be specified in a mdx-go field in your package.json.

"mdx-go": {
  "outDir": "site"
}

Custom webpack config

mdx-go will automatically pick up a webpack.config.js if it exists in the current working directory. A custom path can be passed to the CLI using the --webpack flag. The provided webpack config will be merged with the built-in config using webpack-merge.

Examples

Related

MDX | mdx-deck | mdx-docs | ok-mdx | x0

MIT License


mdx-go's People

Contributors

jxnblk avatar lachlanjc avatar nickcox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mdx-go's Issues

Support Sub-Resources URLs for images

Would be great, if you could put images you'd like to refer to in an article right inside the folder where the file for the article is and then refer to it with a sub-resource URL as described here under "Relative URLs": https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL#examples_of_relative_urls

You already support such Routes for links to other articles, but not for images, so it would also be consistent.

File Structure:

- root
  - main
    - sub
      - index.mdx
      - image.png

image markdown:

![my image](image.png)

Exporting custom path in md component does not work

Hi, First of all, thank you for your awesome work on all the libs!

I noticed the export const path = '...' doesn't seem to work for me. I checked the code and by the looks of it, you only add custom exports when it's the Index component. And the exports are loaded in promise, so the path would not get there anyway I guess.

https://github.com/jxnblk/mdx-go/blob/master/lib/client/routes.js#L58

I am trying to achieve custom and nested links with custom menu, so that's why I needed this. I really like mdx-go and was avoiding using Gatsby, because this was more than sufficient for my needs.

export 'DocsLayout' was not found in 'mdx-go'

The documentation pages mention DocsLayout, yet when trying to import it, I'm being confronted with the error "export 'DocsLayout' was not found in 'mdx-go', and this repo doesn't seem to have any component defined with that name.

Compile to MD

Thanks for this amazing library! I have one question though: Can I export to .md files instead of html + js + css?

Pagination cause `staticContext` react DOM warning

Not sure why, after add a new page, dev console have this error message

react-dom.development.js:492 Warning: React does not recognize the `staticContext` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `staticcontext` instead. If you accidentally passed it from a parent component, remove it from the DOM element.
    in a (created by Link)
    in Link (created by Route)
    in Route (created by NavLink)
    in NavLink
    in Unknown (created by Styled(Component))
    in Styled(Component)
    in div (created by Styled(div))
    in Styled(div)
    in Unknown (created by Route)
    in Route (created by withRouter())
    in withRouter() (created by Root)
    in div (created by Styled(div))
    in Styled(div) (created by Main)
    in div (created by Styled(div))
    in Styled(div) (created by Main)
    in Main (created by Root)
    in div (created by Styled(div))
    in Styled(div) (created by Layout)
    in Layout (created by Root)
    in Root (created by Route)
    in Route (created by withRouter(Root))
    in withRouter(Root) (created by App)
    in MDXProvider (created by App)
    in HeadProvider (created by App)
    in App
    in Router (created by BrowserRouter)
    in BrowserRouter

After removing Pagination from my layout, the error message has gone.

I believe that Pagination component should do the same thing as NavLinks to pull staticContext props, like

Pagination = withRouter(({
  routes = [],
  order = [],
  filter,
  history,
  location,
  match,
  staticContext
  ...props
})

Custom File Match

Attempting to use a custom file matching pattern to only process .mdx files and ignore .js but not having any luck.

I've added:
export const files = require.context('.', true, /\.mdx$/, 'lazy')

To the root level index.mdx file but it is still attempting to process .js files.

What might I be doing incorrectly?

Thanks for your help.

TOC links lead to .md domain instead of a sibling file

I have a doc directory, with:

  • blank index.mdx
  • some docs in build.md

mdx-go renders the default page as:

Project Documentation
TOC

  • <li href="//build.md"> build

I imagine that ./build.md or simply build.md was intended in the link target 🤔

Edit: actually, ./build or build is intended, because that renders markdown, while build.md shows raw file...

Is there a way to replace the template used in HTMLPlugin?

I've got a script that needs to be loaded in the head before the <App /> is rendered. The library I'm trying to load a sets a global variable. When trying to pass that variable as a prop to a component in the MDX file, timing conflicts with <Head /> result in the variable being undefined during the time that the component is processing. This seems like a race condition. Shouldn't the <Head /> component in <Root /> always be resolved first?

However, when I hack up the defaultTemplate in HTMLPlugin to hard-code my script in the actual head, then the page works as expected.

Therefore, it seems like I need to replace the HTMLPlugin's template with my own custom version, but I don't see a way to do that through the local webpack.config.js.

My dirty workaround is to install mdx-go into the local project dir, and apply a patch directly to defaultTemplate. I'd like to avoid that if possible.

Any ideas?

ReferenceError: window is not defined when building

Hi Brent,

Great piece of work. Upon building I get the error ReferenceError: window is not defined when using the window object. It's not obvious what might cause this, I guess it's related to the server side render when building.

Typescript support?

I tried to import a tsx/ts component, it didn't work, and couldn't figure out how to make it work. Do you think this should be supported by default?

build --static still adds prefetch links to head

Im running mdx-go build pages --static but the output html files still get

    <link rel='prefetch' as='script' href='/main.js'><link rel='prefetch' as='script' href='/1.main.js'><link rel='prefetch' as='script' href='/2.main.js'><link rel='prefetch' as='script' href='/3.main.js'><link rel='prefetch' as='script' href='/4.main.js'>

appended to the head tag. Is there a way to prevent this when building static? I'm trying to use mdx-go to create HTML emails and this tag is useless to that.

Also static still builds {number}.main.js files though I just need the HTML and zero JS files.

Failed to compile, unexpected character '#'

I've been trying to get this to work for a while now but, unfortunately, I haven't had the luck.
Firstly, the directory structure looks like this:

├── docs
│   └── res.mdx
├── node_modules
│   ├── bloomer
│   ├── classnames
│   └── tslib
├── package.json
└── package-lock.json

I cd to this directory and run mdx-go docs and get the following error:

➜ mdx-go docs 
[mdx-go] starting dev server...
✔ success [mdx-go] compiled in 1s 848ms


 ERROR  Failed to compile with 1 errors                                 19:30:57

 error  in /home/karan/.nvm/versions/node/v8.11.4/lib/node_modules/mdx-go/lib/client/keyboard-shortcuts.md

Module parse failed: Unexpected character '#' (2:0)
You may need an appropriate loader to handle this file type.
| 
> ### Keyboard Shortcuts
| 
| - `/`: Show directory listing

 @ /home/karan/.nvm/versions/node/v8.11.4/lib/node_modules/mdx-go/lib/client/routes.js 11:0-56 44:26-43
 @ /home/karan/.nvm/versions/node/v8.11.4/lib/node_modules/mdx-go/lib/client/App.js
 @ /home/karan/.nvm/versions/node/v8.11.4/lib/node_modules/mdx-go/lib/client/index.js
 @ multi webpack-hot-middleware/client /home/karan/.nvm/versions/node/v8.11.4/lib/node_modules/mdx-go/lib/client/index.js

✖ 「wdm」: 
ERROR in ./client/keyboard-shortcuts.md 2:0
Module parse failed: Unexpected character '#' (2:0)
You may need an appropriate loader to handle this file type.
| 
> ### Keyboard Shortcuts
| 
| - `/`: Show directory listing
 @ ./client/routes.js 11:0-56 44:26-43
 @ ./client/App.js
 @ ./client/index.js
 @ multi webpack-hot-middleware/client ./client/index.js
[mdx-go] listening on http://localhost:8080
^C

Any idea why it's breaking due to a file that isn't even present in the directory? Is there something wrong on my end that I need to change? Please do let me know.

Copy images to build folder

Hey,

I used mdx-go to spin up my personal site, using my repo's README as the content. The same as you did for yours!

I love the simplicity and how easy it is to maintain. Thanks for that!

I was wondering, is there a way to copy images from src to dist?

I imagine if instead of using mdx-go I used mdx with the webpack loader I could do this, but I love how tooling-less mdx-go is.

Thanks! 👊

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.