Skip to content

Commit

Permalink
Feature: display the language of code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Dec 18, 2020
1 parent b759787 commit d5e6e5b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions assets/css/_addon/syntax.scss
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,20 @@ div {
}
}
}

[class^='language-']::before {
content: attr(lang);
position: absolute;
right: 1.8rem;
margin-top: 3px;
font-size: .7rem;
font-weight: 600;
color: var(--highlight-lineno-color);
text-transform: uppercase;
}

@media (min-width: 768px) {
[class^='language-']::before {
right: 3.1rem;
}
}
20 changes: 20 additions & 0 deletions assets/js/_utils/lang-badge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Add language indicator to code snippets
*/

$(function() {
const prefix = "language-";
const regex = new RegExp(`^${prefix}([a-z])+$`);

$(`div[class^=${prefix}`).each(function() {
let clzsses = $(this).attr("class").split(" ");

clzsses.forEach((clzss) => {
if (regex.test(clzss)) {
let lang = clzss.substring(prefix.length);
$(this).attr("lang", `${lang}`);
}
});

});
});
2 changes: 2 additions & 0 deletions assets/js/post.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ layout: compress
{% include_relative _utils/toc-filter.js %}

{% include_relative _utils/img-hyperlink.js %}

{% include_relative _utils/lang-badge.js %}

0 comments on commit d5e6e5b

Please sign in to comment.