@bookbox/generator-js
Version:
Bookbox generator for js code
106 lines (105 loc) • 3.48 kB
JavaScript
import { isTemplateParams, templateToList } from '@bookbox/core';
const getElement = name => props => (...children) => {
const list = isTemplateParams(children) ? templateToList(...children) : children;
return {
name,
props,
children: list,
};
};
const proxyBuilder = (getBuild, prepare) => {
const getProxy = (params) => new Proxy(getBuild(params), {
get(_, name) {
return (...children) => {
var _a;
const value = isTemplateParams(children) ? prepare(...children) : (_a = children[0]) !== null && _a !== void 0 ? _a : true;
return getProxy(Object.assign(Object.assign({}, params), { [name]: value }));
};
},
});
return getProxy({});
};
function getElementProxy() {
return function g(name, prepare = String.raw) {
const getElementWithProps = getElement(name);
const builder = proxyBuilder(params => getElementWithProps(params), prepare);
return builder;
};
}
export const bookStart = elem => {
const { name, props } = elem();
return {
__start: name,
props,
};
};
export const bookEnd = elem => {
const { name, props } = elem();
return {
__end: name,
props,
};
};
export const bookCreator = (text, ...elements) => {
return { schema: templateToList(text, ...elements) };
};
bookCreator.root = bookCreator;
const getRootProxy = getElementProxy();
const getFormatProxy = getElementProxy();
const getWebProxy = getElementProxy();
export const defaultBookApi = {
title: getRootProxy('title'),
authors: getRootProxy('authors'),
draft: getRootProxy('draft'),
em: getRootProxy('em'),
strong: getRootProxy('strong'),
format: {
sup: getFormatProxy('format.sup'),
sub: getFormatProxy('format.sub'),
pre: getFormatProxy('format.pre'),
i: getFormatProxy('format.i'),
b: getFormatProxy('format.b'),
u: getFormatProxy('format.u'),
s: getFormatProxy('format.s'),
small: getFormatProxy('format.small'),
},
format_sup: getRootProxy('format_sup'),
format_sub: getRootProxy('format_sub'),
format_pre: getRootProxy('format_pre'),
format_i: getRootProxy('format_i'),
format_b: getRootProxy('format_b'),
format_u: getRootProxy('format_u'),
format_s: getRootProxy('format_s'),
format_small: getRootProxy('format_small'),
web: {
video: getWebProxy('web.video'),
audio: getWebProxy('web.audio'),
message: getWebProxy('web.message'),
},
web_video: getRootProxy('web_video'),
web_audio: getRootProxy('web_audio'),
web_message: getRootProxy('web_message'),
header: getRootProxy('header'),
code: getRootProxy('code'),
label: getRootProxy('label'),
link: getRootProxy('link'),
tooltip: getRootProxy('tooltip'),
image: getRootProxy('image'),
external: getRootProxy('external'),
area: getRootProxy('area'),
list: getRootProxy('list'),
separator: getRootProxy('separator'),
item: getRootProxy('item'),
math: getRootProxy('math'),
video: getRootProxy('video'),
audio: getRootProxy('audio'),
use: getRootProxy('use'),
counter: getRootProxy('counter'),
resource: getRootProxy('resource'),
table: getRootProxy('table'),
row: getRootProxy('row'),
cell: getRootProxy('cell'),
start: bookStart,
end: bookEnd,
book: bookCreator,
};