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

Added-support-for-table-parser #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

navjot11022001
Copy link

As per my observation table type support was not there. And in this PR , i have given the support for the table type.
Also i have tested this by modifying test.js file .

@KaizoKev
Copy link

KaizoKev commented Nov 1, 2022

Hello I made this code in Javascript, it is not very optimized I know, but it works well for tables

customParser(block) {
            var tableOpen = "<table class='table table-bordered'>"
            var trOpen = "<tr>"
            var trClose = "</tr>"
            var id = ""
            block.data.content.forEach((element, index) => {
                element.forEach(elem => {
                    if (id != index) {
                        trOpen += trClose
                    }
                    trOpen += `<td scope='row'> ${elem} </td>`
                    id = index
                });

            });
            var tableClose = "</table>"
            return tableOpen + trOpen +  tableClose;
        }

For the HTML class, i use Bootstrap, but you can define your own css
Don't forget to instantiate the class with the parameter, it must be called "table" for it to be recognized

var edjsParser = new edjsHTML({ table: this.customParser })

@navjot11022001
Copy link
Author

yes your code is fine @KevinCeyland for the same parsing i have raised the pr ,for giving the support of the
table

@paulocastellano
Copy link

Any ETA for merge it?

@joligoms
Copy link

joligoms commented Aug 7, 2023

I created a solution that works with headings too:

type TableBlock = {
    type: 'table';
    data: {
        withHeadings: boolean;
        content: string[][];
    };
}

function tableParser(block: TableBlock): string {
    const tableData = block.data.content;
    const withHeadings = block.data.withHeadings;

    let tableHTML = '<table>';

    tableData.forEach((row, rowIndex) => {
        if (withHeadings && rowIndex === 0) {
            tableHTML += '<thead>';
        }

        tableHTML += '<tr>';

        row.forEach((cellData, cellIndex) => {
            if (withHeadings && rowIndex === 0) {
                tableHTML += '<th>' + cellData + '</th>';
            } else {
                tableHTML += '<td>' + cellData + '</td>';
            }
        });

        tableHTML += '</tr>';

        if (withHeadings && rowIndex === 0) {
            tableHTML += '</thead>';
        }
    });

    tableHTML += '</table>';
    return tableHTML;
}

Repository owner deleted a comment from uc-Pri Mar 5, 2024
@imxaander
Copy link

Up

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

Successfully merging this pull request may close these issues.

5 participants