From 04da779212bc7647c90ad406c60e6a118a0baed4 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sun, 2 Aug 2020 00:23:28 +0800 Subject: [PATCH] Optimize prism: support copy_button --- scripts/events/lib/highlight.js | 18 ++++++++++++------ .../scaffolding/highlight/copy-code.styl | 2 +- .../scaffolding/highlight/highlight.styl | 11 ++++++----- source/js/utils.js | 7 +++++-- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/scripts/events/lib/highlight.js b/scripts/events/lib/highlight.js index 86d61cb..c497b86 100644 --- a/scripts/events/lib/highlight.js +++ b/scripts/events/lib/highlight.js @@ -3,12 +3,18 @@ const fs = require('fs'); const path = require('path'); -function resolve(name) { - return path.dirname(require.resolve(`${name}/package.json`)); +function resolve(name, file) { + let dir; + try { + dir = path.dirname(require.resolve(`${name}/package.json`)); + } catch (error) { + return ''; + } + return `${dir}/${file}`; } function highlightTheme(name) { - const file = `${resolve('highlight.js')}/styles/${name}.css`; + const file = resolve('highlight.js', `styles/${name}.css`); const css = fs.readFileSync(file).toString(); let rule = ''; let background = ''; @@ -30,8 +36,8 @@ function highlightTheme(name) { } function prismTheme(name) { - let file = `${resolve('prismjs')}/themes/${name}.css`; - if (!fs.existsSync(file)) file = `${resolve('prism-themes')}/themes/${name}.css`; + let file = resolve('prismjs', `themes/${name}.css`); + if (!fs.existsSync(file)) file = resolve('prism-themes', `themes/${name}.css`); return file; } @@ -49,6 +55,6 @@ module.exports = hexo => { enable: config.prismjs.enable, light : prismTheme(theme.codeblock.prism.light), dark : prismTheme(theme.codeblock.prism.dark), - number: `${resolve('prismjs')}/plugins/line-numbers/prism-line-numbers.css` + number: resolve('prismjs', 'plugins/line-numbers/prism-line-numbers.css') }; }; diff --git a/source/css/_common/scaffolding/highlight/copy-code.styl b/source/css/_common/scaffolding/highlight/copy-code.styl index b8fb039..d26a481 100644 --- a/source/css/_common/scaffolding/highlight/copy-code.styl +++ b/source/css/_common/scaffolding/highlight/copy-code.styl @@ -1,4 +1,4 @@ -.highlight:hover .copy-btn, .highlight .copy-btn:focus { +.highlight:hover .copy-btn, pre:hover .copy-btn { opacity: 1; } diff --git a/source/css/_common/scaffolding/highlight/highlight.styl b/source/css/_common/scaffolding/highlight/highlight.styl index e226e15..a6b5f9a 100644 --- a/source/css/_common/scaffolding/highlight/highlight.styl +++ b/source/css/_common/scaffolding/highlight/highlight.styl @@ -1,14 +1,14 @@ // Use `@require` to fix issue #67 -@require hexo-config('highlight.light.file') if (hexo-config('highlight.enable')); +@require hexo-config('highlight.light.file') if (hexo-config('highlight.enable') && hexo-config('highlight.light.file')); if (hexo-config('prism.enable')) { - @require hexo-config('prism.light'); - @require hexo-config('prism.number'); + @require hexo-config('prism.light') if (hexo-config('prism.light')); + @require hexo-config('prism.number') if (hexo-config('prism.number')); } if (hexo-config('darkmode')) { @media (prefers-color-scheme: dark) { - @require hexo-config('highlight.dark.file') if (hexo-config('highlight.enable')); - @require hexo-config('prism.dark') if (hexo-config('prism.enable')); + @require hexo-config('highlight.dark.file') if (hexo-config('highlight.enable') && hexo-config('highlight.dark.file')); + @require hexo-config('prism.dark') if (hexo-config('prism.enable') && hexo-config('prism.dark')); } } @@ -117,6 +117,7 @@ pre { @extend $code-block; overflow: auto; padding: 10px; + position: relative; code { background: none; diff --git a/source/js/utils.js b/source/js/utils.js index 16cd211..157232b 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -71,7 +71,9 @@ NexT.utils = { * One-click copy code support. */ registerCopyCode: function() { - document.querySelectorAll('figure.highlight').forEach(element => { + let figure = document.querySelectorAll('figure.highlight'); + if (figure.length === 0) figure = document.querySelectorAll('pre'); + figure.forEach(element => { element.querySelectorAll('.code .line span').forEach(span => { span.classList.forEach(name => { span.classList.replace(name, `hljs-${name}`); @@ -81,7 +83,8 @@ NexT.utils = { element.insertAdjacentHTML('beforeend', '
'); const button = element.querySelector('.copy-btn'); button.addEventListener('click', () => { - const code = [...button.parentNode.querySelectorAll('.code .line')].map(line => line.innerText).join('\n'); + const lines = element.querySelector('.code') || element.querySelector('code'); + const code = lines.innerText; const ta = document.createElement('textarea'); ta.style.top = window.scrollY + 'px'; // Prevent page scrolling ta.style.position = 'absolute';