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

Merged
merged 40 commits into from
Oct 22, 2024

Conversation

ElijahAhianyo
Copy link
Contributor

@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

@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

adhami3310
adhami3310 previously approved these changes Oct 21, 2024
@ElijahAhianyo
Copy link
Contributor Author

cant tell why the tests are failing after merge just by looking at the CI errors. @adhami3310 any ideas?

@ElijahAhianyo ElijahAhianyo force-pushed the elijah/shiki-code-block branch from f7df9c6 to f3040ab Compare October 22, 2024 15:05
@ElijahAhianyo ElijahAhianyo merged commit d63b3a2 into main Oct 22, 2024
31 checks passed
Kastier1 pushed a commit that referenced this pull request Oct 23, 2024
* Shiki Code block Experimental

* refactor

* update code

* remove console.log

* add transformers to namespace

* some validations

* fix components paths

* fix ruff

* add a high-level component

* fix mapping

* fix mapping

* python 3.9+

* see if this fixes the tests

* fix pyi and annotations

* minimal update of theme and language map

* add hack for reflex-web/flexdown

* unit test file commit

* [ENG-3895] [ENG-3896] Update styling for shiki code block

* strip transformer triggers

* minor refactor

* linter

* fix pyright

* pyi fix

* add unit tests

* sneaky pyright ignore

* the transformer trigger regex should remove the language comment character

* minor refactor

* fix silly mistake

* component mapping in markdown should use the first child for codeblock

* use ternary operator in numbers.py, move code block args to class for docs discoverability

* precommit

* pyright fix

* remove id on copy button animation

* pyright fix for real

* pyi fix

* pyi fix fr

* check if svg exists

* copy event chain

* do a concatenation instead of first child

* added comment

---------

Co-authored-by: Carlos <[email protected]>
@masenf masenf deleted the elijah/shiki-code-block branch December 12, 2024 07:15
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.

5 participants