Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tabbed Code Blocks #412

Open
jdgamble555 opened this issue Sep 20, 2022 · 2 comments
Open

Tabbed Code Blocks #412

jdgamble555 opened this issue Sep 20, 2022 · 2 comments

Comments

@jdgamble555
Copy link

It would be cool to be able to have tabbed code blocks where you can select different languages.

https://rdmd.readme.io/docs/code-blocks

That way you could do two code blocks in a row, and it tabs them.

https://firebase.google.com/docs/firestore/query-data/order-limit-data#web-version-9

Thank!

J

@fynnfluegge
Copy link

fynnfluegge commented Mar 30, 2023

Hi @jdgamble555, I guess this is out of scope of this project and maybe a suggestion for a plugin in prism. But I use a very straight forward workaround for this as you can see here.

The basic idea is to create a hook function and pass it as a complete hook to Prism with

Prism.hooks.add('complete', hook);

My function to create a tab pane is

// check if element is inside a tabbed element
if (env.element.parentElement.parentNode.classList.contains("tabbed")) {
    const elementId = Math.random().toString(36);
    let firstTab = false;
    // check if tab header already exists
    if (env.element.parentElement.parentNode.previousSibling == undefined || !env.element.parentElement.parentNode.previousSibling.classList?.contains("tab")) {
        firstTab = true;
        // create tab header
        var header = document.createElement('div');    
        header.classList.add("tab");
        env.element.parentElement.parentNode.parentNode.insertBefore(header, env.element.parentElement.parentNode);
    }

    // create tab button
    var tabButton = document.createElement('button');
    tabButton.innerHTML = env.language;
    tabButton.classList.add("tablinks", env.language);

    // set button background color from prism theme
    var theme = getComputedStyle(env.element.parentElement)
    tabButton.style.backgroundColor = theme.backgroundColor;
    tabButton.style.color = theme.color;

    // add onclick event to tab button
    tabButton.onclick = function(event) { openTab(event, elementId); };

    // add tab button to tab header
    env.element.parentElement.parentNode.previousSibling.appendChild(tabButton);

    // if element is inside a tabbed element, add tabcontent class and id
    wrapper.classList.add("tabcontent");
    wrapper.setAttribute("id", elementId);
    // if element is the first tab, show it
    if (firstTab) {
        wrapper.style.display = "block";
    } else {
        tabButton.classList.add("inactive");
    }
    // set border top radius of element to 0
    env.element.parentElement.style.borderTopLeftRadius = "0";
    env.element.parentElement.style.borderTopRightRadius = "0";
    env.element.parentElement.style.marginTop = "0";
}

Note that I use a wrapper tag around the prism code, hence you need to call this

wrapper.classList.add("tabcontent");
wrapper.setAttribute("id", elementId);

on the pre tag element generated by Prism.

How to use it in your markdown code:

<div class"tabbed">
                     <-- this empty line is important
code-block 1
code block 2
</div>

Full source code of my workaround it you can find here prism-custom.js. Hope that it helps 🙌

@jfcere
Copy link
Owner

jfcere commented Nov 13, 2023

Now that ngx-markdown supports marked extensions, that could probably be done with a custom implementation... I'm surprised it has not been implemented yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants