Skip to content

Instantly share code, notes, and snippets.

View evan-brass's full-sized avatar

Evan Brass evan-brass

View GitHub Profile
@evan-brass
evan-brass / interface.mjs
Created November 16, 2022 19:05
Interface that will be provided by the network client:
export const peer_id = ""; // identifies this browser in the network
// Try_send and messages is how you would send sdp/ice candidates to signal your RTCPeerConnections.
export function try_send(peer_id, message) {} // Route a message through the network toward the peer. Unreliable since the peer could be offline or peers could fail in between.
const messages = new EventTarget(); // Receive messages from other peers.
// Would be used to join chatrooms:
export async function subscribe(topic_id) { [/* List of peers that have also subscribed to that topic */] }
export async function unsubscribe(topic) {}
// Parse an open tag
const success = pull(/^<([a-zA-Z][a-zA-Z0-9\-]*)/, tag => {
const new_tag = { tag, attributes: {}, children: [] };
cursor.children.push(new_tag);
parse_attributes(new_tag);
if (!VOID_TAGS.includes(tag.toLowerCase())) {
parse_content(new_tag);
}
})
const VOID_TAGS = [
// List from: https://riptutorial.com/html/example/4736/void-elements
'area',
'base',
'br',
'hr',
'img',
'input',
'link',
'meta',
// Parse a comment node:
|| pull(/^<!--((?:[^-]|-(?!->))*)-->/, comment => {
cursor.children.push({
comment
})
})
function parse_attributes(cursor) {
while(pull(/^\s+([a-zA-Z][a-zA-Z0-9\-]+)="([^"]*)"/, (
name,
value
) => {
cursor.attributes[name] = value;
})) {}
if (!pull(/^\s*>/)) {
throw new Error("Malformed open tag");
}
pull(/^<([a-zA-Z][a-zA-Z0-9\-]*)/, tag => {
const new_tag = { tag, attributes: {}, children: [] };
cursor.children.push(new_tag);
parse_attributes(new_tag);
parse_content(new_tag);
})
[{ tag: 'p', children: [
{ text: "\n\t" },
{ tag: 'b', children: [
{ text: "bold content" }
]},
{ text: "\n" }
]}]
<p>
<b>bold content</b>
</p>
// Parse an open tag
const success = pull(/^<([a-zA-Z][a-zA-Z0-9\-]*)>/, tag => {
const new_tag = { tag, attributes: {}, children: [] };
cursor.children.push(new_tag);
parse_content(new_tag);
}) ||
// Parse close tag
pull(/^<\/([a-zA-Z][a-zA-Z0-9\-]*)>/, tag => {
if (cursor.tag.toLowerCase() !== tag.toLowerCase()) {
throw new Error("Unmatched close tag");
function parse_content(cursor) {
let run = true;
while (run && input.length > 0) {
// Parse an open tag
const success = pull(/^<([a-zA-Z][a-zA-Z0-9\-]*)>/, tag => {
const new_tag = { tag, attributes: {}, children: [] };
cursor.children.push(new_tag);
parse_content(new_tag);
}) ||
// Parse close tag