feat: heading tag, new normalizers

This commit is contained in:
Artemy Egorov 2024-07-25 18:39:46 +03:00
parent 599639dee6
commit 8530f84830
10 changed files with 134 additions and 46 deletions

View file

@ -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 {

View file

@ -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]);
};
}

View file

@ -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);
}
}

View 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
);
}
}

View 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));

View file

@ -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 };

View file

@ -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}>`;
}