christianalfoni / markdown-to-react-components

Convert markdown into react components
MIT License
132 stars 16 forks source link

Multiple line code transformations using MTRC #2

Open dschalk opened 8 years ago

dschalk commented 8 years ago

I am trying to put code such as

class MonadObject {
  constructor(ob) {

    this.x = mobservable.makeReactive(ob);

    this.bnd = (func, ...args) => {
      return func(this.x, ...args);
    };

    this.ret = w => {
      Object.assign(this.x, w);
      return this;
    }
  }
}

on a web page. I don't know how to put the whole snippet in quotes, other than concatenating line by line. Here is what I see in the Chrome browser console log after I insert the code in a running app:

code: "class Monad {↵ constructor(z) {↵↵ this.x = mobservable.makeReactive(z);↵↵ this.bnd = (func, ...args) => {↵ return func(this.x(), ...args);↵ };↵↵ this.ret = a => {↵ this.x(a);↵ return this;↵ };↵ }↵};"

Firefox shows the same thing I get when I put the Chrome text in markdown, namely:

class MonadObject {  constructor(ob) {    this.x = mobservable.makeReactive(ob);    this.bnd = (func, ...args) => {      return func(this.x, ...args);    };    this.ret = w => {      Object.assign(this.x, w);      return this;    }  }}

I might soon have use for the full functionality of "markdown-to-react-components", but right now I don't need anything dynamic or interactive. I just want "this.state.content = ?" where ? never changes. I am trying "MTRC(x).tree, but I don't know how to substitute my "Monad" code for "x".

The examples show only single-line arguments for MTRC. If there is a way to use it with multiple lines of text, I think that would be of general interest. Or maybe I have a blind spot and everybody else already knows how to handle this.

Thanks to anybody who can point me in the right direction. This is great software and I feel confident that it is quite capable of handling my use case. But how?

christianalfoni commented 8 years ago

Remember that you have to use the triple ticks and javascript and end with triple ticks again. Just like you did to write the example in the issue above. Is that the issue? You can use multiline strings with ES6:


const myCode = `
```javascript
class MonadObject {
  constructor(ob) {

    this.x = mobservable.makeReactive(ob);

    this.bnd = (func, ...args) => {
      return func(this.x, ...args);
    };

    this.ret = w => {
      Object.assign(this.x, w);
      return this;
    }
  }
}

`

christianalfoni commented 8 years ago

Look at source of issue, kinda hard explaining this as github parses everything :p