mirror of
https://github.com/TxtDot/dalet.git
synced 2025-02-28 15:21:24 +03:00
feat: heading tag, new normalizers
This commit is contained in:
parent
599639dee6
commit
8530f84830
10 changed files with 134 additions and 46 deletions
|
@ -1,5 +1,13 @@
|
|||
import { encode, decode } from "@msgpack/msgpack";
|
||||
import { ParseError, RawTag, Tag, RawTagAsArray, RawBody, Body } from "./types";
|
||||
import {
|
||||
ParseError,
|
||||
RawTag,
|
||||
Tag,
|
||||
RawTagAsArray,
|
||||
RawBody,
|
||||
Body,
|
||||
RootRaw,
|
||||
} from "./types";
|
||||
import El from "./tags/el";
|
||||
import { TagNormalizers } from "./normalizers";
|
||||
|
||||
|
@ -39,7 +47,7 @@ export function parseBody(body: RawBody): Body {
|
|||
|
||||
if (Array.isArray(body)) {
|
||||
if (Array.isArray(body[0])) {
|
||||
return body.map(parseTag);
|
||||
return body.map((t) => parseTag(t as RawTag));
|
||||
}
|
||||
|
||||
if (typeof body[0] === "number") {
|
||||
|
@ -50,14 +58,14 @@ export function parseBody(body: RawBody): Body {
|
|||
throw new ParseError("Invalid tag body");
|
||||
}
|
||||
|
||||
export function parse(root_data: Uint8Array): Tag[] {
|
||||
export function parse(root_data: Uint8Array): Root {
|
||||
let root = decode(root_data);
|
||||
|
||||
if (!Array.isArray(root)) {
|
||||
throw new ParseError("Daletl root must be array");
|
||||
}
|
||||
|
||||
return root.map(parseTag);
|
||||
return new Root(root.map(parseTag));
|
||||
}
|
||||
|
||||
export class Root {
|
||||
|
@ -66,8 +74,12 @@ export class Root {
|
|||
this.root = root;
|
||||
}
|
||||
|
||||
get raw(): RootRaw {
|
||||
return this.root.map((t) => t.raw);
|
||||
}
|
||||
|
||||
encode(): Uint8Array {
|
||||
return encode(this.root.map((t) => t.encode()));
|
||||
return encode(this.raw);
|
||||
}
|
||||
|
||||
toHtml(classes?: boolean): string {
|
||||
|
|
|
@ -1,17 +1,30 @@
|
|||
import { parseBody } from "./main";
|
||||
import El from "./tags/el";
|
||||
import { ParseError, RawTagAsArray } from "./types";
|
||||
import Heading from "./tags/heading";
|
||||
import { RawTagAsArray } from "./types";
|
||||
import { z } from "zod";
|
||||
|
||||
export function ElNormalizer(tag: RawTagAsArray): El {
|
||||
let body = parseBody(tag[1]);
|
||||
|
||||
if (body == null) {
|
||||
throw new ParseError("Invalid tag body, must be not null");
|
||||
}
|
||||
|
||||
return new El(body);
|
||||
}
|
||||
|
||||
const TagNormalizers = [ElNormalizer];
|
||||
const TagNormalizers = [
|
||||
n(
|
||||
z.custom((b) => b !== null),
|
||||
z.any(),
|
||||
El
|
||||
),
|
||||
n(z.string(), z.number().int().min(1).max(6), Heading),
|
||||
];
|
||||
|
||||
export { TagNormalizers };
|
||||
|
||||
function n(body: z.ZodTypeAny, argument: z.ZodTypeAny, T: any) {
|
||||
return (tag: RawTagAsArray) => {
|
||||
let parsedBody = parseBody(tag[1]);
|
||||
|
||||
z.tuple([z.number().int(), body, argument]).parse([
|
||||
tag[0],
|
||||
parsedBody,
|
||||
tag[2],
|
||||
]);
|
||||
|
||||
return new T(parsedBody, tag[2]);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { bodyToRaw } from "../../utils";
|
||||
import { bodyToRaw, chtml } from "../../utils";
|
||||
import { RawTag, Tag, Body } from "../types";
|
||||
|
||||
export default class El extends Tag {
|
||||
|
@ -6,11 +6,11 @@ export default class El extends Tag {
|
|||
super(0, body, null);
|
||||
}
|
||||
|
||||
public get raw(): RawTag {
|
||||
return bodyToRaw(this.body);
|
||||
get raw(): RawTag {
|
||||
return bodyToRaw(this.body)!;
|
||||
}
|
||||
|
||||
public toHtml(classes?: boolean): string {
|
||||
return "";
|
||||
toHtml(classes?: boolean): string {
|
||||
return chtml("section", "el", classes, this.body);
|
||||
}
|
||||
}
|
||||
|
|
17
libs/typescript/src/daletl/tags/heading.ts
Normal file
17
libs/typescript/src/daletl/tags/heading.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { chtml } from "../../utils";
|
||||
import { Tag } from "../types";
|
||||
|
||||
export default class Heading extends Tag {
|
||||
constructor(body: string, argument: number) {
|
||||
super(1, body, argument);
|
||||
}
|
||||
|
||||
toHtml(classes?: boolean): string {
|
||||
return chtml(
|
||||
`h${this.argument}`,
|
||||
`h hl${this.argument}`,
|
||||
classes,
|
||||
this.body
|
||||
);
|
||||
}
|
||||
}
|
12
libs/typescript/src/index.ts
Normal file
12
libs/typescript/src/index.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { parse, Root, El, Heading } from "./lib";
|
||||
|
||||
let data = new Root([
|
||||
new El("I am Element"),
|
||||
new Heading("I am heading", 1),
|
||||
]).encode();
|
||||
|
||||
let root = parse(data);
|
||||
|
||||
console.log(root.raw, "\n");
|
||||
console.log(root.toHtml());
|
||||
console.log(root.toHtml(false));
|
|
@ -1,2 +1,5 @@
|
|||
export { parse } from "./daletl/main";
|
||||
export * as converters from "./converters/main";
|
||||
import El from "./daletl/tags/el";
|
||||
import Heading from "./daletl/tags/heading";
|
||||
|
||||
export { parse, Root } from "./daletl/main";
|
||||
export { El, Heading };
|
||||
|
|
|
@ -14,20 +14,45 @@ export function bodyToRaw(body: Body): RawBody {
|
|||
return null;
|
||||
}
|
||||
|
||||
export function bodyToHtml(body: Body): string {
|
||||
if (typeof body === "string") {
|
||||
return body;
|
||||
}
|
||||
|
||||
if (Array.isArray(body)) {
|
||||
return body.map((t) => t.toHtml()).join("");
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
export function getRaw(t: Tag): RawTag {
|
||||
return t.raw;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
[key: string]: string;
|
||||
[key: string]: string | undefined;
|
||||
}
|
||||
|
||||
function hy(tag: string, body?: string, props?: Props) {
|
||||
return `<${tag} ${
|
||||
props
|
||||
? Object.entries(props)
|
||||
.map(([key, value]) => `${key}="${value}"`)
|
||||
.join(" ")
|
||||
: ""
|
||||
}>${body}</${tag}>`;
|
||||
export function chtml(
|
||||
tag: string,
|
||||
classNames: string,
|
||||
classes: boolean = true,
|
||||
body?: Body,
|
||||
props?: Props
|
||||
) {
|
||||
let classProp = classes ? { class: classNames } : {};
|
||||
return html(tag, body, { ...props, ...classProp });
|
||||
}
|
||||
|
||||
function html(tag: string, body?: Body, props?: Props) {
|
||||
let pr = Object.entries(props || {})
|
||||
.map(([key, value]) => `${key}="${value}"`)
|
||||
.join(" ");
|
||||
|
||||
if (!body) {
|
||||
return `<${tag}${pr ? " " + pr : ""}/>`;
|
||||
}
|
||||
|
||||
return `<${tag}${pr ? " " + pr : ""}>${bodyToHtml(body)}</${tag}>`;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue