Page MenuHomeSealhub

block.ts
No OneTemporary

block.ts

import { predicates, ShapeToType } from "@sealcode/ts-predicates";
import { ConstPredicate } from "@sealcode/ts-predicates/@types/predicates/const";
import { LazyPredicate } from "@sealcode/ts-predicates/@types/predicates/lazy";
import { HeadingPrimitive, HeadingShape } from "./heading";
import { ImageShape } from "./image";
import { parse_primitives } from "./parse_primitives";
import { Primitive, PrimitiveJSON } from "./primitive";
import { TextShape } from "./text";
export const BlockShape: {
readonly type: ConstPredicate<"block">;
readonly content: LazyPredicate<PrimitiveJSON[]>;
} = <const>{
type: predicates.const(<const>"block"),
content: predicates.lazy(() =>
predicates.array<PrimitiveJSON>(
predicates.or(
predicates.shape(BlockShape),
predicates.shape(HeadingShape),
predicates.shape(ImageShape),
predicates.shape(TextShape)
)
)
),
};
export type BlockJSON = ShapeToType<typeof BlockShape>;
export class BlockPrimitive extends Primitive {
content: Primitive[];
id: string | null = null;
constructor(json: Record<string, unknown>) {
super();
this.content = parse_primitives(
json["content"] as Record<string, unknown>[]
);
this.id = json["id"] as string | null;
}
to_html(): string {
let html = `<div>`;
for (const comp of this.content) {
html += comp.to_html();
}
return `${html}</div>`;
}
extract_headings(): HeadingPrimitive[] {
const headings: HeadingPrimitive[] = [];
for (const primitive of this.content) {
headings.push(...primitive.extract_headings());
}
return headings;
}
extract_images(): string[] {
const ret: string[] = [];
for (const comp of this.content) {
ret.push(...comp.extract_images());
}
return ret;
}
}

File Metadata

Mime Type
text/x-java
Expires
Wed, May 7, 19:38 (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
624746
Default Alt Text
block.ts (1 KB)

Event Timeline