Skip to content

Commit

Permalink
refactor: Do not rely on JavaScript for code highlighting, make dark …
Browse files Browse the repository at this point in the history
…and light themes consistent
  • Loading branch information
palant committed Jun 4, 2020
1 parent 1322fdd commit 9c6e869
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 146 deletions.
8 changes: 0 additions & 8 deletions assets/js/dark-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ function changeMode() {
const themeColor = isDark ? '{{ .Site.Params.themeColorDark }}': '{{ .Site.Params.themeColor }}';
document.querySelector('meta[name="theme-color"]').setAttribute('content', themeColor);

// Change Chroma Code Highlight Theme
const oldChromaTheme = isDark ? 'chroma' : 'chroma-dark';
const newChromaTheme = isDark ? 'chroma-dark' : 'chroma';

[].slice.apply(document.getElementsByClassName(oldChromaTheme)).forEach((e) => {
e.className = newChromaTheme;
});

{{ if and .Site.Params.enableUtterances (eq hugo.Environment "production") }}
// Change Utterances Comments Theme
// https://github.com/utterance/utterances/issues/229
Expand Down
65 changes: 64 additions & 1 deletion assets/scss/components/_highlight.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ span.lnt {
text-align: right;
}

.chroma, .chroma-dark {
.chroma {
color: var(--color-contrast-high);
background-color: alpha(var(--color-contrast-lower), 0.5);
}
Expand All @@ -34,3 +34,66 @@ span.lnt {
max-height: $maxHeight;
}
}

/* The following is the output of `hugo gen chromastyles --style=dracula` */
/* with color values replaced by variables. */

/* LineTableTD */ .chroma .lntd { vertical-align: top; padding: 0; margin: 0; border: 0; }
/* LineTable */ .chroma .lntable { border-spacing: 0; padding: 0; margin: 0; border: 0; width: auto; overflow: auto; display: block; }
/* LineHighlight */ .chroma .hl { display: block; width: 100%;background-color: var(--chroma-line-numbers-background) }
/* LineNumbersTable */ .chroma .lnt { margin-right: 0.4em; padding: 0 0.4em 0 0.4em;color: var(--chroma-line-numbers-color) }
/* LineNumbers */ .chroma .ln { margin-right: 0.4em; padding: 0 0.4em 0 0.4em;color: var(--chroma-line-numbers-color) }
/* Keyword */ .chroma .k { color: var(--chroma-keyword-color) }
/* KeywordConstant */ .chroma .kc { color: var(--chroma-keyword-color) }
/* KeywordDeclaration */ .chroma .kd { color: var(--chroma-declaration-color); font-style: italic }
/* KeywordNamespace */ .chroma .kn { color: var(--chroma-name-color) }
/* KeywordPseudo */ .chroma .kp { color: var(--chroma-keyword-color) }
/* KeywordReserved */ .chroma .kr { color: var(--chroma-keyword-color) }
/* KeywordType */ .chroma .kt { color: var(--chroma-name-color) }
/* NameAttribute */ .chroma .na { color: var(--chroma-attribute-color) }
/* NameBuiltin */ .chroma .nb { color: var(--chroma-name-color); font-style: italic }
/* NameClass */ .chroma .nc { color: var(--chroma-attribute-color) }
/* NameFunction */ .chroma .nf { color: var(--chroma-attribute-color) }
/* NameLabel */ .chroma .nl { color: var(--chroma-name-color); font-style: italic }
/* NameTag */ .chroma .nt { color: var(--chroma-keyword-color) }
/* NameVariable */ .chroma .nv { color: var(--chroma-name-color); font-style: italic }
/* NameVariableClass */ .chroma .vc { color: var(--chroma-name-color); font-style: italic }
/* NameVariableGlobal */ .chroma .vg { color: var(--chroma-name-color); font-style: italic }
/* NameVariableInstance */ .chroma .vi { color: var(--chroma-name-color); font-style: italic }
/* LiteralString */ .chroma .s { color: var(--chroma-literal-color) }
/* LiteralStringAffix */ .chroma .sa { color: var(--chroma-literal-color) }
/* LiteralStringBacktick */ .chroma .sb { color: var(--chroma-literal-color) }
/* LiteralStringChar */ .chroma .sc { color: var(--chroma-literal-color) }
/* LiteralStringDelimiter */ .chroma .dl { color: var(--chroma-literal-color) }
/* LiteralStringDoc */ .chroma .sd { color: var(--chroma-literal-color) }
/* LiteralStringDouble */ .chroma .s2 { color: var(--chroma-literal-color) }
/* LiteralStringEscape */ .chroma .se { color: var(--chroma-literal-color) }
/* LiteralStringHeredoc */ .chroma .sh { color: var(--chroma-literal-color) }
/* LiteralStringInterpol */ .chroma .si { color: var(--chroma-literal-color) }
/* LiteralStringOther */ .chroma .sx { color: var(--chroma-literal-color) }
/* LiteralStringRegex */ .chroma .sr { color: var(--chroma-literal-color) }
/* LiteralStringSingle */ .chroma .s1 { color: var(--chroma-literal-color) }
/* LiteralStringSymbol */ .chroma .ss { color: var(--chroma-literal-color) }
/* LiteralNumber */ .chroma .m { color: var(--chroma-number-color) }
/* LiteralNumberBin */ .chroma .mb { color: var(--chroma-number-color) }
/* LiteralNumberFloat */ .chroma .mf { color: var(--chroma-number-color) }
/* LiteralNumberHex */ .chroma .mh { color: var(--chroma-number-color) }
/* LiteralNumberInteger */ .chroma .mi { color: var(--chroma-number-color) }
/* LiteralNumberIntegerLong */ .chroma .il { color: var(--chroma-number-color) }
/* LiteralNumberOct */ .chroma .mo { color: var(--chroma-number-color) }
/* Operator */ .chroma .o { color: var(--chroma-keyword-color) }
/* OperatorWord */ .chroma .ow { color: var(--chroma-keyword-color) }
/* Comment */ .chroma .c { color: var(--chroma-comment-color) }
/* CommentHashbang */ .chroma .ch { color: var(--chroma-comment-color) }
/* CommentMultiline */ .chroma .cm { color: var(--chroma-comment-color) }
/* CommentSingle */ .chroma .c1 { color: var(--chroma-comment-color) }
/* CommentSpecial */ .chroma .cs { color: var(--chroma-comment-color) }
/* CommentPreproc */ .chroma .cp { color: var(--chroma-keyword-color) }
/* CommentPreprocFile */ .chroma .cpf { color: var(--chroma-keyword-color) }
/* GenericDeleted */ .chroma .gd { color: var(--chroma-deleted-color) }
/* GenericEmph */ .chroma .ge { text-decoration: underline }
/* GenericHeading */ .chroma .gh { font-weight: bold }
/* GenericInserted */ .chroma .gi { font-weight: bold }
/* GenericOutput */ .chroma .go { color: var(--chroma-output-color) }
/* GenericSubheading */ .chroma .gu { font-weight: bold }
/* GenericUnderline */ .chroma .gl { text-decoration: underline }
11 changes: 6 additions & 5 deletions assets/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@
$defaultTheme: light;
{{ end }}

{{ if .Site.Params.enableHighlight }}
$enableHighlight: true;
{{ else }}
$enableHighlight: false;
{{ end }}

@import "themes/light";

// Dark Mode
Expand All @@ -184,10 +190,6 @@

@import "themes/dark";
@import "components/dark-mode";

{{ if .Site.Params.enableHighlight }}
@import "themes/highlight/dark";
{{ end }}
{{ end }}


Expand Down Expand Up @@ -343,7 +345,6 @@
{{ end }}

{{ if .Site.Params.enableHighlight }}
@import "themes/highlight/light";
@import "components/highlight";
{{ end }}

Expand Down
15 changes: 15 additions & 0 deletions assets/scss/themes/_dark.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
.theme-icon-dark {
display: inline-block;
}
@if ($enableHighlight) {
/* https://xyproto.github.io/splash/docs/all.html */
/* Color values matching dracula style */

--chroma-line-numbers-background: #ffffcc;
--chroma-line-numbers-color: #7f7f7f;
--chroma-keyword-color: #ff79c6;
--chroma-name-color: #8be9fd;
--chroma-attribute-color: #50fa7b;
--chroma-literal-color: #f1fa8c;
--chroma-number-color: #bd93f9;
--chroma-comment-color: #6272a4;
--chroma-deleted-color: #8b080b;
--chroma-output-color: #44475a;
}
}

@media (prefers-color-scheme: dark) {
Expand Down
18 changes: 16 additions & 2 deletions assets/scss/themes/_light.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,25 @@
.theme-icon-dark {
display: none;
}
@if ($enableHighlight) {
/* https://xyproto.github.io/splash/docs/all.html */
/* Color values matching manni style */

--chroma-line-numbers-background: #ffffcc;
--chroma-line-numbers-color: #7f7f7f;
--chroma-keyword-color: #006699;
--chroma-name-color: #9999ff;
--chroma-attribute-color: #330099;
--chroma-literal-color: #cc3300;
--chroma-number-color: #ff6600;
--chroma-comment-color: #0099ff;
--chroma-deleted-color: #ffcccc;
--chroma-output-color: #aaaaaa;
}
}

@media (prefers-color-scheme: light) {
:root:not([data-theme])
{
:root:not([data-theme]) {
@include light-theme;
}
}
Expand Down
63 changes: 0 additions & 63 deletions assets/scss/themes/highlight/_dark.scss

This file was deleted.

67 changes: 0 additions & 67 deletions assets/scss/themes/highlight/_light.scss

This file was deleted.

0 comments on commit 9c6e869

Please sign in to comment.