Skip to content

Commit beabb36

Browse files
committed
Add custom title to code block
1 parent 42ff0f2 commit beabb36

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

_includes/refactor-content.html

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,39 @@
104104

105105
<!-- Add header for code snippets -->
106106

107-
{% if _content contains '<div class="language-' %}
108-
{% assign _code_splits = _content | split: '<div class="language-' %}
109-
{% assign _new_content = nil %}
107+
{% if _content contains '<div class="highlight"><code>' %}
108+
{% assign _code_spippets = _content | split: '<div class="highlight"><code>' %}
109+
{% assign _new_content = '' %}
110+
111+
{% for _snippet in _code_spippets %}
112+
113+
{% if forloop.last %}
114+
{% assign _new_content = _new_content | append: _snippet %}
110115

111-
{% for _snippet in _code_splits %}
112-
{% if forloop.first %}
113-
{% assign _new_content = _snippet %}
114116
{% else %}
115-
{% assign _lang = _snippet | split: ' ' | first %}
116-
{% capture _tag_head %}<div class="language-{% endcapture %}
117-
{% assign _replacement = '-rouge"><div class="code-header" data-lang="'
118-
| append: _lang
117+
118+
{% assign _left = _snippet | split: '><' | last%}
119+
120+
{% if _left contains 'file="' %}
121+
{% assign _text = _left | split: 'file="' | last | split: '"' | first %}
122+
{% else %}
123+
{% assign _text = _left | split: 'language-' | last | split: ' ' | first %}
124+
{% endif %}
125+
126+
{% assign _new_content = _new_content | append: _snippet
127+
| append: '<div class="code-header" text-data="'
128+
| append: _text
119129
| append: '"><button data-original-title="'
120130
| append: site.data.locales[lang].post.button.copy
121-
| append: '"><i class="far fa-clone"></i></button></div><div class="highlight">'
131+
| append: '"><i class="far fa-clone"></i></button></div>'
132+
| append: '<div class="highlight"><code>'
122133
%}
123-
{% capture _tag_tail %}{{ _snippet | replace: '-rouge"><div class="highlight">', _replacement }}{% endcapture %}
124-
{% assign _new_content = _new_content | append: _tag_head | append: _tag_tail %}
134+
125135
{% endif %}
136+
126137
{% endfor %}
127138

128-
{% if _new_content %}
129-
{% assign _content = _new_content %}
130-
{% endif %}
139+
{% assign _content = _new_content %}
131140

132141
{% endif %}
133142

_sass/addon/syntax.scss

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,22 @@ div {
172172
align-items: center;
173173
line-height: 1.85rem;
174174

175-
// language badge
175+
// text data
176176
&::before {
177-
content: attr(data-lang);
177+
content: attr(text-data);
178178
color: var(--lang-badge-color);
179+
padding-left: 1em;
180+
181+
// language
179182
font-size: 0.75rem;
180183
font-weight: 600;
181184
text-transform: uppercase;
182-
padding-left: 1em;
185+
186+
// file name
187+
@at-root [file] #{&} {
188+
font-size: 0.85rem;
189+
text-transform: none;
190+
}
183191
}
184192

185193
// clipboard

0 commit comments

Comments
 (0)