question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing complex content into a react component into an mdx document.

See original GitHub issue

Not sure where on the internet to ask this, so I figured I would start where it’s trying to be done and see of anybody here has any ideas or resources to lead me towards. I’m desiring to make react components that can be used in mdx documents where I can feed them content like the following, which is sort of a simple YAML like syntax.

<PracticeSchedule>
  Week 1: 
    Day 1:
      Play all 12 notes, starting with the note C
    Day 2:
      Play all 12 notes, starting with the note G
    Day 3:
      Play all 12 notes, starting with the note D
    Day 4:
      Play all 12 notes, starting with the note A
    Day 5:
      Play all 12 notes, starting with the note E
    Day 6:
      Play all 12 notes, starting with the note B
    Day 7:
      Rest
  Week 2: 
    Day 1:
      Play all 12 notes, starting with the note F#/Gb
    Day 2:
      Play all 12 notes, starting with the note C#/Db
    Day 3:
      Play all 12 notes, starting with the note G#/Ab
    Day 4:
      Play all 12 notes, starting with the note D#/Eb
    Day 5:
      Play all 12notes, starting with the note A#/Bb
    Day 6:
      Play all 12 notes, starting with the note F
    Day 7:
      Rest
</PracticeSchedule>

The preceding would be the desired author experience for a react component I’m working on that is a practice schedule for piano. I originally made a version of this in vanilla HTML and CSS and I tried tables and lists then but they didn’t quite work for me in the styling department for various reasons, so I ended up making a series of divs with custom styling that ended up doing the job, but was all hand coded. So I desire to turn all that mess into a react component and feed it data in the preceeding manner, or something similar, that is keeping with the spirit of Markdown/YAML/MDX. I suppose I could feed the components props of arrays and objects filled with strings, but I would like to be able to keep the simple YAML/Markdown feel, even inside an instance of a react component. Does anybody know if something like this is even possible to do, and if so, would it be too much time/effort/trouble to implement?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ChristopherBiscardicommented, Mar 19, 2019

if you go the child manipulation route, you could also use something likejsYaml in the react component itself to parse children from a string. It’d require the `` syntax for strings though.

<PracticeSchedule>{`
Week 1: 
  Day 1:
}`</PracticeSchedule>
3reactions
johnocommented, Mar 19, 2019

This isn’t very easy to achieve right now since MDX doesn’t support embedding MD syntax inside JSX blocks. However, you can achieve something similar by breaking out the PracticeSchedule content into its own file:

index.mdx

import Content from './some-content.mdx'

# Hello, world!

<Content />

some-content.mdx

import styled from 'styled-components'

export default styled.div`
  & ul {
    color: tomato;
  }
  & ul ul {
    color: rebeccapurple;
  }
`

- Item one
  - Item one a
  - Item one b
- Item two
  - Item two a
  - Item two b

Then, in some-content.mdx you could import PracticeSchedule and export it as the default.

You could also manipulate the children passed to PracticeSchedule since it will just be a string, but the newline information for your syntax will be lost because JSX, similarly to HTML (or at least most tags/elements), isn’t whitespace sensitive.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MDX and React - Docusaurus
To define any custom component within an MDX file, you have to export it: only paragraphs that start with export will be parsed...
Read more >
Accessing Props in MDX - Chris Biscardi
Since MDX is "just a React component" we can leverage the props that get passed in for this. Lets say we have some...
Read more >
React Markdown component: the easy way to create rich text
It lets you create rich content without having to worry about all the formatting and code ... Loading a markdown file into a...
Read more >
Using MDX
This article explains how to use MDX files in your project. It shows how you can pass props and how to import, define,...
Read more >
(Gatsby) How to pass image source as a prop in MDX ...
In other words, your MDX can receive a src to be used in a <img> tag or you can use GatsbyImage component if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found