Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

all: highlight with shiki in production #42

Merged
merged 4 commits into from
Jul 23, 2024
Merged

all: highlight with shiki in production #42

merged 4 commits into from
Jul 23, 2024

Conversation

jo3-l
Copy link
Collaborator

@jo3-l jo3-l commented Jul 23, 2024

  • Syntax highlighting in codeblocks don't look as good as they could (compare to current GitBook docs)
  • At some point, we would like to switch a highlighting style specialized to custom commands instead of using Go, but currently Hugo provides no facility to do so: Add support for chroma lexer overrides gohugoio/hugo#11496
  • Alternatives: switch away from Chroma (Hugo's default highlighter) to a different syntax highlighter
    • However, most syntax highlighting libraries (notably, hljs and prism) render on client-side; Chroma runs at build time so the highlighted content is shipped directly to users
    • Can we do better?
  • (This PR) experiments with a different build-time highlighter: https://shiki.style/
    • Shiki has additional benefits compared to all options above; see its website; it is also the highlighter that GitBook uses
  • Downside: since Shiki is not built into Hugo, we need a second build step that runs after Hugo which post-processes the built HTML files directly (scripts/highlight.mjs)
    • Takes ~1s for 60 files on my machine, comparable to Hugo build time
  • Downside 2: Shiki can't work with hot reload (npm run dev) since it is not built into Hugo, so the appearance of codeblocks is different in a production/dev env. That is, npm run dev will use built-in Doks highlighting (i.e., not Shiki.)
    • Can still preview appearance with Shiki by building npm run build (which automatically runs the post-processer), then npm run preview
  • With that said, there is a (massive) upside:
    • Shiki is basically ~infinitely customizable, we are free from the Doks/Hugo shackles. Should be super straightforward to add a custom style for custom commands.

Terms

@jo3-l
Copy link
Collaborator Author

jo3-l commented Jul 23, 2024

I yanked the copy button out of Doks internal in the latest commit, so we should be at feature parity now. Note that the copy button colors were designed for the Doks syntax highlighting theme so do not look as nice right now, but that is easily fixed in a follow-up. With that, then, I'll mark this PR as (tentatively) ready for review.

@jo3-l jo3-l marked this pull request as ready for review July 23, 2024 02:04
@jo3-l jo3-l changed the title wip: all: highlight with shiki in production all: highlight with shiki in production Jul 23, 2024
Copy link
Collaborator

@l-zeuch l-zeuch left a comment

Choose a reason for hiding this comment

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

Cool 👍

@l-zeuch l-zeuch merged commit 3d6dcd2 into master Jul 23, 2024
4 checks passed
@l-zeuch l-zeuch deleted the feat/use-shiki branch July 23, 2024 14:56
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants