Skip to content

Instantly share code, notes, and snippets.

@coder0107git
Created October 24, 2024 01:27

Revisions

  1. coder0107git created this gist Oct 24, 2024.
    52 changes: 52 additions & 0 deletions index.astro
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    ---
    /**
    * @param moduleId Path to component
    */
    function function2AstroComponent<Args extends any>(
    callback: (...arg: Args[]) => any
    ): (...arg: Args[]) => any & {
    isAstroComponentFactory: true;
    propagation: "none";
    name: string;
    moduleId: string;
    } {
    const fn = (...args: Args[]) => {
    return callback(...args);
    };
    Object.defineProperty(fn, "name", { value: callback.name, writable: false });
    fn.isAstroComponentFactory = true;
    fn.moduleId = callback.name + ".astro";
    fn.propagation = "none";
    return fn;
    }
    const TR = function2AstroComponent((_astroMetadata, props: any, childRenderer: any, ...extra) => {
    const children = childRenderer.default();
    // Children pieces directly to html string
    const res = String.raw({ raw: children.htmlParts }, ...children.expressions);
    // Log everything to better break down what information we can use.
    console.log(props, children, res, ...extra);
    // Astro can handle the child object directly so we just Astro it.
    return children;
    });
    ---

    <TR x="y">
    <br>
    my children
    <br>
    {true ? "hi" : "hello"}
    <br>
    </TR>
    <!--
    !
    ! Renders:
    ! ```html
    ! <br>
    ! my children
    ! <br> hi<br>
    ! ```
    !
    -->