Skip to content

Commit

Permalink
Support using highlight.js
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed May 8, 2020
1 parent 9527ef6 commit 9fdaba2
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 198 deletions.
5 changes: 2 additions & 3 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,8 @@ custom_logo: #/uploads/custom-logo.jpg

codeblock:
# Code Highlight theme
# Available values: normal | night | night eighties | night blue | night bright | solarized | solarized dark | galactic
# See: https://github.com/chriskempson/tomorrow-theme
highlight_theme: normal
# See: https://github.com/highlightjs/highlight.js/tree/master/src/styles
highlight_theme: default
# Add copy button on codeblock
copy_button:
enable: false
Expand Down
2 changes: 2 additions & 0 deletions scripts/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ hexo.on('generateBefore', () => {
require('./lib/config')(hexo);
// Add filter type `theme_inject`
require('./lib/injects')(hexo);
// Highlight
require('./lib/highlight')(hexo);
});

hexo.on('exit', () => {
Expand Down
28 changes: 28 additions & 0 deletions scripts/events/lib/highlight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs');

function parse(file) {
let css = fs.readFileSync(file).toString();
let rule = '';
let background = '';
let foreground = '';
css.replace(/\.hljs([^\S]+|,[^\{]+)\{(.*?)\}/sg, (match, $1, content) => {
rule += content;
return match;
});
rule.split('\n').forEach(line => {
if (line.includes('background:')) background = line.split('background:')[1];
else if (line.includes('background-color:')) background = line.split('background-color:')[1];
else if (line.includes('color:')) foreground = line.split('color:')[1];
});
return {
file,
background,
foreground
};
}

module.exports = hexo => {
hexo.config.highlight.hljs = false;

This comment has been minimized.

Copy link
@mans0n

mans0n Jun 13, 2020

Why is this set false here? I had to remove this line to make highlight work.

This comment has been minimized.

Copy link
@stevenjoezhang

stevenjoezhang Jun 13, 2020

Author Member

Because hljs: true will break the style of the codeblock.
Run hexo clean or remove all custom files in the theme and try again.

This comment has been minimized.

Copy link
@mans0n

mans0n Jun 13, 2020

I opened #29.

let file = `${hexo.plugin_dir}highlight.js/styles/${hexo.theme.config.codeblock.highlight_theme}.css`;
hexo.theme.config.highlight = parse(file);
};
1 change: 0 additions & 1 deletion source/css/_common/scaffolding/highlight/copy-code.styl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

if (hexo-config('codeblock.copy_button.style') == 'mac') {
.highlight {
background: #21252b;
border-radius: 5px;
box-shadow: 0 10px 30px 0 rgba(0, 0, 0, .4);
padding-top: 30px;
Expand Down
7 changes: 0 additions & 7 deletions source/css/_common/scaffolding/highlight/diff.styl

This file was deleted.

57 changes: 7 additions & 50 deletions source/css/_common/scaffolding/highlight/highlight.styl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// https://github.com/chriskempson/tomorrow-theme
$highlight-theme = hexo-config('codeblock.highlight_theme');

@import 'theme';
@import 'diff';
@import hexo-config('highlight.file');
$highlight-background = convert(hexo-config('highlight.background'));
$highlight-foreground = convert(hexo-config('highlight.foreground'));
$highlight-gutter = {
color: mix($highlight-background, $highlight-foreground, 10%),
bg-color: mix($highlight-background, $highlight-foreground, 90%)
};

@import 'copy-code' if (hexo-config('codeblock.copy_button.enable'));

Expand Down Expand Up @@ -30,10 +32,6 @@ code {
@extend $code-block;
position: relative;

*::selection {
background: $highlight-selection;
}

pre {
border: 0;
margin: 0;
Expand Down Expand Up @@ -108,45 +106,4 @@ pre {
padding: 0;
text-shadow: none;
}
// For diff highlight
.deletion {
background: $highlight-deletion;
}

.addition {
background: $highlight-addition;
}

.meta {
color: $highlight-yellow;
disable-user-select();

This comment has been minimized.

Copy link
@stevenjoezhang
}

.comment {
color: $highlight-comment;
}

.variable, .attribute, .tag, .name, .regexp, .ruby .constant, .xml .tag .title, .xml .pi, .xml .doctype, .html .doctype, .css .id, .css .class, .css .pseudo {
color: $highlight-red;
}

.number, .preprocessor, .built_in, .builtin-name, .literal, .params, .constant, .command {
color: $highlight-orange;
}

.ruby .class .title, .css .rules .attribute, .string, .symbol, .value, .inheritance, .header, .ruby .symbol, .xml .cdata, .special, .formula {
color: $highlight-green;
}

.title, .css .hexcolor {
color: $highlight-aqua;
}

.function, .python .decorator, .python .title, .ruby .function .title, .ruby .title .keyword, .perl .sub, .javascript .title, .coffeescript .title {
color: $highlight-blue;
}

.keyword, .javascript .function {
color: $highlight-purple;
}
}
137 changes: 0 additions & 137 deletions source/css/_common/scaffolding/highlight/theme.styl

This file was deleted.

6 changes: 6 additions & 0 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ NexT.utils = {
*/
registerCopyCode: function() {
document.querySelectorAll('figure.highlight').forEach(element => {
element.querySelectorAll('.code .line span').forEach(span => {
span.classList.forEach(name => {
span.classList.remove(name);
span.classList.add(`hljs-${name}`);
});
});
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', event => {
Expand Down

0 comments on commit 9fdaba2

Please sign in to comment.