mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
添加站内消息,修复讨论节点丢失的问题
This commit is contained in:
1 parent
33e277f28f
commit
627e64a901
44 files changed
+582
-540
No files matched your search
+11
-1
@@ -21,13 +21,23 @@ String.prototype.format = function formatStr(...args) {
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
String.prototype.rawformat = function rawFormat(object) {
|
||||
const res = this.split('{@}');
|
||||
return [res[0], object, res[1]];
|
||||
};
|
||||
|
||||
String.prototype.translate = function translate(language = 'zh_CN') {
|
||||
if (locales[language]) return locales[language][this] || this;
|
||||
return this;
|
||||
};
|
||||
|
||||
module.exports = locales;
|
||||
function load(data) {
|
||||
for (const i in data) {
|
||||
if (!locales[i]) locales[i] = data[i];
|
||||
else locales[i] = Object.assign(locales[i], data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
global.Hydro.locales = locales;
|
||||
global.Hydro.lib.i18n = module.exports = load;
|
||||
@@ -1,49 +0,0 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
let installed;
|
||||
function root(name) {
|
||||
return path.resolve(process.cwd(), name);
|
||||
}
|
||||
function exist(name) {
|
||||
try {
|
||||
fs.statSync(root(name));
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const superRequire = (name) => {
|
||||
let m;
|
||||
try {
|
||||
m = require(root(name)); // eslint-disable-line import/no-dynamic-require
|
||||
} catch (e) {
|
||||
m = __non_webpack_require__(root(name)); // eslint-disable-line no-undef
|
||||
}
|
||||
return m;
|
||||
};
|
||||
|
||||
async function prepare() {
|
||||
installed = fs.readdirSync(root('.build/module'));
|
||||
}
|
||||
|
||||
async function handler() {
|
||||
for (const i of installed) {
|
||||
if (exist(`.build/module/${i}/handler.js`)) {
|
||||
superRequire(`.build/module/${i}/handler.js`);
|
||||
console.log(`Handler init: ${i}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function model() {
|
||||
for (const i of installed) {
|
||||
if (exist(`.build/module/${i}/model.js`)) {
|
||||
superRequire(`.build/module/${i}/model.js`);
|
||||
console.log(`Model init: ${i}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
global.Hydro['lib.loader'] = module.exports = { prepare, handler, model };
|
||||
@@ -1,15 +1,19 @@
|
||||
const hljs = require('highlight.js');
|
||||
const MarkdownIt = require('markdown-it');
|
||||
const katex = require('markdown-it-katex');
|
||||
const Prism = require('prismjs');
|
||||
const loadLanguages = require('prismjs/components/');
|
||||
|
||||
// FIXME doesnt work in webpack
|
||||
loadLanguages(['js', 'javascript', 'go', 'c', 'pascal', 'hs', 'java', 'cs', 'cpp', 'py', 'ruby']);
|
||||
|
||||
class Markdown extends MarkdownIt {
|
||||
constructor() {
|
||||
super({
|
||||
linkify: true,
|
||||
highlight(str, lang) {
|
||||
if (lang && hljs.getLanguage(lang)) {
|
||||
if (lang && Prism.languages[lang]) {
|
||||
try {
|
||||
return hljs.highlight(lang, str).value;
|
||||
return Prism.highlight(str, Prism.languages[lang], lang);
|
||||
} catch (__) { } // eslint-disable-line no-empty
|
||||
}
|
||||
return '';
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
const md5 = require('./md5');
|
||||
|
||||
exports.gravatar_url = function gravatarUrl(email, size = 32) {
|
||||
exports.gravatar = function gravatar(email, size = 32) {
|
||||
return `//gravatar.loli.net/avatar/${md5((email || '').toString().trim().toLowerCase())}?d=mm&s=${size}`;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
const permission = require('../permission');
|
||||
|
||||
global.Hydro.ui.nav = [];
|
||||
|
||||
const Item = (path, name, prefix, perm) => {
|
||||
global.Hydro.ui.nav.push({
|
||||
path, name, prefix, perm,
|
||||
});
|
||||
};
|
||||
|
||||
Item('/', 'domain_main', 'domain_main');
|
||||
Item('/p', 'problem_main', 'problem', permission.PERM_VIEW_PROBLEM);
|
||||
Item('/t', 'training_main', 'training', permission.PERM_VIEW_TRAINING);
|
||||
Item('/discuss', 'discussion_main', 'discussion', permission.PERM_VIEW_DISCUSSION);
|
||||
Item('/c', 'contest_main', 'contest', permission.PERM_VIEW_CONTEST);
|
||||
Item('/manage', 'domain_manage', 'domain_manage', permission.PERM_MANAGE);
|
||||
|
||||
global.Hydro.lib.nav = module.exports = Item;
|
||||
+32
-18
@@ -1,35 +1,49 @@
|
||||
const path = require('path');
|
||||
const nunjucks = require('nunjucks');
|
||||
const markdown = require('./markdown');
|
||||
const options = require('../options');
|
||||
const perm = require('../permission');
|
||||
const builtin = require('../model/builtin');
|
||||
const model = require('../model');
|
||||
const misc = require('./misc');
|
||||
|
||||
function Loader() { }
|
||||
Loader.prototype.getSource = function getSource(name) {
|
||||
if (!global.Hydro.template[name]) throw new Error();
|
||||
return {
|
||||
src: global.Hydro.template[name],
|
||||
path: name,
|
||||
};
|
||||
};
|
||||
|
||||
class Nunjucks extends nunjucks.Environment {
|
||||
constructor() {
|
||||
super(
|
||||
new nunjucks.FileSystemLoader(path.resolve(options.template.path)),
|
||||
{ autoescape: true, trimBlocks: true },
|
||||
);
|
||||
super(new Loader(), { autoescape: true, trimBlocks: true });
|
||||
this.addFilter('json', (self) => JSON.stringify(self), false);
|
||||
this.addFilter('assign', (self, data) => Object.assign(self, data));
|
||||
this.addFilter('markdown', (self) => markdown.render(self));
|
||||
this.addFilter('base64_encode', (s) => Buffer.from(s).toString('base64'));
|
||||
this.addFilter('gravatar_url', misc.gravatar_url);
|
||||
this.addFilter('format_size', misc.format_size);
|
||||
this.addGlobal('typeof', (o) => typeof o);
|
||||
this.addGlobal('console', console);
|
||||
this.addGlobal('static_url', (str) => `/${str}`);
|
||||
this.addGlobal('reverse_url', (str) => str);
|
||||
this.addGlobal('datetime_span', misc.datetime_span);
|
||||
this.addGlobal('paginate', misc.paginate);
|
||||
this.addGlobal('perm', perm);
|
||||
this.addGlobal('status', builtin.STATUS);
|
||||
this.addGlobal('model', model);
|
||||
}
|
||||
}
|
||||
const env = new Nunjucks();
|
||||
|
||||
module.exports = env;
|
||||
module.exports = {
|
||||
render(name, state) {
|
||||
Object.assign(state, {
|
||||
typeof: (o) => typeof o,
|
||||
static_url: (str) => `/${str}`,
|
||||
datetime_span: misc.datetime_span,
|
||||
paginate: misc.paginate,
|
||||
perm,
|
||||
status: builtin.status,
|
||||
size: misc.size,
|
||||
gravatar: misc.gravatar,
|
||||
model,
|
||||
Context: global.Hydro.ui,
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
env.render(name, state, (err, res) => {
|
||||
if (err) reject(err);
|
||||
else resolve(res);
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user