Skip to content

Commit

Permalink
Optimize prism: support copy_button
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Aug 1, 2020
1 parent 74ccc18 commit 04da779
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
18 changes: 12 additions & 6 deletions scripts/events/lib/highlight.js
Expand Up @@ -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 = '';
Expand All @@ -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;
}

Expand All @@ -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')
};
};
2 changes: 1 addition & 1 deletion 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;
}

Expand Down
11 changes: 6 additions & 5 deletions 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'));
}
}

Expand Down Expand Up @@ -117,6 +117,7 @@ pre {
@extend $code-block;
overflow: auto;
padding: 10px;
position: relative;

code {
background: none;
Expand Down
7 changes: 5 additions & 2 deletions source/js/utils.js
Expand Up @@ -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}`);
Expand All @@ -81,7 +83,8 @@ NexT.utils = {
element.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-clipboard fa-fw"></i></div>');
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';
Expand Down

1 comment on commit 04da779

@stevenjoezhang
Copy link
Member Author

@stevenjoezhang stevenjoezhang commented on 04da779 Aug 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.