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

Grouping by line #8

Closed
Hypercubed opened this issue Jul 28, 2018 · 10 comments
Closed

Grouping by line #8

Hypercubed opened this issue Jul 28, 2018 · 10 comments

Comments

@Hypercubed
Copy link

I've been considering how to add line highlights similar to https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-remark-prismjs . This is what I came up with:

const refractor = require("refractor")
const rehype = require('rehype')

const code = `
(function(win) {
    console.log(win);
))(window || global);
`;

const highlightLines = [3];

const nodes = code.split('\n',)
  .map((line, index) => {
    return {
      type: 'element',
      tagName: 'span',
      properties: { className: ['line', highlightLines.includes(index) ? 'highlight' : ''] },
      children: refractor.highlight(line, 'js')
    };
  });

const html = rehype()
  .stringify({type: 'root', children: nodes})
  .toString();

Do you think this is something we could add to refractor itself?

@Hypercubed
Copy link
Author

Here is a more complete (and working) example: https://stackblitz.com/edit/js-r2mz9b

@wooorm
Copy link
Owner

wooorm commented Jul 31, 2018

@conorhastings anything we could port over from your library to here? Seems you've worked out a few things already that I'd like to implement in refractor too, and maybe it doesn't make sense to have them in two places. What do you think?

@conorhastings
Copy link
Contributor

@wooorm definitely, line handling in prism was the easier of the two, let me see if i can pull out my code and take away lowlight specific stuff

@Hypercubed
Copy link
Author

Any luck with this? You can see my implementation here: https://github.com/swimlane/DocSPA/blob/master/projects/swimlane/docspa-core/src/lib/plugins/prisim.ts#L32

@conorhastings
Copy link
Contributor

@Hypercubed sorry been super busy traveling for work, I'll compare our implementations this weekend for sure and I if think i have any worthwhile contribution here, sorry about the long delay

@Hypercubed
Copy link
Author

No worries... I'm willing to contribute if I can.

@wooorm
Copy link
Owner

wooorm commented Jan 9, 2019

I’m not sure anymore if refractor should include this here. It could be a plugin though, working the same way as prism’s line-numbers.

I do think the core of react-syntax-highlighters line mechanism could be externalised. And it could be a HAST utility for use with refractor and inside react-syntax-highlighter. And be published as refractor-line-numbers?

I do think having the plugins work with refractor, and I’d include links to ports here!

@wooorm
Copy link
Owner

wooorm commented Jan 9, 2019

Closing as I don’t think it needs code changes here btw, but would like to chat about a plugin!

@wooorm wooorm closed this as completed Jan 9, 2019
@iammarkps
Copy link

Any luck using line highlighting here?

@suin
Copy link

suin commented Jun 2, 2021

Here I made a utility to group refractor ASTs by lines: https://github.com/suin/refractor-group-by-lines

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

No branches or pull requests

5 participants