mirror of
https://github.com/next-theme/hexo-theme-next.git
synced 2026-01-18 18:33:42 +00:00
Create utils.js
This commit is contained in:
parent
3ede1b3ed0
commit
68654c2985
@ -1,39 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
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 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
|
||||
};
|
||||
}
|
||||
const { resolve, highlightTheme } = require('./utils');
|
||||
|
||||
function prismTheme(name) {
|
||||
let file = resolve('prismjs', `themes/${name}.css`);
|
||||
|
||||
@ -1,19 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
views: [
|
||||
'head',
|
||||
'header',
|
||||
'sidebar',
|
||||
'postMeta',
|
||||
'postBodyEnd',
|
||||
'footer',
|
||||
'bodyEnd',
|
||||
'comment'
|
||||
],
|
||||
styles: [
|
||||
'variable',
|
||||
'mixin',
|
||||
'style'
|
||||
]
|
||||
};
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const points = require('./injects-point');
|
||||
const { points } = require('./utils');
|
||||
const defaultExtname = '.njk';
|
||||
|
||||
// Defining stylus types
|
||||
|
||||
60
scripts/events/lib/utils.js
Normal file
60
scripts/events/lib/utils.js
Normal file
@ -0,0 +1,60 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
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 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
|
||||
};
|
||||
}
|
||||
|
||||
const points = {
|
||||
views: [
|
||||
'head',
|
||||
'header',
|
||||
'sidebar',
|
||||
'postMeta',
|
||||
'postBodyEnd',
|
||||
'footer',
|
||||
'bodyEnd',
|
||||
'comment'
|
||||
],
|
||||
styles: [
|
||||
'variable',
|
||||
'mixin',
|
||||
'style'
|
||||
]
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
resolve,
|
||||
highlightTheme,
|
||||
points
|
||||
};
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const points = require('../events/lib/injects-point');
|
||||
const { points } = require('../events/lib/utils');
|
||||
|
||||
hexo.extend.filter.register('theme_inject', injects => {
|
||||
const filePath = hexo.theme.config.custom_file_path;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user