Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

1.3 KB
Raw Blame
---
/* eslint-disable unicorn/better-dom-traversing */

import assert from "node:assert/strict";

import {fromHtml} from "hast-util-from-html";
import {toHtml} from "hast-util-to-html";

const tree = fromHtml(await Astro.slots.render("default"), {fragment: true});

const tree_with_removed_newlines = {
    type: "root",
    children: tree.children.filter((child) => {
        if (child.type === "text" && child.value === "\n") {
            return false;
        }
        return true;
    }),
};
const first_element = tree_with_removed_newlines.children[0];
assert.ok(first_element?.type === "element" && ["ol", "ul"].includes(first_element.tagName));
const flattened = {
    ...first_element,
    children: tree_with_removed_newlines.children.flatMap((other) => {
        if (other.type === "comment") {
            return [];
        }
        assert.ok(other.type === "element");
        // Flatten only in case of matching tagName, for the rest, we
        // return the elements without flattening since asides, code
        // blocks and other elements can be part of a single list item
        // and we do not want to flatten them.
        if (other.tagName === first_element.tagName) {
            return other.children;
        }
        return [other];
    }),
};
---

<Fragment set:html={toHtml(flattened)} />