forked from fluid-dev/hexo-theme-fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,7 @@ | ||
<% | ||
import_script(`<script> | ||
(function() { | ||
var enableLang = CONFIG.code_language.enable && CONFIG.code_language.default; | ||
var enableCopy = CONFIG.copy_btn; | ||
if (!enableLang && !enableCopy) { | ||
return; | ||
} | ||
function getBgClass(ele) { | ||
return Fluid.utils.getBackgroundLightness(ele) >= 0 ? 'code-widget-light' : 'code-widget-dark'; | ||
} | ||
var copyTmpl = ''; | ||
copyTmpl += '<div class="code-widget">'; | ||
copyTmpl += 'LANG'; | ||
copyTmpl += '</div>'; | ||
jQuery('.markdown-body pre').each(function() { | ||
var $pre = jQuery(this); | ||
if ($pre.find('code.mermaid').length > 0) { | ||
return; | ||
} | ||
if ($pre.find('span.line').length > 0) { | ||
return; | ||
} | ||
var lang = ''; | ||
if (enableLang) { | ||
lang = CONFIG.code_language.default; | ||
if ($pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2 && $pre.children().hasClass('hljs')) { | ||
lang = $pre[0].children[0].classList[1]; | ||
} else if ($pre[0].getAttribute('data-language')) { | ||
lang = $pre[0].getAttribute('data-language'); | ||
} else if ($pre.parent().hasClass('sourceCode') && $pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2) { | ||
lang = $pre[0].children[0].classList[1]; | ||
$pre.parent().addClass('code-wrapper'); | ||
} else if ($pre.parent().hasClass('markdown-body') && $pre[0].classList.length === 0) { | ||
$pre.wrap('<div class="code-wrapper"></div>'); | ||
} | ||
lang = lang.toUpperCase().replace('NONE', CONFIG.code_language.default); | ||
} | ||
$pre.append(copyTmpl.replace('LANG', lang).replace('code-widget">', | ||
getBgClass($pre[0]) + (enableCopy ? ' code-widget copy-btn" data-clipboard-snippet><i class="iconfont icon-copy"></i>' : ' code-widget">'))); | ||
if (enableCopy) { | ||
Fluid.utils.createScript('${url_join(theme.static_prefix.clipboard, 'clipboard.min.js')}', function() { | ||
var clipboard = new window.ClipboardJS('.copy-btn', { | ||
target: function(trigger) { | ||
var nodes = trigger.parentNode.childNodes; | ||
for (var i = 0; i < nodes.length; i++) { | ||
if (nodes[i].tagName === 'CODE') { | ||
return nodes[i]; | ||
} | ||
} | ||
} | ||
}); | ||
clipboard.on('success', function(e) { | ||
e.clearSelection(); | ||
e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-copy', 'icon-success'); | ||
setTimeout(function() { | ||
e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-success', 'icon-copy'); | ||
}, 2000); | ||
}); | ||
}); | ||
} | ||
}); | ||
})(); | ||
</script> | ||
if (theme.code.copy_btn) { | ||
import_script(`<script src=${url_join(theme.static_prefix.clipboard, 'clipboard.min.js')}></script>`) | ||
} | ||
import_script(`<script>Fluid.plugins.codeWidget();</script> | ||
`) | ||
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters