Improve how plugin javascript is loaded

- Load the plugins after Reveal.initialize or some plugins don't work
- Put the object.assign polyfill in its own file
This commit is contained in:
dzello
2019-03-17 00:52:21 +01:00
parent 7aad664abf
commit fc11a37889
2 changed files with 47 additions and 41 deletions
+18 -41
View File
@@ -1,3 +1,5 @@
<!-- polyfill needed for IE11 and below -->
<script type="text/javascript" src={{ "reveal-hugo/object-assign.js" | relURL }}></script>
<!-- Printing and PDF exports -->
<!-- use Hugo to create the right path for the print stylesheet, then conditionally include it -->
{{- $reveal_location := $.Param "reveal_hugo.reveal_cdn" | default "reveal-js" -}}
@@ -17,48 +19,7 @@
<script src="{{ printf "%s/lib/js/head.min.js" $reveal_location | relURL }}"></script>
<script src="{{ printf "%s/js/reveal.js" $reveal_location | relURL }}"></script>
<!-- load Reveal.js plugins -->
<script type="text/javascript" src="{{ printf "%s/lib/js/classList.js" $reveal_location | relURL }}"></script>
{{ $default_plugins := slice "plugin/markdown/marked.js" "plugin/markdown/markdown.js" "plugin/highlight/highlight.js" "plugin/zoom-js/zoom.js" }}
{{ range $default_plugins }}
<script type="text/javascript" src="{{ printf "%s/%s" $reveal_location . | relURL }}"></script>
{{ end }}
<!-- always use local version of the notes plugin since HTML file it requires isn't on CDN -->
<script type="text/javascript" src="{{ "reveal-js/plugin/notes/notes.js" | relURL }}"></script>
<!-- load custom plugins locally only (not CDN since many plugins won't exist there) -->
{{ range $.Param "reveal_hugo.plugins" }}
<script type="text/javascript" src="{{ . | relURL }}"></script>
{{ end }}
<script type="text/javascript">
// polyfill needed for IE11 and below
if (typeof Object.assign != 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
// Hugo lowercases all params and Reveal.js needs camelcase
// So params in Hugo must be stored in snakecase and we camelize them here
function camelize(map) {
@@ -83,5 +44,21 @@
camelize(revealHugoSiteParams),
camelize(revealHugoPageParams));
Reveal.initialize(options);
</script>
<!-- load Reveal.js plugins after Reveal.js is initialized -->
<script type="text/javascript" src="{{ printf "%s/lib/js/classList.js" $reveal_location | relURL }}"></script>
{{ $default_plugins := slice "plugin/markdown/marked.js" "plugin/markdown/markdown.js" "plugin/highlight/highlight.js" "plugin/zoom-js/zoom.js" }}
{{ range $default_plugins }}
<script type="text/javascript" src="{{ printf "%s/%s" $reveal_location . | relURL }}"></script>
{{ end }}
<!-- always use local version of the notes plugin since HTML file it requires isn't on CDN -->
<script type="text/javascript" src="{{ "reveal-js/plugin/notes/notes.js" | relURL }}"></script>
<!-- load custom plugins locally only (not CDN since many plugins won't exist there) -->
{{ range $.Param "reveal_hugo.plugins" }}
<script type="text/javascript" src="{{ . | relURL }}"></script>
{{ end }}
<!-- init highlighting plugin if it was loaded -->
<script type="text/javascript">
if (hljs)
hljs.initHighlightingOnLoad();
</script>
+29
View File
@@ -0,0 +1,29 @@
if (typeof Object.assign != 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}