Skip to content

Commit 8354a9c

Browse files
ctruedenclaude
andcommitted
Theme-aware GitHub code embeds (light/dark)
Replace the static emgithub <script> tag with a <div data-src-base> placeholder. code.js injects the <script> at runtime with &style=github or &style=github-dark based on the active theme, and a MutationObserver on data-theme re-renders embeds whenever the user toggles light/dark mode. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 473fbe3 commit 8354a9c

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

_includes/code

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
{%- assign line-start = include.line-start | default: "1" -%}
77
{%- assign line-end = include.line-end | default: "99999" -%}
88
{%- assign label = include.label -%}
9-
{%- assign style = include.style | default: "github" -%}
109
{%- assign show-border = include.show-border | default: true -%}
1110
{%- assign show-line-numbers = include.show-line-numbers | default: true -%}
1211
{%- assign show-file-meta = include.show-file-meta | default: true -%}
@@ -41,14 +40,14 @@
4140
{%- capture content -%}
4241
{%- case service -%}
4342
{%- when "github" -%}
44-
<script src="https://emgithub.com/embed.js?target=
45-
{{- url | replace: "/", "%2F" | replace: "#", "%23" | replace: ":", "%3A" -}}
46-
&style={{style}}
43+
{%- assign embed-target = url | strip | replace: "/", "%2F" | replace: "#", "%23" | replace: ":", "%3A" -%}
44+
{%- capture embed-params -%}
4745
{%- if show-border -%} &showBorder=on {%- endif -%}
4846
{%- if show-line-numbers -%} &showLineNumbers=on {%- endif -%}
4947
{%- if show-file-meta -%} &showFileMeta=on {%- endif -%}
5048
{%- if show-copy -%} &showCopy=on {%- endif -%}
51-
"></script>
49+
{%- endcapture -%}
50+
<div class="emgithub-embed" data-src-base="https://emgithub.com/embed-v2.js?target={{embed-target}}{{embed-params | strip}}"></div>
5251
{%- when "gitlab" -%}
5352
{% include notice icon="warning" content="
5453
Embedding content from GitLab is not yet supported.

assets/js/code.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,41 @@ function selectAndCopy(element) {
1818
document.execCommand("copy");
1919
}
2020

21+
// Load emgithub embeds with the correct style for the current light/dark theme.
22+
// The _includes/code template emits a <div class="emgithub-embed" data-src-base="...">
23+
// placeholder (URL without &style=). We inject the <script> tag at runtime so the
24+
// style can match the active theme, and re-inject when the user toggles the theme.
25+
(function () {
26+
function emgithubStyle() {
27+
return document.documentElement.getAttribute('data-theme') === 'dark'
28+
? 'github-dark' : 'github';
29+
}
30+
31+
function loadEmbed(div) {
32+
div.innerHTML = '';
33+
var script = document.createElement('script');
34+
script.src = div.dataset.srcBase + '&style=' + emgithubStyle();
35+
div.appendChild(script);
36+
}
37+
38+
function loadAllEmbeds() {
39+
document.querySelectorAll('.emgithub-embed').forEach(loadEmbed);
40+
}
41+
42+
if (document.readyState === 'loading') {
43+
document.addEventListener('DOMContentLoaded', loadAllEmbeds);
44+
} else {
45+
loadAllEmbeds();
46+
}
47+
48+
// Re-render when the user toggles light/dark (theme.js sets data-theme on <html>).
49+
new MutationObserver(function (mutations) {
50+
for (var i = 0; i < mutations.length; i++) {
51+
if (mutations[i].attributeName === 'data-theme') { loadAllEmbeds(); return; }
52+
}
53+
}).observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
54+
})();
55+
2156
// Splice in a "Copy" button next to each code block.
2257
document.querySelectorAll("pre").forEach(function(pre) {
2358
if (pre.childElementCount != 1) return;

0 commit comments

Comments
 (0)