@farberg/reveal-template 1.1.16 → 1.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -67,10 +67,69 @@ function extractBeginEndSnippet(code, beginMarker, endMarker) {
|
|
|
67
67
|
return out.trim();
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
function injectStyles() {
|
|
71
|
+
const style = document.createElement('style');
|
|
72
|
+
style.textContent = `
|
|
73
|
+
pre.with-copy-btn {
|
|
74
|
+
position: relative;
|
|
75
|
+
}
|
|
76
|
+
.copy-code-btn {
|
|
77
|
+
position: absolute;
|
|
78
|
+
top: 0.4em;
|
|
79
|
+
right: 0.4em;
|
|
80
|
+
padding: 0.2em 0.5em;
|
|
81
|
+
font-size: 0.75em;
|
|
82
|
+
font-family: sans-serif;
|
|
83
|
+
background: rgba(255, 255, 255, 0.15);
|
|
84
|
+
color: #ccc;
|
|
85
|
+
border: 1px solid rgba(255, 255, 255, 0.25);
|
|
86
|
+
border-radius: 4px;
|
|
87
|
+
cursor: pointer;
|
|
88
|
+
opacity: 0;
|
|
89
|
+
transition: opacity 0.15s ease, background 0.15s ease;
|
|
90
|
+
z-index: 10;
|
|
91
|
+
line-height: 1.4;
|
|
92
|
+
}
|
|
93
|
+
pre.with-copy-btn:hover .copy-code-btn {
|
|
94
|
+
opacity: 1;
|
|
95
|
+
}
|
|
96
|
+
.copy-code-btn:hover {
|
|
97
|
+
background: rgba(255, 255, 255, 0.3);
|
|
98
|
+
color: #fff;
|
|
99
|
+
}
|
|
100
|
+
.copy-code-btn.copied {
|
|
101
|
+
color: #4caf50;
|
|
102
|
+
border-color: #4caf50;
|
|
103
|
+
}
|
|
104
|
+
`;
|
|
105
|
+
document.head.appendChild(style);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function addCopyButton(preEl) {
|
|
109
|
+
preEl.classList.add('with-copy-btn');
|
|
110
|
+
const btn = document.createElement('button');
|
|
111
|
+
btn.className = 'copy-code-btn';
|
|
112
|
+
btn.textContent = 'Copy';
|
|
113
|
+
btn.addEventListener('click', () => {
|
|
114
|
+
const code = preEl.querySelector('code');
|
|
115
|
+
const text = code ? code.innerText : preEl.innerText;
|
|
116
|
+
navigator.clipboard.writeText(text).then(() => {
|
|
117
|
+
btn.textContent = 'Copied!';
|
|
118
|
+
btn.classList.add('copied');
|
|
119
|
+
setTimeout(() => {
|
|
120
|
+
btn.textContent = 'Copy';
|
|
121
|
+
btn.classList.remove('copied');
|
|
122
|
+
}, 1500);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
preEl.appendChild(btn);
|
|
126
|
+
}
|
|
127
|
+
|
|
70
128
|
export default () => {
|
|
71
129
|
return {
|
|
72
130
|
id: 'show_code_snippets',
|
|
73
131
|
init: (deck) => {
|
|
132
|
+
injectStyles();
|
|
74
133
|
|
|
75
134
|
deck.on('ready', async () => {
|
|
76
135
|
const highlightPlugin = deck.getPlugin("highlight")
|
|
@@ -80,6 +139,17 @@ export default () => {
|
|
|
80
139
|
ignoreUnescapedHTML: true
|
|
81
140
|
});
|
|
82
141
|
|
|
142
|
+
// highlightOnLoad is false (to avoid double-highlighting external code snippets
|
|
143
|
+
// loaded below). Manually highlight all inline markdown code blocks here.
|
|
144
|
+
// The reveal.js markdown plugin sets class="mermaid" (no "language-" prefix) for
|
|
145
|
+
// mermaid fences. RevealMermaid already rendered those elements before the ready
|
|
146
|
+
// event fired, so we must skip them — otherwise hljs reads the SVG innerHTML as
|
|
147
|
+
// code text and overwrites the rendered diagram.
|
|
148
|
+
for (let el of deck.getRevealElement().querySelectorAll("pre code[class]")) {
|
|
149
|
+
if (!el.classList.contains("mermaid")) {
|
|
150
|
+
highlightPlugin.hljs.highlightElement(el);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
83
153
|
|
|
84
154
|
for (let el of deck.getRevealElement().querySelectorAll("a[data-code]")) {
|
|
85
155
|
//console.log(`Loading code snippets, looking at`, el)
|
|
@@ -122,7 +192,7 @@ export default () => {
|
|
|
122
192
|
|
|
123
193
|
const newEl = showCode(el, language, code, showLink ? url : null, outdent)
|
|
124
194
|
if (language !== 'mermaid')
|
|
125
|
-
highlightPlugin.hljs.highlightElement(newEl)
|
|
195
|
+
highlightPlugin.hljs.highlightElement(newEl.querySelector('code') ?? newEl)
|
|
126
196
|
} catch (err) {
|
|
127
197
|
console.error(`show-code-snippets: failed to load ${url}:`, err)
|
|
128
198
|
showError(el, `Failed to load ${url}: ${err.message}`)
|