Skip to content

Commit

Permalink
Replace highlight.js with server-side Rouge highlighting
Browse files Browse the repository at this point in the history
Jekyll 3 and 4 process code blocks by default using Rouge[1], which adds
<span>s of various classes to the code indicating how different parts
should be highlighted. So to highlight code, all a theme needs to do is
include an appropriate stylesheet, which can be easily created for a
variety of color schemes using Rouge's `rougify style` command.

YAT, however, ignores the Rouge highlighting and instead includes
highlight.js, which does its own highlighting client-side. That
increases both bandwidth and client CPU usage for no clear benefit.
Furthermore, it tries to guess a language if none is specified even if
`guess_lang` is turned off in Jekyll's configuration, since `guess_lang`
only affects Rouge.

Let me know if there's a good reason to prefer highlight.js over Rouge,
but to me it it just looks like extra complexity for no gain. As such,
this change removes it and adds a Rouge stylesheet instead.

[1] https://github.com/rouge-ruby/rouge
  • Loading branch information
tchebb committed Aug 24, 2023
1 parent 2301024 commit 4a81476
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ I will respect your crucial support and say THANK YOU!
- Full layouts `home`, `post`, `tags`, `archive` and `about`.
- Uses font awesome 5 for icons.
- Beautiful page banner with image and video.
- Beautiful Syntax Highlight using [highlight.js][highlight-js].
- Beautiful Syntax Highlight with overridable [Rouge][rouge] themes.
- RSS support using [Jekyll Feed][jekyll-feed] gem.
- Optimized for search engines using [Jekyll Seo Tag][jekyll-seo-tag] gem.
- Sitemap support using [Jekyll Sitemap][jekyll-sitemap] gem.
Expand Down Expand Up @@ -197,4 +197,4 @@ This theme is licensed under the [MIT license](https://opensource.org/licenses/m
[jekyll-seo-tag]: https://github.com/jekyll/jekyll-seo-tag
[jekyll-sitemap]: https://github.com/jekyll/jekyll-sitemap
[jekyll-feed]: https://github.com/jekyll/jekyll-feed
[highlight-js]: https://github.com/highlightjs/highlight.js
[rouge]: https://github.com/rouge-ruby/rouge
16 changes: 15 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ yat:
# background_color: "#ff4e00"
# text_transform: "uppercase" # ("uppercase", "lowercase", "capitalize")

# Code highlighting setting
# This is a Jekyll setting rather than a theme setting, but the theme includes
# stylesheets to highlight code when the Rouge highlighter (the default) is in
# use. This setting affects both Markdown code blocks and Liquid `highlight`
# blocks. You can set it to `none` to disable highlighting.
#
# By default, we highlight code with Rouge's "github.light" theme. If you want
# to change that, you can override `assets/css/code-highlight-rouge.css` with a
# different stylesheet generated with Rouge's `rougify` command. If you do so,
# you must set the CSS scope to `.highlight pre` for proper rendering:
#
# $ mkdir -p assets/css
# $ bundle exec rougify style --scope '.highlight pre' <THEME> >assets/css/code-highlight-rouge.css
# highlighter: rouge

# If you want to link only specific pages in your header, uncomment
# this and add the path to the pages in order as they should show up
# header_pages:
Expand Down Expand Up @@ -233,7 +248,6 @@ yat:
# follow_site_theme: true

# Build settings
# highlighter: none
markdown: kramdown
kmarkdown:
input: GFM
Expand Down
14 changes: 4 additions & 10 deletions _includes/extensions/code-highlight.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js"></script>
<!-- and it's easy to individually load additional languages -->
<script charset="UTF-8"
src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/languages/go.min.js"
async></script>
<link rel="stylesheet" href="{{ "/assets/css/code-highlight-rouge.css" | relative_url }}">

{%- assign name = 'code_badge.enabled' -%}
{%- include functions.html func='get_value' default='true' -%}
Expand All @@ -21,8 +16,8 @@
{%- include functions.html func='get_value' default='uppercase' -%}
{%- assign badge_text_transform = return -%}

{%- if badge_enabled -%}
<script>
// Init highlight js
document.addEventListener('DOMContentLoaded', function(event) {
var els = document.querySelectorAll('pre code')

Expand All @@ -38,12 +33,11 @@
}
if (!lang) {
cls = block.getAttribute('class');
lang = cls ? cls.replace('hljs ', '') : '';
lang = cls ? cls : '';
}
if (lang.startsWith('language-')) {
lang = lang.substr(9);
}
block.setAttribute('class', 'hljs ' + lang);
block.parentNode.setAttribute('data-lang', lang);
}

Expand All @@ -58,7 +52,6 @@
function handle(block) {
addLangData(block);
addBadge(block)
hljs.highlightBlock(block);
}

for (var i = 0; i < els.length; i++) {
Expand Down Expand Up @@ -89,3 +82,4 @@
display: none;
}
</style>
{%- endif -%}
1 change: 0 additions & 1 deletion _sass/yat/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ blockquote {
pre,
code {
@include relative-font-size(0.9375);
color: $text-color;
}

*:not(pre) > code {
Expand Down
1 change: 0 additions & 1 deletion _sass/yat/_dark.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ html[data-theme="dark"] {
}

*:not(pre) > code {
color: $dark-text-color;
background-color: #454545;
}

Expand Down
112 changes: 112 additions & 0 deletions assets/css/code-highlight-rouge.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
.highlight pre table td { padding: 5px; }
.highlight pre table pre { margin: 0; }
.highlight pre, .highlight pre .w {
color: #24292f;
background-color: #f6f8fa;
}
.highlight pre .k, .highlight pre .kd, .highlight pre .kn, .highlight pre .kp, .highlight pre .kr, .highlight pre .kt, .highlight pre .kv {
color: #cf222e;
}
.highlight pre .gr {
color: #f6f8fa;
}
.highlight pre .gd {
color: #82071e;
background-color: #ffebe9;
}
.highlight pre .nb {
color: #953800;
}
.highlight pre .nc {
color: #953800;
}
.highlight pre .no {
color: #953800;
}
.highlight pre .nn {
color: #953800;
}
.highlight pre .sr {
color: #116329;
}
.highlight pre .na {
color: #116329;
}
.highlight pre .nt {
color: #116329;
}
.highlight pre .gi {
color: #116329;
background-color: #dafbe1;
}
.highlight pre .kc {
color: #0550ae;
}
.highlight pre .l, .highlight pre .ld, .highlight pre .m, .highlight pre .mb, .highlight pre .mf, .highlight pre .mh, .highlight pre .mi, .highlight pre .il, .highlight pre .mo, .highlight pre .mx {
color: #0550ae;
}
.highlight pre .sb {
color: #0550ae;
}
.highlight pre .bp {
color: #0550ae;
}
.highlight pre .ne {
color: #0550ae;
}
.highlight pre .nl {
color: #0550ae;
}
.highlight pre .py {
color: #0550ae;
}
.highlight pre .nv, .highlight pre .vc, .highlight pre .vg, .highlight pre .vi, .highlight pre .vm {
color: #0550ae;
}
.highlight pre .o, .highlight pre .ow {
color: #0550ae;
}
.highlight pre .gh {
color: #0550ae;
font-weight: bold;
}
.highlight pre .gu {
color: #0550ae;
font-weight: bold;
}
.highlight pre .s, .highlight pre .sa, .highlight pre .sc, .highlight pre .dl, .highlight pre .sd, .highlight pre .s2, .highlight pre .se, .highlight pre .sh, .highlight pre .sx, .highlight pre .s1, .highlight pre .ss {
color: #0a3069;
}
.highlight pre .nd {
color: #8250df;
}
.highlight pre .nf, .highlight pre .fm {
color: #8250df;
}
.highlight pre .err {
color: #f6f8fa;
background-color: #82071e;
}
.highlight pre .c, .highlight pre .ch, .highlight pre .cd, .highlight pre .cm, .highlight pre .cp, .highlight pre .cpf, .highlight pre .c1, .highlight pre .cs {
color: #6e7781;
}
.highlight pre .gl {
color: #6e7781;
}
.highlight pre .gt {
color: #6e7781;
}
.highlight pre .ni {
color: #24292f;
}
.highlight pre .si {
color: #24292f;
}
.highlight pre .ge {
color: #24292f;
font-style: italic;
}
.highlight pre .gs {
color: #24292f;
font-weight: bold;
}

0 comments on commit 4a81476

Please sign in to comment.