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

Code block syntax highlighting differs drastically between editing and reading modes #39

Open
ramsey opened this issue Jan 28, 2024 · 11 comments
Assignees
Labels
bug Something isn't working

Comments

@ramsey
Copy link

ramsey commented Jan 28, 2024

When I'm working with notes that contain code blocks, the syntax highlighting of the code is very different between the editing and reading modes.

My expectation is that the syntax highlighting would use the same highlighting in both editing and reading modes.

I'm not sure which version is intended to be the "correct" form of syntax highlighting, but the highlighting I see in editing mode looks nicer (to me).

Example of syntax highlighting in editing mode

Obsidian syntax highlighting in editing mode

Example of syntax highlighting in reading mode

Obsidian syntax highlighting in reading mode

Reference source

For reference, here is the Markdown source I used in the example screenshots:

## Example

```python
def binary_search(arr, item):  
    low = 0  
    high = len(arr) - 1  
  
    while low <= high:  
        mid = low + ((high - low) // 2)
        guess = arr[mid]  
        if guess == item:  
            return mid  
        elif guess > item:  
            high = mid - 1  
        else:  
            low = mid + 1  
  
    return None

item_count = sys.maxsize  
item_to_find = random.randint(1, item_count)
items = range(1, item_count + 1)

print(f'Item {item_to_find} is at index {binary_search(items, item_to_find)} in the list.')
```
@Chrismettal Chrismettal self-assigned this Jan 28, 2024
@Chrismettal Chrismettal added the bug Something isn't working label Jan 28, 2024
@Chrismettal
Copy link
Collaborator

Thanks for reporting this.

I haven't yet had the time to deep dive into this, but looking into other themes, even the default Obsidian theme, this behavior seems to be pretty much present in whatever theme I test. Mostly with the same colors even.

@Chrismettal
Copy link
Collaborator

I've yet to find an example of a theme that manages to make codeblocks look the same in preview and editing form and I think I see why.

In the editing view, the input variables to your example would be themeable as cm-hmd-codeblock.cm-variable:

image

But in the reading view, function variables aren't "adressable" at all, making them use the standard text color:

image

And the function itself in the editing view would be cm-def.cm-hmd-codeblock:

image

While in the reading view, this would be themed via token.function:

image

So I can make sure that Dracula colors are used "wherever possible" in code spans, but I doubt I'll be able to make them look identical in both views, while following the original Dracula color guides.

@ramsey
Copy link
Author

ramsey commented Feb 4, 2024

Thanks for taking a look. Is this a bug in Obsidian's reading view, then?

@Chrismettal
Copy link
Collaborator

I hesitate calling it "bugged", but the way Obsidian renders the reading view does make it difficult to impossible to keep the coloring exactly the same for code blocks.

I'll add overrides for the colors that can be themed so they at least match Dracula theming though.

@ramsey
Copy link
Author

ramsey commented Feb 5, 2024

Thanks!

@Chrismettal
Copy link
Collaborator

Chrismettal commented Feb 9, 2024

I added code block theming with Version 1.1.0 just now.

Tried to align the code views as best as Obsidian allows. In the editing view, many things share the same CSS tag, namely cm-hmd-codeblock which doesn't allow me to theme every aspect of the code like a regular IDE, but we should now approach true Dracula colors better.

This is an example of the Dracula theme with your example in VSCode:

image

This is what the editing view looks like now (Most of what shows up as the white foreground color uses the cm-hmd-codeblock, thus isn't possible to be individually themed as of now.

image

Reading view looks pretty similar, but for example the "None" in line 15 is themed as a keyword in the editing view, while it is tagged as a boolean in the reading view.

image

I gotta admit though, I leaned heavily on your example and haven't gone out of my way to look for all possible keywords that might require theming. If you have example code where portions stick out after 1.1.0 feel free to reopen and paste them here.

@ramsey
Copy link
Author

ramsey commented Feb 9, 2024

This looks great. Thanks!

@EDM115
Copy link

EDM115 commented Feb 12, 2024

These changes indeed look awesome !
But I'm just wondering, it is possible to bring back the old background color of code blocks ? On big files it helped making a clear difference between them and regular text (ex. I use them without any language property to list the commits of the day during my internship, and now I just have big chunks of text showing in my files whereas before I clearly distinguished them as being less important than their summary above)

@ramsey
Copy link
Author

ramsey commented Feb 12, 2024

@EDM115 That's a good point. In IDEs using Dracula, the background is the same color as the whole page in Obsidian, so making it lighter behind code listings tends to wash out the colors. I wonder if the background color could be darker as a way to offset the code blocks from text?

@EDM115
Copy link

EDM115 commented Feb 12, 2024

I wonder if the background color could be darker as a way to offset the code blocks from text?

Well I believe the background color is already Background (#282A36), and there's no darker color (https://draculatheme.com/contribute)
My best idea would be to revert code block's background to Current Line (#44475A), as it was before. Or if it's not readable, then maybe it would be possible to have a border around code blocks that is Current Line

@Chrismettal Chrismettal reopened this Feb 12, 2024
@Chrismettal
Copy link
Collaborator

Good points both.
I felt the original light background color messed with the text contrast too much and didn't think a distinction was required. I did notice however that between editing an reading view, the background of single line code differs still.

Maybe there is a way to put a border of the lighter background color around the code blocks, instead of changing the background entirely?

Let me try a few mockups of what might work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

3 participants