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

[ENG-3848][ENG-3861]Shiki Code block Experimental #4030

Open
wants to merge 28 commits into
base: main
Choose a base branch
from

Conversation

ElijahAhianyo
Copy link
Collaborator

@ElijahAhianyo ElijahAhianyo commented Sep 30, 2024

High-level API

rx._x.code_block(
        """
     console.log('hewwo') 
     console.log('hello')
     console.log('goodbye') 
  """,
       language="typescript",
       theme="github-dark",
)
Screenshot 2024-10-01 at 4 09 03 PM (2)

With copy button

rx._x.code_block(
        """
     console.log('hewwo') 
     console.log('hello')
     console.log('goodbye') 
  """,
       language="typescript",
       theme="github-dark",
       can_copy=True
 )
Screenshot 2024-10-01 at 4 12 17 PM (2)

Using transformers

rx._x.code_block(
        """
    console.log('hewwo') // [!code --]
     console.log('hello') // [!code ++]
     console.log('goodbye') // [!code highlight]
  """,
       language="typescript",
       use_transformers=True,
       theme="github-dark",
 )
Screenshot 2024-10-01 at 4 17 21 PM (2)

Low-level API

Shikijs transformer

The shiki transformer is provided via code_block.transformers API.

rx._x.code_block.root(
        """
    console.log('hewwo') // [!code --]
    console.log('hello') // [!code ++]
    console.log('goodbye') // [!code highlight]
  """,
       language="typescript",
       transformers=[rx._x.code_block.transformers.shikijs(fns=["transformerNotationDiff", "transformerNotationHighlight"])],
       theme="github-dark",
 )
Screenshot 2024-10-01 at 4 17 21 PM (2)

Custom transformer

In using custom transformers from third-party libs, we provide a code_block.create_transformer API that allows you to specify the library and functions to call from the lib.

rx._x.code_block.root(
        """
    console.log('hewwo') // [!code --]
    console.log('hello') // [!code ++]
    console.log('goodbye') // [!code highlight]
  """,
       language="typescript",
       transformers=[
                rx._x.code_block.create_transformer( # using a transformer from a third party library
                          library="shiki-transformer-color-highlight",
                          fns=["transformerColorHighlight"], 
                          style={...}
                  ),
                 {
                      code(node) { # lifted this off https://shiki.matsu.io/guide/transformers#transformers
                        this.addClassToHast(node, 'language-js')
                      },
                      line(node, line) {
                        node.properties['data-line'] = line
                        if ([1, 3, 4].includes(line))
                          this.addClassToHast(node, 'highlight')
                      },
                      span(node, line, col) {
                        node.properties['data-token'] = `token:${line}:${col}`
                      },
                },
         ],
         theme="github-dark",
 )

Some Notes:

  • The styling for provided transformer(code_block.transformers.shikijs ) needs to be improved. The bg colors for the diffs overflow out of the div as well as the line numbers.
  • Styles for other transformer fns in the provided transformer need to be provided (eg, for highlighting, etc)
  • copy button needs to be improved

@Alek99 Alek99 self-requested a review October 1, 2024 16:30
@Alek99
Copy link
Member

Alek99 commented Oct 1, 2024

Could we just include transformers internally in the component so they dont have to define this. Just have all the transformers in there

That way we can avoid this line.
transformers=[rx._x.code_block.transformers.shikijs(fns=["transformerNotationDiff", "transformerNotationHighlight"])],

Or have transformers be a boolean where it's disabled by default but they can enable it with some default styles. Then have a separate prop called custom_transformer where they can add customized ones. I think the current code is a a bit too low level for an end user

@ElijahAhianyo ElijahAhianyo changed the title [WIP]Shiki Code block Experimental [ENG-3848][ENG-3861]Shiki Code block Experimental Oct 2, 2024
Copy link

linear bot commented Oct 2, 2024

Comment on lines +8 to +14
let final_code;

if (Array.isArray(code)) {
final_code = code[0];
} else {
final_code = code;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is more of a hack to get reflex web to run. somehow, a list is sometimes passed as the value of code. Have a tracking ticket to look into that https://linear.app/reflex-dev/issue/ENG-3893/get-shiki-to-work-with-reflex-web

@ElijahAhianyo ElijahAhianyo marked this pull request as ready for review October 2, 2024 15:00
Copy link
Contributor

@picklelo picklelo left a comment

Choose a reason for hiding this comment

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

Does the entire style dictionary get compiled in every time you call it? I wonder if we need to do some optimizations around that

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

Successfully merging this pull request may close these issues.

4 participants