mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
template into ui
This commit is contained in:
1 parent
43ee86d34b
commit
8bb389eedd
13 files changed
+70
-206
No files matched your search
@@ -1,67 +0,0 @@
|
||||
import MarkdownIt from 'markdown-it';
|
||||
import Prism from 'prismjs';
|
||||
|
||||
// For math: $a\times b\eq 10$
|
||||
// Specific image size: 
|
||||
import Imsize from 'markdown-it-imsize';
|
||||
/* Footnote support.
|
||||
Here is a footnote reference,[^1] and another.[^longnote]
|
||||
|
||||
[^1]: Here is the footnote.
|
||||
|
||||
[^longnote]: Here's one with multiple blocks.
|
||||
|
||||
Subsequent paragraphs are indented to show that they
|
||||
belong to the previous footnote.
|
||||
*/
|
||||
import Footnote from 'markdown-it-footnote';
|
||||
// ==Highlight==
|
||||
import Mark from 'markdown-it-mark';
|
||||
import TOC from 'markdown-it-table-of-contents';
|
||||
import Anchor from 'markdown-it-anchor';
|
||||
|
||||
require('prismjs/components/index');
|
||||
|
||||
type Plugin = MarkdownIt.PluginSimple | MarkdownIt.PluginWithOptions | MarkdownIt.PluginWithParams;
|
||||
|
||||
class Markdown extends MarkdownIt {
|
||||
constructor() {
|
||||
super({
|
||||
linkify: true,
|
||||
highlight(str, lang) {
|
||||
if (lang && Prism.languages[lang]) {
|
||||
try {
|
||||
return Prism.highlight(str, Prism.languages[lang], lang);
|
||||
} catch (__) { } // eslint-disable-line no-empty
|
||||
}
|
||||
return '';
|
||||
},
|
||||
});
|
||||
this.linkify.tlds('.py', false);
|
||||
this.use(Imsize);
|
||||
this.use(Footnote);
|
||||
this.use(Mark);
|
||||
this.use(Anchor);
|
||||
this.use(TOC);
|
||||
}
|
||||
}
|
||||
|
||||
export const md = new Markdown();
|
||||
|
||||
export function plugin(func: Plugin, ...args: any[]) {
|
||||
md.use(func, ...args);
|
||||
}
|
||||
|
||||
export function render(text: string, html = false) {
|
||||
md.set({ html: !!html });
|
||||
return md.render(text);
|
||||
}
|
||||
|
||||
export function renderInline(text: string, html = false) {
|
||||
md.set({ html: !!html });
|
||||
return md.renderInline(text);
|
||||
}
|
||||
|
||||
global.Hydro.lib.markdown = {
|
||||
md, plugin, render, renderInline,
|
||||
};
|
||||
@@ -3,7 +3,7 @@ import { ValidationError } from '../error';
|
||||
|
||||
export default async function paginate(
|
||||
cursor: Cursor, page: number, pageSize: number,
|
||||
): Promise<[any[], number, number]> {
|
||||
): Promise<[any[]/* docs */, number/* numPages */, number/* count */]> {
|
||||
if (page <= 0) throw new ValidationError('page');
|
||||
const [count, pageDocs] = await Promise.all([
|
||||
cursor.count(),
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import serialize from 'serialize-javascript';
|
||||
import nunjucks from 'nunjucks';
|
||||
import html2text from 'html-to-text';
|
||||
import { argv } from 'yargs';
|
||||
import * as markdown from './markdown';
|
||||
import * as misc from './misc';
|
||||
|
||||
class Loader extends nunjucks.Loader {
|
||||
// eslint-disable-next-line class-methods-use-this
|
||||
getSource(name: string) {
|
||||
if (!argv.template) {
|
||||
if (!global.Hydro.ui.template[name]) throw new Error(`Cannot get template ${name}`);
|
||||
return {
|
||||
src: global.Hydro.ui.template[name],
|
||||
path: name,
|
||||
noCache: false,
|
||||
};
|
||||
}
|
||||
let fullpath = null;
|
||||
const p = path.resolve(process.cwd(), argv.template as string, name);
|
||||
if (fs.existsSync(p)) fullpath = p;
|
||||
if (!fullpath) throw new Error(`Cannot get template ${name}`);
|
||||
return {
|
||||
src: fs.readFileSync(fullpath, 'utf-8').toString(),
|
||||
path: fullpath,
|
||||
noCache: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class Nunjucks extends nunjucks.Environment {
|
||||
constructor() {
|
||||
super(new Loader(), { autoescape: true, trimBlocks: true });
|
||||
this.addFilter('json', (self) => JSON.stringify(self), false);
|
||||
this.addFilter('serialize', (self, ignoreFunction = true) => serialize(self, { ignoreFunction }));
|
||||
this.addFilter('assign', (self, data) => Object.assign(self, data));
|
||||
this.addFilter('markdown', (self, html = false) => markdown.render(self, html));
|
||||
this.addFilter('ansi', (self) => misc.ansiToHtml(self));
|
||||
this.addFilter('inline', (self) => html2text.fromString(self).replace(/\n/gmi, ' '));
|
||||
this.addFilter('base64_encode', (s) => Buffer.from(s).toString('base64'));
|
||||
this.addFilter('bitand', (self, val) => self & val);
|
||||
}
|
||||
}
|
||||
const env = new Nunjucks();
|
||||
|
||||
export async function render(name: string, state: any): Promise<string> {
|
||||
const cdnPrefix = await global.Hydro.model.system.get('server.cdn');
|
||||
return await new Promise((resolve, reject) => {
|
||||
env.render(name, {
|
||||
page_name: name.split('.')[0],
|
||||
...state,
|
||||
typeof: (o) => typeof o,
|
||||
static_url: (assetName: string) => {
|
||||
if (global.Hydro.ui.manifest[assetName]) {
|
||||
return `${cdnPrefix}${global.Hydro.ui.manifest[assetName]}`;
|
||||
}
|
||||
return `${cdnPrefix}${assetName}`;
|
||||
},
|
||||
datetimeSpan: misc.datetimeSpan,
|
||||
paginate: misc.paginate,
|
||||
perm: global.Hydro.model.builtin.PERM,
|
||||
PRIV: global.Hydro.model.builtin.PRIV,
|
||||
STATUS: global.Hydro.model.builtin.STATUS,
|
||||
size: misc.size,
|
||||
gravatar: misc.gravatar,
|
||||
model: global.Hydro.model,
|
||||
Context: global.Hydro.ui,
|
||||
isIE: (str: string) => str.includes('MSIE') || str.includes('rv:11.0'),
|
||||
}, (err, res) => {
|
||||
if (err) reject(err);
|
||||
else resolve(res);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
global.Hydro.lib.template = { render };
|
||||
Reference in new issue
Block a user