Add code block header

This commit is contained in:
Cotes Chung 2021-09-09 21:07:07 +08:00
parent 39b09b97c0
commit 7dc7a7efc8
5 changed files with 55 additions and 48 deletions

View File

@ -113,8 +113,13 @@
{% assign _new_content = _snippet %} {% assign _new_content = _snippet %}
{% else %} {% else %}
{% assign _lang = _snippet | split: ' ' | first %} {% assign _lang = _snippet | split: ' ' | first %}
{% capture _tag_head %}<div lang="{{_lang}}" class="language-{% endcapture %} {% capture _tag_head %}<div class="language-{% endcapture %}
{% assign _new_content = _new_content | append: _tag_head | append: _snippet %} {% assign _replacement = '-rouge"><div class="code-header" data-lang="'
| append: _lang
| append: '"></div><div class="highlight">'
%}
{% capture _tag_tail %}{{ _snippet | replace: '-rouge"><div class="highlight">', _replacement }}{% endcapture %}
{% assign _new_content = _new_content | append: _tag_head | append: _tag_tail %}
{% endif %} {% endif %}
{% endfor %} {% endfor %}

View File

@ -27,16 +27,18 @@ html[mode=dark] {
/* -- Codes Snippet -- */ /* -- Codes Snippet -- */
$code-radius: 6px;
%code-snippet-bg { %code-snippet-bg {
background: var(--highlight-bg-color); background: var(--highlight-bg-color);
} }
%code-snippet-radius { %code-snippet-radius {
border-radius: 6px; border-radius: $code-radius;
} }
%code-snippet-padding { %code-snippet-padding {
padding: 1.5rem; padding: 1.2rem;
} }
div > pre { div > pre {
@ -86,12 +88,10 @@ div > pre {
} }
.lineno { .lineno {
margin-left: 0.2rem;
padding-right: 0.5rem; padding-right: 0.5rem;
min-width: 2.2rem; min-width: 2.2rem;
text-align: right; text-align: right;
color: var(--highlight-lineno-color); color: var(--highlight-lineno-color);
border-right: 1px solid var(--highlight-lineno-border-color);
-webkit-user-select: none; -webkit-user-select: none;
-khtml-user-select: none; -khtml-user-select: none;
-moz-user-select: none; -moz-user-select: none;
@ -135,7 +135,7 @@ code {
} }
td.rouge-code { td.rouge-code {
padding: 1.5rem 1.5rem 1.5rem 1rem; padding: 1.2rem 1.5rem 1.2rem 1rem;
// Prevent some browser extends from // Prevent some browser extends from
// changing the URL string of code block. // changing the URL string of code block.
@ -162,25 +162,33 @@ div {
} }
} }
div[class^='language-']::before { .code-header {
content: attr(lang); background: var(--code-header-bg);
position: absolute; border-top-left-radius: $code-radius;
right: 2rem; border-top-right-radius: $code-radius;
margin-top: 3px;
font-size: 0.7rem;
font-weight: 600;
color: var(--highlight-lineno-color);
text-transform: uppercase;
}
@media (min-width: 768px) { // language badge
div[class^='language-']::before { &::before {
right: 3.1rem; content: attr(data-lang);
color: var(--lang-badge-color);
font-size: 0.75rem;
font-weight: 600;
text-transform: uppercase;
padding-left: 1em;
} }
} }
@media (min-width: 1650px) { @media all and (max-width: 576px) {
div[class^='language-']::before { .post-content {
right: 3.5rem; > div[class^='language-'] {
@include ml-mr(-1.25rem);
border-radius: 0;
.code-header {
border-radius: 0;
padding: 0 0.4rem;
}
}
} }
} }

View File

@ -3,20 +3,6 @@
*/ */
@mixin dark-syntax { @mixin dark-syntax {
/* ----- My styles ------ */
--highlight-bg-color: #252525;
--highlighter-rouge-color: #de6b18;
--highlight-lineno-color: #6c6c6d;
--highlight-lineno-border-color: #303435;
--inline-code-bg: #272822;
.highlight {
.gp { color: #818c96; }
}
pre { color: #bfbfbf; } /* override Bootstrap */
kbd { background-color: black; }
/* syntax highlight colors from https://raw.githubusercontent.com/jwarby/pygments-css/master/monokai.css */ /* syntax highlight colors from https://raw.githubusercontent.com/jwarby/pygments-css/master/monokai.css */
.highlight pre { background-color: var(--highlight-bg-color); } .highlight pre { background-color: var(--highlight-bg-color); }
.highlight .hll { background-color: var(--highlight-bg-color); } .highlight .hll { background-color: var(--highlight-bg-color); }
@ -81,4 +67,20 @@
.highlight .gu { color: #75715e; } /* Generic.Subheading & Diff Unified/Comment? */ .highlight .gu { color: #75715e; } /* Generic.Subheading & Diff Unified/Comment? */
.highlight .gd { color: #f92672; background-color: #561c08; } /* Generic.Deleted & Diff Deleted */ .highlight .gd { color: #f92672; background-color: #561c08; } /* Generic.Deleted & Diff Deleted */
.highlight .gi { color: #a6e22e; background-color: #0b5858; } /* Generic.Inserted & Diff Inserted */ .highlight .gi { color: #a6e22e; background-color: #0b5858; } /* Generic.Inserted & Diff Inserted */
/* ----- custom styles ------ */
--highlight-bg-color: #252525;
--highlighter-rouge-color: #de6b18;
--highlight-lineno-color: #6c6c6d;
--inline-code-bg: #272822;
--code-header-bg: #353535;
--lang-badge-color: #6c6c6d;
.highlight {
.gp { color: #818c96; }
}
pre { color: #bfbfbf; } /* override Bootstrap */
kbd { background-color: black; }
} }

View File

@ -70,6 +70,8 @@
--highlight-bg-color: #f7f7f7; --highlight-bg-color: #f7f7f7;
--highlighter-rouge-color: #2f2f2f; --highlighter-rouge-color: #2f2f2f;
--highlight-lineno-color: #c2c6cc; --highlight-lineno-color: #c2c6cc;
--highlight-lineno-border-color: #e9ecef;
--inline-code-bg: #f3f3f3; --inline-code-bg: #f3f3f3;
--code-header-bg: #eaeaea;
--lang-badge-color: #a4a7ab;
} // light-syntax } // light-syntax

View File

@ -299,16 +299,6 @@
margin-top: 1rem; margin-top: 1rem;
} }
} }
.post-content > div[class^='language-'] {
@include ml-mr(-1.25rem);
border-radius: 0;
&::before { // the lang badge
right: 1rem;
}
}
} }
@media all and (max-width: 768px) { @media all and (max-width: 768px) {