Skip to content

Commit beaa17f

Browse files
committed
Load required JS if not loaded
1 parent b5344ed commit beaa17f

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

assets/author-reveal.html

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,26 @@
1111
</template>
1212

1313
<script>
14-
require.config({
15-
paths: {
16-
'js-yaml': 'https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min'
17-
}
18-
});
14+
// First, load RequireJS if it's not already loaded
15+
if (typeof require === 'undefined') {
16+
const requireScript = document.createElement('script');
17+
requireScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js';
18+
requireScript.onload = function() {
19+
initAuthorReveal();
20+
};
21+
document.head.appendChild(requireScript);
22+
} else {
23+
initAuthorReveal();
24+
}
25+
26+
function initAuthorReveal() {
27+
require.config({
28+
paths: {
29+
'js-yaml': 'https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min'
30+
}
31+
});
1932

20-
require(['js-yaml'], function (jsyaml) {
33+
require(['js-yaml'], function (jsyaml) {
2134
// Function to get last name from full name
2235
function getLastName(fullName) {
2336
const nameParts = fullName.trim().split(' ');
@@ -39,6 +52,10 @@
3952
const authorYamlPath = baseUrl + '/authors.yml';
4053

4154
const response = await fetch(authorYamlPath);
55+
if (!response.ok) {
56+
throw new Error(`HTTP error! status: ${response.status}`);
57+
}
58+
4259
const yamlText = await response.text();
4360
window.authorData = jsyaml.load(yamlText);
4461
initializeAuthors();
@@ -50,10 +67,15 @@
5067
function initializeAuthors() {
5168
// Find the quarto-title-meta div
5269
const titleMeta = document.querySelector('.quarto-title-meta');
53-
if (!titleMeta) return;
70+
if (!titleMeta) {
71+
return;
72+
}
5473

5574
// Get template content
5675
const template = document.getElementById('authors-template');
76+
if (!template) {
77+
return;
78+
}
5779
const authorsSection = template.content.cloneNode(true);
5880

5981
// Get the authors content div from the cloned template
@@ -114,6 +136,10 @@
114136
const toggle = document.querySelector('.authors-toggle');
115137
const content = document.querySelector('.authors-content');
116138

139+
if (!toggle || !content) {
140+
return;
141+
}
142+
117143
toggle.addEventListener('click', () => {
118144
const isExpanded = toggle.getAttribute('aria-expanded') === 'true';
119145
toggle.setAttribute('aria-expanded', !isExpanded);
@@ -132,5 +158,6 @@
132158
} else {
133159
loadAuthorData();
134160
}
135-
});
161+
});
162+
}
136163
</script>

0 commit comments

Comments
 (0)