From 2e4e1bc16fa3cd311e9e7f9e25cdf228c7819e0b Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 4 Jul 2020 18:58:32 +0800 Subject: [PATCH] Include additional themes for prism --- .github/support.yml | 2 +- _config.yml | 5 ++--- scripts/events/index.js | 4 ++-- scripts/events/lib/highlight.js | 24 ++++++++++++++++++------ 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/.github/support.yml b/.github/support.yml index b4cdb20..a4c9ea5 100644 --- a/.github/support.yml +++ b/.github/support.yml @@ -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 diff --git a/_config.yml b/_config.yml index defc830..79989a0 100644 --- a/_config.yml +++ b/_config.yml @@ -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 @@ -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 diff --git a/scripts/events/index.js b/scripts/events/index.js index 6b84a25..b76784d 100644 --- a/scripts/events/index.js +++ b/scripts/events/index.js @@ -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'); diff --git a/scripts/events/lib/highlight.js b/scripts/events/lib/highlight.js index 6245fa7..0bbfa1c 100644 --- a/scripts/events/lib/highlight.js +++ b/scripts/events/lib/highlight.js @@ -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 = ''; @@ -21,6 +27,12 @@ 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; @@ -28,13 +40,13 @@ module.exports = hexo => { 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` }; };