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

feat: highlight code blocks #51

Merged
merged 12 commits into from
Oct 13, 2024
Merged

feat: highlight code blocks #51

merged 12 commits into from
Oct 13, 2024

Conversation

timonv
Copy link
Contributor

@timonv timonv commented Oct 7, 2024

Hey there, love the simplicity of tui-markdown. Using this for a project that renders code, and I noticed it was adding an extra new line after starting the codeblock, and adding newlines between each code line. Additionaly, the code event does not get triggered.

After fixing that, I figured code highlighting would be cool. Added it behind a feature flag as the library is not lightweight.

Added tests with insta. Since the github pipeline is not in the same repository, I couldn't test for both features. Perhaps using cargo-hack with --each-feature is a nice solution.

image

Might be nice to also set a background for the full block, with the maximum line length from the full block. Current solution and syntect just do line length, which in my opinion doesn't look great.

@timonv
Copy link
Contributor Author

timonv commented Oct 11, 2024

Hmm I think Code is actually used for inline snippetes

@timonv
Copy link
Contributor Author

timonv commented Oct 11, 2024

Fixed 👌

Copy link
Owner

@joshka joshka left a comment

Choose a reason for hiding this comment

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

Generally LGTM. I started to review this earlier this week, but didn't hit submit. Apologies on the delay. A few minor comments.

@@ -0,0 +1,352 @@
---
source: tui-markdown/src/lib.rs
expression: "from_str(indoc!\n{\n \"\n ```rust\n fn main() {\n println!(\\\"Hello, world!\\\");\n }\n ```\n \"\n})"
Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if there's a good way to write this as a multi-line string instead of with inline \n characters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it always the expression inside the macro? Maybe it's an idea to assign variables with descriptive names.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems to work by assigning to a variable first, I've also changed the blocks slightly so it's a bit easier when reviewing

Copy link
Owner

Choose a reason for hiding this comment

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

In theory this could also be manually written in yaml as something like

expression: |
    from_str(indoc!{"
    ...

From some naive testing, insta doesn't seem to care too much about the expression regardless, so the indoc parts are probably unnecessary there either (their effect is just to remove the initial indentation in the rust code). This is not a big deal however.

@timonv timonv requested a review from joshka October 12, 2024 08:19
@timonv
Copy link
Contributor Author

timonv commented Oct 12, 2024

Thanks for the feedback! Updated the PR.

@timonv
Copy link
Contributor Author

timonv commented Oct 13, 2024

Hmm after 09353f1 some indentation is missing

Added a test to verify, looks alright

@codecov-commenter
Copy link

codecov-commenter commented Oct 13, 2024

Codecov Report

Attention: Patch coverage is 97.22222% with 1 line in your changes missing coverage. Please review.

Project coverage is 41.68%. Comparing base (65241aa) to head (168da5e).

Files with missing lines Patch % Lines
tui-markdown/src/lib.rs 97.22% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #51      +/-   ##
==========================================
+ Coverage   37.66%   41.68%   +4.01%     
==========================================
  Files           5        5              
  Lines         446      475      +29     
==========================================
+ Hits          168      198      +30     
+ Misses        278      277       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@joshka
Copy link
Owner

joshka commented Oct 13, 2024

The ratatui/underline-color feature was added automatically due to feature unification at the workspace level (due to markdown-reader pulling in ratatui/default). This is probably a small problem for targets that don't have this feature available, but if it's one that causes problems, this can be solved in another PR I think.

@joshka
Copy link
Owner

joshka commented Oct 13, 2024

I enabled this by default. To disable set default-features = false.

@joshka joshka changed the title feat: Optional code highlighting with syntect feat: highlight code blocks Oct 13, 2024
@joshka joshka merged commit 6d41f83 into joshka:main Oct 13, 2024
11 checks passed
@joshka
Copy link
Owner

joshka commented Oct 13, 2024

Thanks for the PR @timonv

@github-actions github-actions bot mentioned this pull request Oct 7, 2024
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.

3 participants