Skip to content

Commit

Permalink
Include additional themes for prism
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Jul 4, 2020
1 parent 06812e0 commit 2e4e1bc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/support.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ supportLabel: Support
supportComment: >
:wave: @{issue-author}, we use the issue tracker exclusively for bug reports
and feature requests. However, this issue appears to be a support request.
Please use our [support channels](https://github.com/next-theme/hexo-theme-next/tree/master#feedback) to get help with the project.
Please use our [support channels](https://github.com/next-theme/hexo-theme-next#feedback) to get help with the project.
# Close issues marked as support requests
close: true
Expand Down
5 changes: 2 additions & 3 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,10 @@ custom_logo: #/uploads/custom-logo.jpg

codeblock:
# Code Highlight theme
# See: https://github.com/highlightjs/highlight.js/tree/master/src/styles
# All available themes: https://theme-next.js.org/highlight/
theme:
light: default
dark: tomorrow-night
# See: https://github.com/PrismJS/prism/tree/master/themes
prism:
light: prism
dark: prism-dark
Expand Down Expand Up @@ -817,7 +816,7 @@ motion:
enable: true
async: false
transition:
# All available Transition variants: https://theme-next.js.org/animate/
# All available transition variants: https://theme-next.js.org/animate/
post_block: fadeIn
post_header: fadeInDown
post_body: fadeInDown
Expand Down
4 changes: 2 additions & 2 deletions scripts/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

'use strict';

hexo.on('generateBefore', () => {
hexo.extend.filter.register('before_generate', () => {
// Merge config.
require('./lib/config')(hexo);
// Add filter type `theme_inject`
require('./lib/injects')(hexo);
// Highlight
require('./lib/highlight')(hexo);
});
}, 0);

hexo.on('ready', () => {
const { version } = require('../../package.json');
Expand Down
24 changes: 18 additions & 6 deletions scripts/events/lib/highlight.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const fs = require('fs');
const path = require('path');

function parse(file) {
function resolve(package) {
return path.dirname(require.resolve(`${package}/package.json`));
}

function highlightTheme(name) {
let file = `${resolve('highlight.js')}/styles/${name}.css`;
let css = fs.readFileSync(file).toString();
let rule = '';
let background = '';
Expand All @@ -21,20 +27,26 @@ function parse(file) {
};
}

function prismTheme(name) {
let file = `${resolve('prismjs')}/themes/${name}.css`;
if (!fs.existsSync(file)) file = `${resolve('prism-themes')}/themes/${name}.css`;
return file;
}

module.exports = hexo => {
let { config } = hexo;
let theme = hexo.theme.config;
config.highlight.hljs = false;
config.prismjs = config.prismjs || {};
theme.highlight = {
enable: config.highlight.enable && !config.prismjs.enable,
light : parse(`${hexo.plugin_dir}highlight.js/styles/${theme.codeblock.theme.light}.css`),
dark : parse(`${hexo.plugin_dir}highlight.js/styles/${theme.codeblock.theme.dark}.css`)
light : highlightTheme(theme.codeblock.theme.light),
dark : highlightTheme(theme.codeblock.theme.dark)
};
theme.prism = {
enable: config.prismjs.enable,
light : `${hexo.plugin_dir}prismjs/themes/${theme.codeblock.prism.light}.css`,
dark : `${hexo.plugin_dir}prismjs/themes/${theme.codeblock.prism.dark}.css`,
number: `${hexo.plugin_dir}prismjs/plugins/line-numbers/prism-line-numbers.css`
light : prismTheme(theme.codeblock.prism.light),
dark : prismTheme(theme.codeblock.prism.dark),
number: `${resolve('prismjs')}/plugins/line-numbers/prism-line-numbers.css`
};
};

2 comments on commit 2e4e1bc

@pplmx
Copy link

@pplmx pplmx commented on 2e4e1bc Jul 4, 2020

Choose a reason for hiding this comment

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

@stevenjoezhang
Copy link
Member Author

Choose a reason for hiding this comment

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

The webpage is under construction. It will be online later this month.
截屏2020-07-04 下午8 45 59

Please sign in to comment.