-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DevDocs: add syntax highlighting #10359
DevDocs: add syntax highlighting #10359
Conversation
This is great! thanks @spen! the code looks fine to me but I'd be interested in knowing why you chose prism over highlight and other options if you also looked into them |
So I only really looked in to Prism and Highlight.js as they seemed to surface the most. |
I've put together some notes on some of the open-source syntax highlighters currently available. RequirementsHere're a few points that I think are important to keep in mind when considering one of the projects:
notes ...Highlight.jsThis seems popular and active. Unfortunately it seems to struggle somewhat with JSX syntax - particularly JS within JSX.
PrismThis also seems popular and active - I've seen a few people mention that they've made the switch from highlight.js to this package based on the Prism is extendable with plug-ins - which could prove useful if any Calypso-specific behaviour is needed. There's a tool for testing Prism's highlighting here. For the initial commit here in this PR I chose to use Prism because highlightsThis highlighter is used in Atom's Markdown Preview. From what I can see, the intent is that this reads a file and outputs the tokenized contents. Negatives
SyntaxhighlighterI feel like I recently saw a block of code on wordpress.org that wasn't highlighted and assumed there was just no highlighting in place but when I went looking for an example I stumbled across this update that includes a highlighted block. On inspection it turns out that this uses syntaxhighlighter, applied on the client side to the DOM nodes. Quickly tested by just loading the page with JS turned off. The (brevityified) markup for the first line looks like this: <!-- container -->
<div id="highlighter_696439" class="syntaxhighlighter php">
<!-- omitting line numbers... -->
<code class="php plain">wp_nav_menu( </code>
<code class="php keyword">array</code>
<code class="php plain">(</code>
<!-- on so on --> This approach is different to that of the other tools above in that instead of classing the containing I've explored syntaxhighlighter a little, looked at it's source and docs and it doesn't seem to apply to strings (I may have missed something though) and so unfortunately I'd rule that out for Calypso. I've not gone very deep with this package - since it's applied retrospectively to the DOM so I don't know how its JSX support is. Ultimately it'd be awesome to have something that can be used universally, whether thats one syntax processor that can be used all over Automattic products... or at the very least a standard output from, say, a node syntax-processor and a PHP syntax-processor so that 'theme' css can be applied in all necessary places - JSX syntax support on wordpress.org should be identical to that on Calypso's DevDocs as it should be for code-blocks in Calypso's Reader. |
Thanks @spen - this is really a valuable assessment of the options. It seems very reasonable why you chose prism. I support this PR 😄 |
background: #b3d4fc; | ||
} | ||
|
||
.token.comment, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there is a plug-in that allows for a custom class, which might be worth adding to better namespace these styles. I'll add that to #10359 (comment) .
… is not registered
@spen: thanks for this PR. Can you please let me know when you consider it ready for testing and I can do some testing on it. |
Sure thing! I've added some test instructions to the pull description so it could be tested now but I've not tested all of these syntaxes myself (I'm working on another project right now so can't test these myself until a little later) so you may want to wait until then. Though I think it's ready to test I don't think it's necessarily ready to merge as I think the CSS could be improved, using variables etc. |
It seems that |
Ok, this should be good to test now (see the test instructions in the desc). note:I've hardcoded an alias of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great and tests well - I will merge it now
Deployed and looking great on wpcalypso: eg: https://wpcalypso.wordpress.com/devdocs/client/components/empty-content/README.md |
Ahh awesome! Thanks @alisterscott ! I think I'll do some investigating in to how those string props might be better handled and, if I can figure it out, open an upstream PR :) |
@@ -90,6 +90,7 @@ | |||
"phone": "git+https://github.com/Automattic/node-phone.git#1.0.8", | |||
"photon": "2.0.0", | |||
"postcss-cli": "2.5.1", | |||
"prismjs": "^1.6.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spen let's try to pin all dependencies we add 👍 We've been burned by things like left-pad in the past.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh sorry looks like this was already pinned elsewhere 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the nudge though @gwwar, always good to be thorough! :)
This PR adds syntax highlighting to the code blocks shown in DevDocs.
Currently code blocks are wrapped in
<pre>
and<code>
tags via themarked
package but the content itself is not processed for syntax.@alisterscott pointed this out in a recent PR of mine (#10329)
This is not a perfect solution however... I've been working with the EmptyContent DevDoc page while developing and noticed that some
.jsx
syntax is not parsed properly.In this PR
marked
is configured to useprismjs
to handle syntax highlighting.I've added some styling that is fairly close to GitHubs (at least in terms of colors).
So far I've tried a similar approach with highlight.js too, though this is also lacking in perfect
.jsx
support.There is certainly an improvement but I wouldn't expect a merge right away until other options are explored.
I thought I'd get this up early in the hopes of starting a conversation - hopefully somebody out there has more experience than I do with highlighting packages :)
Testing
Test various syntax types, checking that A) they are highlighted and B) they are highlighted correctly (allowing for caveats mentioned in the discussion):
Notes: the only other syntax in use in code-blocks is 'text' (from a project-wide search for all instances of
```
).