Syntax highlighting for instructions markdown!#1000
Syntax highlighting for instructions markdown!#1000outoftime merged 3 commits intopopcodeorg:masterfrom
Conversation
|
UPDATE: The deprecation warning fix in |
|
@inlinestyle so I’m definitely not in love with the idea of using Other high-level feedback is that LMK your thoughts on the above—definitely open to counterarguments : ) |
|
@outoftime I agree that there's a lot in import markdown from '../util/markdown';
...
export default class Instructions extends React.Component {
...
render() {
const {instructions, useHighlighting} = this.props;
return markdown.toReact(instructions, useHighlighting);
}
...
} |
8d51c09 to
58f51b8
Compare
|
@outoftime updated with the move to a util file, and lazy loading of the remark renderer. |
outoftime
left a comment
There was a problem hiding this comment.
@inlinestyle left some thoughts… feels v. close though!
| Highlight.js gets run. | ||
| In theory this component can be removed and replaced with a | ||
| `componentDidCatch` method on the parent when we upgrade to React 16. | ||
| */ |
There was a problem hiding this comment.
I’m generally much more into function/method/class-level comments than inline ones… in this case just a matter of moving the comment block a couple lines north, you down?
There was a problem hiding this comment.
I'm assuming you mean put it immediately above unstable_handleError, instead of immediately below? To be honest I interpret both as "method-level" but it's no problem to move it.
There was a problem hiding this comment.
Yep, I guess just from the way jsdoc and equivalent systems do it, putting it above the method header feels a bit more natural
| In theory this component can be removed and replaced with a | ||
| `componentDidCatch` method on the parent when we upgrade to React 16. | ||
| */ | ||
| // eslint-disable-next-line react/no-set-state |
There was a problem hiding this comment.
I would argue that for this one, we want to disable the rule at the module level—it makes more sense to me to say “this is a component that uses state” rather than “this is a component that doesn’t use state except for this one place where it does”
| remark().use(remarkReact), | ||
| ); | ||
|
|
||
| export default { |
There was a problem hiding this comment.
I’m generally not a fan of export defaulting an object of functions—seems like named exports are a more idiomatic way to accomplish the same thing. How do you think about it?
There was a problem hiding this comment.
This is probably a holdover from other languages, but I like being able to use the module name to qualify a function... if I want to be able to write markdown.toReact I can use import * as markdown from ... (doesn't need to be a default) or import markdown from ... (needs to be a default).
I'd be fine switching to the import * as markdown from ... version, or abandoning the module qualification thing and changing the declaration to export function markdownToReact but those seem a little less natural. Up to you.
There was a problem hiding this comment.
mm, I definitely feel you and intuitively I too like having the qualifier. Ultimately I don’t have a specific preference to stake out but a couple of observations:
- It seems like
import * as markdownis a strict improvement, since you’re still getting the same interface in the importing module, and the exporting module structure is more idiomatic - It doesn’t seem very common to do e.g.
import {toReact as markdownToReact}but maybe that is the actual right answer?markdownToReactis justmarkdown.toReactwithout a dot, right?
| `componentDidCatch` method on the parent when we upgrade to React 16. | ||
| */ | ||
| // eslint-disable-next-line react/no-set-state | ||
| this.setState({useHighlighting: false}); |
There was a problem hiding this comment.
So, not a hill I want to die on, but having given it more thought I think there is a strong argument for pushing this to Redux, not in the form of “there was an error here” but rather in the form of “the instructions are known to break syntax highlighting”. So, a property of the instructions rather than the UI. WDYT?
There was a problem hiding this comment.
Well, it's not like putting it in Redux hurts at all. I don't really see a benefit, but I can do it for the sake of consistency within the project. Where in the redux state structure would you put it?
There was a problem hiding this comment.
Yeah I guess I see two appeals:
- Consistency, as you mention—this would be the only
setStatein the code base - Also, I am a partisan of the spirit behind “everything in Redux”, particularly the claim that the UI state does derive entirely from an underlying state of the world, and that the React layer is most legible when its job is just translating that state of the world into a DOM. Obviously the
uitree in the Redux store does betray the notion that sometimes UI state is just UI state, and libraries like https://github.com/tonyhb/redux-ui and others (which I’m not inherently against) make that even more explicit. But in this case what we’re talking about is actually a fact about the instructions, namely that they can’t be rendered with the syntax highlighting code we have in place. And I do think there is value to making that explicit in the “one true state of the world”.
As for a specific proposal, I think I’d put it in the project, basically turning instructions into a record with properties along the lines of content and isKnownToBreakSyntaxHighlighting.
All that said, I’m definitely open to being told I’m wrong/being too much of a purist/etc.!
58f51b8 to
3a4b1b8
Compare
1d15eba to
f4a3a1d
Compare
|
@outoftime updated! After all that drama, it turned out that |
Notes:
remark-react-lowlightemits a React.PropTypes deprecation warning. I've sent them a pull request here: Fix React deprecation warning - use 'prop-types' package inlinestyle/remark-react-lowlight#6InstructionsErrorBoundarycomponent with a loooong comment. The app now gracefully degrades to non-highlit code when an unconfigured language is supplied. Unfortunately I can't think of a way to make this apply "partially" (only to the offending code block) without parsing the markdown by hand.stateto store whether our syntax highlighting has errored out; I can move it to redux ifstateis that undesirable.Popcode with highlit html/css/js: http://localhost:3000/?gist=911a82a17a280545858d2d8ecc557ef3
Popcode with no highlighting due to a python section: http://localhost:3000/?gist=b2b6a8fa00f160e00ccf9ca0c1895994