OK, it's plugged in, now how to test it

This commit is contained in:
gnuxie
2023-01-14 18:33:06 +00:00
parent de39bf5274
commit 4b7e6637fa
5 changed files with 48 additions and 14 deletions
@@ -35,7 +35,8 @@ export enum NodeTag {
Paragraph = 'p',
HeadingOne = 'h1',
UnorderedList = 'ul',
ListItem = 'li'
ListItem = 'li',
LineBreak = 'br',
}
/**
@@ -4,7 +4,7 @@
*/
import { FringeLeafRenderFunction, FringeType, LeafNode, NodeTag, SimpleFringeRenderer } from "./DeadDocument";
import { staticString, TransactionalOutputContext } from "./DeadDocumentMarkdown";
import { blank, staticString, TransactionalOutputContext } from "./DeadDocumentMarkdown";
export const HTML_RENDERER = new SimpleFringeRenderer<TransactionalOutputContext>();
@@ -38,4 +38,7 @@ HTML_RENDERER.registerRenderer<FringeLeafRenderFunction<TransactionalOutputConte
).registerInnerNode(NodeTag.ListItem,
staticString('<li>'),
staticString('</li>')
).registerInnerNode(NodeTag.LineBreak,
blank,
staticString('<br/>'),
);
@@ -3,7 +3,7 @@
* All rights reserved.
*/
import { DocumentNode, FringeInnerRenderFunction, FringeLeafRenderFunction, FringeType, LeafNode, NodeTag, SimpleFringeRenderer, TagDynamicEnvironment, TagDynamicEnvironmentEntry } from "./DeadDocument";
import { DocumentNode, FringeInnerRenderFunction, FringeLeafRenderFunction, FringeType, LeafNode, NodeTag, SimpleFringeRenderer, TagDynamicEnvironment } from "./DeadDocument";
/**
* Ideally this would call a callback when a page is ready
@@ -47,8 +47,10 @@ export class PagedDuplexStream {
* Creates a new page from the previously committed text
* @returns A page with all committed text.
*/
public forceNewPage(): void {
this.pages.push('');
public ensureNewPage(): void {
if (this.currentPage.length !== 0) {
this.pages.push('');
}
}
/**
@@ -65,7 +67,7 @@ export class PagedDuplexStream {
if (this.currentPage.length === 0 && (this.buffer.length > this.sizeLimit)) {
throw new TypeError('Commit is too large, could not write a page for this commit');
}
this.forceNewPage();
this.ensureNewPage();
this.appendToCurrentPage(this.buffer);
this.lastCommittedNode = node;
} else {
@@ -158,4 +160,7 @@ MARKDOWN_RENDERER.registerRenderer<FringeLeafRenderFunction<TransactionalOutputC
context.output.writeString(' * ');
},
staticString('\n')
).registerInnerNode(NodeTag.LineBreak,
blank,
staticString('\n')
);
@@ -3,6 +3,7 @@
* All rights reserved.
*/
import { MatrixSendClient } from "../../MatrixEmitter";
import { AbstractNode, DocumentNode, FringeWalker } from "./DeadDocument";
import { HTML_RENDERER } from "./DeadDocumentHtml";
import { MARKDOWN_RENDERER, PagedDuplexStream } from "./DeadDocumentMarkdown";
@@ -39,14 +40,12 @@ export async function renderMatrix(node: DocumentNode, cb: SendMatrixEventCB) {
let currentHtmlNode = htmlWalker.increment();
checkEqual(currentHtmlNode, currentMarkdownNode);
while (currentHtmlNode !== undefined) {
const outputsWithNewPage = outputs.filter(o => o.peekPage());
if (outputsWithNewPage.length !== 0) {
if (outputs.some(o => o.peekPage())) {
// Ensure each stream has the same nodes in the new page.
for (const output of outputs) {
if (!outputsWithNewPage.includes(output)) {
output.forceNewPage();
}
}
// I'm really worried that somehow a stream can have a page waiting
// while also having buffered output? so when it is ensured, that output is appended?
// that means there's an implementation issue in the walker's though.
outputs.forEach(o => o.ensureNewPage());
// Send the new pages as an event.
await cb(markdownOutput.readPage()!, htmlOutput.readPage()!);
}
@@ -56,3 +55,15 @@ export async function renderMatrix(node: DocumentNode, cb: SendMatrixEventCB) {
checkEqual(currentHtmlNode, currentMarkdownNode);
}
}
export async function renderMatrixAndSend(node: DocumentNode, roomId: string, event: any, client: MatrixSendClient): Promise<void> {
// We desperatley need support for threads to make this work in a non-shit way.
await renderMatrix(node, async (text: string, html: string) => {
await client.sendMessage(roomId, {
msgtype: "m.notice",
body: text,
format: "org.matrix.custom.html",
formatted_body: html,
});
});
}
+15 -1
View File
@@ -5,7 +5,13 @@
import { DocumentNode, LeafNode, makeDocumentNode, makeLeafNode, NodeTag, TextNode } from "./DeadDocument";
declare global {
namespace JSX {
interface IntrinsicElements {
[elemName: string]: any;
}
}
}
export function JSXFactory(tag: NodeTag, properties: any, ...rawChildren: (DocumentNode|LeafNode|string)[]) {
const node = makeDocumentNode(tag);
@@ -20,3 +26,11 @@ export function JSXFactory(tag: NodeTag, properties: any, ...rawChildren: (Docum
rawChildren.forEach(ensureChild);
return node;
}
namespace JSXFactory {
export interface IntrinsicElements {
[elemName: string]: any;
}
}