integrate jsonschema2markdown; add support for inline markdown rendering from templates
This commit is contained in:
@@ -4,35 +4,57 @@ import { render as mustache_render } from 'mustache';
|
||||
import { promises as fs } from 'fs';
|
||||
import { resolve as resolve_path } from 'path';
|
||||
import { glob } from 'glob';
|
||||
import { load_from_dir } from './fs';
|
||||
import { ColorTheme } from '@doc-utils/color-themes';
|
||||
import { ThemeGroups } from './build';
|
||||
|
||||
export interface Context {
|
||||
env?: Record<string, string>;
|
||||
page?: {
|
||||
title?: string;
|
||||
layout?: string;
|
||||
[key: string]: string | number | boolean;
|
||||
};
|
||||
page?: FrontMatter;
|
||||
icons: Record<string, string>;
|
||||
themes: ColorTheme[];
|
||||
theme_groups: ThemeGroups;
|
||||
build_time: {
|
||||
iso: string;
|
||||
rfc2822: string;
|
||||
};
|
||||
markdown: {
|
||||
render_inline(): MustacheRenderer;
|
||||
}
|
||||
}
|
||||
|
||||
export function render_template(template: string, context: Context, layout?: string, partials?: Record<string, string>) {
|
||||
export interface FrontMatter {
|
||||
title?: string;
|
||||
layout?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export function render_template(template: string, context: Context, layout?: string, partials: Record<string, string> = { }, tags?: [ string, string ]) {
|
||||
partials['.content'] = template;
|
||||
return mustache_render(layout || template, context, partials);
|
||||
return mustache_render(layout || template, context, partials, tags);
|
||||
}
|
||||
|
||||
export async function load_extras() {
|
||||
const extras: Record<string, string> = Object.create(null);
|
||||
const extras_dir = resolve_path(__dirname, '../extras');
|
||||
const extras_files = [
|
||||
'components/color-scheme-toggle-button.js',
|
||||
'components/outline-button.js',
|
||||
'components/outline-inline.js',
|
||||
'prism.css',
|
||||
];
|
||||
|
||||
const promises = extras_files.map((file) => load_from_dir(extras_dir, file));
|
||||
|
||||
for (let i = 0; i < extras_files.length; i++) {
|
||||
extras[`.extras/${extras_files[i]}`] = await promises[i];
|
||||
}
|
||||
|
||||
return extras;
|
||||
}
|
||||
|
||||
export async function load_layout(conf: Config, file: string) {
|
||||
const path = conf.templates?.layouts;
|
||||
|
||||
if (! path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rel_path = resolve_path('/', file);
|
||||
const abs_path = resolve_path(path, '.' + rel_path);
|
||||
return await fs.readFile(abs_path, 'utf8');
|
||||
return load_from_dir(conf.templates?.layouts, file);
|
||||
}
|
||||
|
||||
export async function load_partials(conf: Config) {
|
||||
@@ -55,3 +77,7 @@ export async function load_partials(conf: Config) {
|
||||
|
||||
return partials;
|
||||
}
|
||||
|
||||
export interface MustacheRenderer {
|
||||
(this: void, text: string, render: (text: string) => string): string;
|
||||
}
|
||||
|
Reference in New Issue
Block a user