Skip to content

Conversation

@Daniel15
Copy link
Member

Simple HTML to JSX converter, built during Hackathon 40 at Facebook. See /react/html-jsx.html. Not directly linked from the site yet as there may still be some minor issues with it.

I'll eventually split html-jsx-lib.js into another project (react-tools?) as it could be used elsewhere. I think it'd work with Node.js if a module like jsdom was used to implement the DOM functionality.

See /react/html-jsx.html. Not directly linked from the site yet as there may still be some minor issues with it.
@jordwalke
Copy link
Contributor

very useful tool!

@Daniel15
Copy link
Member Author

Thanks! I've got a Handlebars to JSX converter that's almost complete as well, I'll post it once I finish it :)

I also had a proof-of-concept bookmarklet that magically made a server-rendered app into a live updating app. Basically it converted document.body to JSX and used renderComponent to set the initial state. Then, whenever a link is clicked or a form is submitted, the target page is AJAX loaded and React renderComponent is used to render it over the top. There's already solutions like Ajaxify that do this, but with React you also get the benefit of it only updating the DOM nodes that need it (rather than just throwing away document.body like ajaxify does) and as a result you can use things like CSS transitions :)

@jordwalke
Copy link
Contributor

If you want to take the markup converter to the extreme, you could make it so that when you select markup in any page (or Chrome inspector tools), not only can you create a React component out of the markup, but it also captures the set of css rules needed to render it in its exact form, and generates an accompanying style sheet! @sebmarkbage can help you out there as he's gained quite a lot of knowledge of the Chrome inspector and Chrome plugins.

@Daniel15
Copy link
Member Author

@jordwalke That does sound like an interesting idea :). I'll keep it in mind. It'd make a good addition to the React inspector plugin that's currently Facebook-only. Someone mentioned it on IRC recently and last I heard it was going to be released as open-source shortly.

@jordwalke
Copy link
Contributor

@Daniel15 That sounds likely. I remember there was already a plugin that helped you do something like that in the Chrome store - not sure if it's open source, but if it were, it would not be hard to fork it to simply run the output through your markup transforming code (and boiler plate generator). I imagine that would be quite an impressive demo.

@jordwalke
Copy link
Contributor

This one is open source and should integrate nicely into our React developer tools!
https://github.com/kdzwinel/SnappySnippet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we took document as a param (rather than relying on the global) we could use this from node with jsdom

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea 👍
I wanted to get this version merged in, and do the work to get it fully working with both Node.js and the website in a separate commit / pull request.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to mention it works with jsdom as is:

var fs = require("fs");
var jsdom = require("jsdom");

var htmljsx = fs.readFileSync("docs/js/html-jsx-lib.js","utf-8");

jsdom.env({
url:"http://facebook.com/",
src: [htmljsx],
done: function(errors,window){
      HTMLtoJSX = window.HTMLtoJSX;
      var converter = new HTMLtoJSX({
        outputClassName: "MyClass",
        createClass: true
      });
      console.log(converter.convert("<!doctype html><html></head><body><p><input></p></body></html>"));
}
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know! I'm currently on vacation but I'll look at adding a simple
command-line version to react-tools once I'm back and get some free time.

Sent from my mobile.
On Dec 29, 2013 4:03 PM, "jhiswin" notifications@github.com wrote:

In docs/_js/html-jsx-lib.js:

  • reset: function() {
  •  this.output = '';
    
  •  this.level = 0;
    
  • },
  • /**
  • \* Main entry point to the converter. Given the specified HTML, returns a
    
  • \* JSX object representing it.
    
  • \* @param {string} html HTML to convert
    
  • \* @return {string} JSX
    
  • */
    
  • convert: function(html) {
  •  this.reset();
    
  •  // It turns out browsers have good HTML parsers (imagine that).
    
  •  // Let's take advantage of it.
    
  •  var containerEl = document.createElement('div');
    

Just want to mention it works with jsdom as is:

var fs = require("fs");
var jsdom = require("jsdom");

var htmljsx = fs.readFileSync("./html-jsx-lib.js","utf-8");

jsdom.env({
url:"http://sip1.idiil.org/",
src: [htmljsx],
done: function(errors,window){
HTMLtoJSX = window.HTMLtoJSX;
var converter = new HTMLtoJSX({
outputClassName: "MyClass",
createClass: true
});
console.log(converter.convert("<!doctype html>

"));
}
});


Reply to this email directly or view it on GitHubhttps://github.com//pull/667/files#r8579909
.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey while you're at it, I'd love to see the Handlebars-JSX converter and that nifty "Reactify" bookmarklet you were talking about ;)
Definitely a lot of possibilities with that code.

@petehunt
Copy link
Contributor

I am inclined to merge this. @zpao do you have any reservations?

@zpao
Copy link
Member

zpao commented Dec 16, 2013

We should merge to master and cherry-pick to stable branch, otherwise we'll lose this next time we cut a branch. No idea if it's possible to edit a PR, so we can either keep this and pull in the commit manually or you can close this PR and open a new one.

@Daniel15
Copy link
Member Author

@jordwalke That does look interesting, I'll look into it some time :)

@zpao I'd have to rebase the commit to get it on top of master. I was having issues getting the JSX compiler page working on master (raised as #666) but that may just be an environment issue. We should be able to cherry pick this commit to master though (same thing but the other way). How are bug fixes normally handled?

@zpao
Copy link
Member

zpao commented Dec 16, 2013

We normally land on master and merge into the stable branch (0.5-stable right this second but very shortly not). With the 0.8 branch existing now, that's what the next docs will be cut from. We could cherry-pick either way though, just makes it's a bit easier to not push it an effectively done branch.

@Daniel15
Copy link
Member Author

I'll rebase this on master instead (after retesting #666) and send it in a separate pull request (or update this one if possible? I don't really know how to Github).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants