Skip to content
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

feat(v2): support comments for code highlighting #2456

Merged
merged 6 commits into from
Mar 31, 2020

Conversation

elviswolcott
Copy link
Contributor

@elviswolcott elviswolcott commented Mar 24, 2020

Motivation

Per #2271, this adds the ability to highlight lines using highlight-next-line, highlight-start, and highlight-end comments.

Supported comment syntax:

  • JSX - { /* comment */ }
  • JS/C like single line - //comment
  • JS/C like multi line - /* comment */
  • Python - # comment
  • HTML - <!-- comment -->

AFAIK most languages use at least one of these for comments.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

localhost_3000_

To test, use this markdown:
https://gist.github.com/elviswolcott/c4f15c6b0ec579e43a775cfdf9b25cc4

Related PRs

#2457 (docs)

@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Mar 24, 2020

Deploy preview for docusaurus-2 ready!

Built with commit 49319b2

https://deploy-preview-2456--docusaurus-2.netlify.com

@elviswolcott
Copy link
Contributor Author

I noticed some potential issues in the Gatsby implementation so I approached it a bit differently.

  1. The line highlight comments are picked out of the syntax highlighted HTML. This leaves the highlighting dependent on knowing how Prism generates markup for certain code. It also doesn't lend itself to use in a component. By taking the comments from the raw code, it is easy to strip them out and pass in the highlight information without having any knowledge of what the markup will look like.
  2. The regex expressions it uses are very loose. i.e. /* highlight-next-line --> seems to be considered a valid comment.

As I mentioned over in the issue, I've only added highlight-next-line, highlight-start, and highlight-end.

highlight-range would be trivial to add, but doesn't seem to really solve the issue of readability or maintainability the line ranges pose.

highlight-line would be simple to add (just a second pattern that doesn't check for a whole line), but it doesn't seem to have much value over highlight-next-line (especially as far as readability) so I've decided to keep it simple for now.

@markerikson
Copy link

OH THIS IS SO COOOOOOOL! :)

I'm hyped. Can't wait to use this!

@yangshun yangshun changed the title feat: support comments for code highlighting feat(v2): support comments for code highlighting Mar 24, 2020
docs(v2): demonstrate highlighting with comments
@yangshun yangshun added the pr: new feature This PR adds a new API or behavior. label Mar 27, 2020
@yangshun yangshun added this to the v2.0.0-alpha.49 milestone Mar 29, 2020
Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

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

This is amazing. I can't wait to change all the code blocks to use this!

@yangshun yangshun merged commit 054563b into facebook:master Mar 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants