Skip to content

Commit

Permalink
pass uiRootPath to script using data attribute instead of global vari…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
mojavelinux committed Dec 2, 2022
1 parent 522aaf7 commit 41be3eb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/js/06-copy-to-clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
var CMD_RX = /^\$ (\S[^\\\n]*(\\\n(?!\$ )[^\\\n]*)*)(?=\n|$)/gm
var LINE_CONTINUATION_RX = /( ) *\\\n *|\\\n( ?) */g
var TRAILING_SPACE_RX = / +$/gm

var config = (document.getElementById('site-script') || { dataset: {} }).dataset
var uiRootPath = config.uiRootPath == null ? '.' : config.uiRootPath
var svgAs = config.svgAs
var supportsCopy = window.navigator.clipboard

;[].slice.call(document.querySelectorAll('.doc pre.highlight, .doc .literalblock pre')).forEach(function (pre) {
var code, language, lang, copy, toast, toolbox
Expand All @@ -28,19 +32,19 @@
}
;(toolbox = document.createElement('div')).className = 'source-toolbox'
if (lang) toolbox.appendChild(lang)
if (window.navigator.clipboard) {
if (supportsCopy) {
;(copy = document.createElement('button')).className = 'copy-button'
copy.setAttribute('title', 'Copy to clipboard')
if (config.svgAs === 'svg') {
if (svgAs === 'svg') {
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
svg.setAttribute('class', 'copy-icon')
var use = document.createElementNS('http://www.w3.org/2000/svg', 'use')
use.setAttribute('href', window.uiRootPath + '/img/octicons-16.svg#icon-clippy')
use.setAttribute('href', uiRootPath + '/img/octicons-16.svg#icon-clippy')
svg.appendChild(use)
copy.appendChild(svg)
} else {
var img = document.createElement('img')
img.src = window.uiRootPath + '/img/octicons-16.svg#view-clippy'
img.src = uiRootPath + '/img/octicons-16.svg#view-clippy'
img.alt = 'copy icon'
img.className = 'copy-icon'
copy.appendChild(img)
Expand Down
2 changes: 1 addition & 1 deletion src/partials/footer-scripts.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script src="{{{uiRootPath}}}/js/site.js"></script>
<script id="site-script" src="{{{uiRootPath}}}/js/site.js" data-ui-root-path="{{{uiRootPath}}}"></script>
<script async src="{{{uiRootPath}}}/js/vendor/highlight.js"></script>
<script async src="{{{uiRootPath}}}/js/vendor/{{#if (asciidocExtensionRegistered '@asciidoctor/tabs')}}asciidoctor{{else}}spring{{/if}}-tabs.js"></script>
<script src="{{{uiRootPath}}}/js/vendor/switchtheme.js"></script>
Expand Down
1 change: 0 additions & 1 deletion src/partials/head-scripts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
<script async src="https://www.googletagmanager.com/gtag/js?id={{this}}"></script>
<script>function gtag(){dataLayer.push(arguments)};window.dataLayer=window.dataLayer||[];gtag('js',new Date());gtag('config','{{this}}')</script>
{{/with}}
<script>var uiRootPath = '{{{uiRootPath}}}'</script>

0 comments on commit 41be3eb

Please sign in to comment.