u/Venerous

Issue with closing a macro tag

Trying to create a custom macro that creates a tabbed menu - a sidebar with buttons to click to navigate to new panels, and a main content container that manages what each tab contains.

Macro.add('tabmenu', {
    tags: ['tab'],
    handler: function () {
        const tabs = this.payload.slice(1);

        const $wrapper = $('<div>').addClass('tab-wrap');
        const $sidebar = $('<div>').addClass('tab-nav');
        const $content = $('<div>').addClass('tab-content');

        tabs.forEach((tab, i) => {
            var isFirst = i === 0;

            let $button = $('<button>')
                .addClass(`tab-btn${isFirst ? ' active' : ''}`)
                .attr('data-target', `tab-panel-${i}`)
                .text(tab.args[0]);

            var $panel = $('<div>')
                .addClass(`tab-panel${isFirst ? ' active' : ''}`)
                .attr('id', `tab-panel-${i}`);

            // processes contents and SugarCube logic on the tab
            new Wikifier($panel[0], tab.contents);

            $sidebar.append($button);
            $content.append($panel);
        });

        $wrapper.append($sidebar, $content);

        $wrapper.on('click', '.tab-btn', function () {
            let $menu = $(this).closest('.tab-wrap');

            $menu.find('.tab-btn').removeClass('active');
            $menu.find('.tab-panel').removeClass('active');

            $(this).addClass('active');
            $menu.find('#' + $(this).data('target')).addClass('active');
        });

        $(this.output).append($wrapper);
    },
})

// And then in the Twine passage...

:: SomePassageHere [nobr]

<<tabmenu>>
    <<tab 'Tab 1'>>
        <p>Hello 1</p>

        <<if (1 + 1) == 2>>Hello<</if>>
        <<if (2 + 2) == 22>>Goodbye<</if>>
    <</tab>>
    <<tab 'Tab 2'>>
        <p>Hello 2</p>
    <</tab>>
    <<tab 'Tab 3'>>
        <p>Hello 3</p>
    <</tab>>
    <<tab 'Tab 4'>>
        <p>Hello 4</p>
    <</tab>>
<</tabmenu>

This works, however I'm getting a macro error: <</tab>> does not exist. I assume this is because macro tags cannot be containers. If I remove the closing <</tab>> it works just fine and the error goes away, but it looks weird in the Twine passage and I was hoping to avoid doing that. I've tried simply removing it programmatically before rendering the final result but that seems inelegant and I'm hoping for a better solution. May need to split it into a separate macro (<<tabmenu>> and <<tab>>) but not sure. Any advice is appreciated.

reddit.com
u/Venerous — 7 days ago