Skip to content

Code highlighting#1036

Merged
fantactuka merged 4 commits intomainfrom
code-highlight
Jan 6, 2022
Merged

Code highlighting#1036
fantactuka merged 4 commits intomainfrom
code-highlight

Conversation

@fantactuka
Copy link
Copy Markdown
Collaborator

@fantactuka fantactuka commented Dec 21, 2021

Submitting PR for initial feedback

  • Using refractor for highlighting (it's built on top of prism.js and generates highlights tree structure, comparing to prism's html output) prism.tokenize to get highlight tokens.
  • Listening for nodes transforms, and for Text or CodeHighlightNode triggers re-highlighting on their parent
  • Highlighting takes whole content of the code block, generates new nodes list and diff it with current children, trying to replace minimal range.

Some concerns/thoughts

  • With current highlight diffing performance might suffer for long code blocks, e.g. 600 lines of code generate ~8k highlight nodes
  • Selection retention is not efficient and reliable. Right now it calculates text offset (goes through all anchor siblings and get their text content length), and will be slow for longer documents
  • It likely need to go into DecoratedNode and be a nested editor, e.g. to allow render language selector (right now JS is hardcoded) and/or copy content button

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 21, 2021
@vercel
Copy link
Copy Markdown

vercel bot commented Dec 21, 2021

@fantactuka is attempting to deploy a commit to the Facebook Open Source Team on Vercel.

To accomplish this, @fantactuka needs to request access to the Team.

Afterwards, an owner of the Team is required to accept their membership request.

If you're already a member of the respective Vercel Team, make sure that your Personal Vercel Account is connected to your GitHub account.

Comment on lines 93 to 102
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've added this to allow copy/pasting from editor into code block. By default divs are ignored and all code is inserted as a single line. Although it's intended to be temp code I wonder if there're any concerns to make it permanent

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@acywatson can you review this please?

Copy link
Copy Markdown
Contributor

@acywatson acywatson Dec 22, 2021

Choose a reason for hiding this comment

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

I wonder if this would work for your case:

#948

I was thinking about adding a generic childTransformer that can apply any arbitrary logic to the children of a DOM Node. It would handle cases like encountering a <strong> tag, where we need to apply the transformation of styling the text to "bold" for every child that's a TextNode.

I can look a bit deeper at this later, but appendAfter might make sense to have as well. Either way, we would need some tests for it.

@fantactuka fantactuka force-pushed the code-highlight branch 3 times, most recently from 314bed1 to 6600798 Compare December 23, 2021 15:10
@fantactuka
Copy link
Copy Markdown
Collaborator Author

Rebased PR to lexical names and removed copy/paste transform update - it deserves its own PR

@acywatson
Copy link
Copy Markdown
Contributor

Rebased PR to lexical names and removed copy/paste transform update - it deserves its own PR

#1042

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As far as I know, this would be the only place that we provide a className in a node constructor instead of getting it from the Theme. Not necessarily a problem, just something I wanted to point out.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That's fair point, I'll extract highlighting classes into theme, it should be ~10 of them. Thanks for catching!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm a little confused here - if you remove all the children, won't the length always be 0?

Copy link
Copy Markdown
Collaborator Author

@fantactuka fantactuka Dec 23, 2021

Choose a reason for hiding this comment

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

Not always, e.g. you can have code block with 10 code-highlight children, and only need to replace from 2nd to 5th and keep the rest. This method works similar to Array.splice and allows deletion/replacement/insertion for specified range

Right now it's not very efficient, as it removes/inserts nodes one by one, and can be replaced with low-level api instead:

https://gist.github.com/fantactuka/27eca8dfc269ba4e371a7e257bd46af8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, right, the loop is bounded by the values of the from and to parameters...missed that. Thanks

@fantactuka
Copy link
Copy Markdown
Collaborator Author

Extracted code highlighting styles into theme, now instead of applying refractor's class name directly it'll be mapped to theme class instead.

@vercel
Copy link
Copy Markdown

vercel bot commented Dec 28, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/fbopensource/lexical/2JbRiWJB5LoVX3fadydLaJD64drR
✅ Preview: https://lexical-git-code-highlight-fbopensource.vercel.app

@fantactuka fantactuka merged commit 14500db into main Jan 6, 2022
@fantactuka fantactuka deleted the code-highlight branch January 6, 2022 14:10
acywatson pushed a commit that referenced this pull request Apr 9, 2022
* Code highlighting
GermanJablo added a commit to payloadcms/payload that referenced this pull request Sep 3, 2025
Fixes #13386

Below I write a clarification to copy and paste into the release note,
based on our latest upgrade of Lexical [in
v3.29.0](https://github.com/payloadcms/payload/releases/tag/v3.29.0).

## Important
This release upgrades the lexical dependency from 0.28.0 to 0.34.0.

If you installed lexical manually, update it to 0.34.0. Installing
lexical manually is not recommended, as it may break between updates,
and our re-exported versions should be used. See the [yellow banner
box](https://payloadcms.com/docs/rich-text/custom-features) for details.

If you still encounter richtext-lexical errors, do the following, in
this order:

- Delete node_modules
- Delete your lockfile (e.g. pnpm-lock.json)
- Reinstall your dependencies (e.g. pnpm install)

### Lexical Breaking Changes

The following Lexical releases describe breaking changes. We recommend
reading them if you're using Lexical APIs directly
(`@payloadcms/richtext-lexical/lexical/*`).

- [v.0.33.0](https://github.com/facebook/lexical/releases/tag/v0.33.0)
- [v.0.30.0](https://github.com/facebook/lexical/releases/tag/v0.30.0)
- [v.0.29.0](https://github.com/facebook/lexical/releases/tag/v0.29.0)

___

TODO:
- [x] facebook/lexical#7719
- [x] facebook/lexical#7362
- [x] facebook/lexical#7707
- [x] facebook/lexical#7388
- [x] facebook/lexical#7357
- [x] facebook/lexical#7352
- [x] facebook/lexical#7472
- [x] facebook/lexical#7556
- [x] facebook/lexical#7417
- [x] facebook/lexical#1036
- [x] facebook/lexical#7509
- [x] facebook/lexical#7693
- [x] facebook/lexical#7408
- [x] facebook/lexical#7450
- [x] facebook/lexical#7415
- [x] facebook/lexical#7368
- [x] facebook/lexical#7372
- [x] facebook/lexical#7572
- [x] facebook/lexical#7558
- [x] facebook/lexical#7613
- [x] facebook/lexical#7405
- [x] facebook/lexical#7420
- [x] facebook/lexical#7662

---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1211202581885926
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants