From 73c04f86146beb3a8e178779a5b0bf32866cb13d Mon Sep 17 00:00:00 2001 From: navjot11022001 Date: Thu, 13 Oct 2022 18:10:19 +0530 Subject: [PATCH] added-support-for-table-parser --- build/edjsHTML.browser.js | 2 +- build/edjsHTML.js | 2 +- build/edjsHTML.node.js | 2 +- build/transforms.d.ts | 1 + src/transforms.ts | 16 ++++++++++++- test/data.json | 9 ++++++++ test/test.js | 48 +++++++++++++++++++++++++++------------ 7 files changed, 61 insertions(+), 19 deletions(-) diff --git a/build/edjsHTML.browser.js b/build/edjsHTML.browser.js index 1f399b4..6dff0c6 100644 --- a/build/edjsHTML.browser.js +++ b/build/edjsHTML.browser.js @@ -1 +1 @@ -var edjsHTML=function(){"use strict";var e={delimiter:function(){return"
"},header:function(e){var t=e.data;return""+t.text+""},paragraph:function(e){return"

"+e.data.text+"

"},list:function(e){var t=e.data,r="unordered"===t.style?"ul":"ol",n=function(e,t){var r=e.map((function(e){if(!e.content&&!e.items)return"
  • "+e+"
  • ";var r="";return e.items&&(r=n(e.items,t)),e.content?"
  • "+e.content+"
  • "+r:void 0}));return"<"+t+">"+r.join("")+""};return n(t.items,r)},image:function(e){var t=e.data,r=t.caption?t.caption:"Image";return''+r+''},quote:function(e){var t=e.data;return"
    "+t.text+"
    - "+t.caption},code:function(e){return"
    "+e.data.code+"
    "},embed:function(e){var t=e.data;switch(t.service){case"vimeo":return'';case"youtube":return'';default:throw new Error("Only Youtube and Vime Embeds are supported right now.")}}};function t(e){return new Error(' The Parser function of type "'+e+'" is not defined. \n\n Define your custom parser functions as: https://github.com/pavittarx/editorjs-html#extend-for-custom-blocks ')}var r=function(n){void 0===n&&(n={});var i=Object.assign({},e,n);return{parse:function(e){return e.blocks.map((function(e){return i[e.type]?i[e.type](e):t(e.type)}))},parseBlock:function(e){return i[e.type]?i[e.type](e):t(e.type)},parseStrict:function(e){var n=e.blocks,o=r(i).validate({blocks:n});if(o.length)throw new Error("Parser Functions missing for blocks: "+o.toString());for(var u=[],a=0;a"},header:function(e){var t=e.data;return""+t.text+""},paragraph:function(e){return"

    "+e.data.text+"

    "},list:function(e){var t=e.data,r="unordered"===t.style?"ul":"ol",n=function(e,t){var r=e.map((function(e){if(!e.content&&!e.items)return"
  • "+e+"
  • ";var r="";return e.items&&(r=n(e.items,t)),e.content?"
  • "+e.content+"
  • "+r:void 0}));return"<"+t+">"+r.join("")+""};return n(t.items,r)},image:function(e){var t=e.data,r=t.caption?t.caption:"Image";return''+r+''},quote:function(e){var t=e.data;return"
    "+t.text+"
    - "+t.caption},code:function(e){return"
    "+e.data.code+"
    "},embed:function(e){var t=e.data;switch(t.service){case"vimeo":return'';case"youtube":return'';default:throw new Error("Only Youtube and Vime Embeds are supported right now.")}},table:function(e){return""+function(e){return""+e.content.map((function(e){return""+e.reduce((function(e,t){return e+=""}),"")+""})).join("")+""}(e.data)+"
    "+t+"
    "}};function t(e){return new Error(' The Parser function of type "'+e+'" is not defined. \n\n Define your custom parser functions as: https://github.com/pavittarx/editorjs-html#extend-for-custom-blocks ')}var r=function(n){void 0===n&&(n={});var i=Object.assign({},e,n);return{parse:function(e){return e.blocks.map((function(e){return i[e.type]?i[e.type](e):t(e.type)}))},parseBlock:function(e){return i[e.type]?i[e.type](e):t(e.type)},parseStrict:function(e){var n=e.blocks,o=r(i).validate({blocks:n});if(o.length)throw new Error("Parser Functions missing for blocks: "+o.toString());for(var u=[],a=0;a"},header:function(e){var t=e.data;return""+t.text+""},paragraph:function(e){return"

    "+e.data.text+"

    "},list:function(e){var t=e.data,r="unordered"===t.style?"ul":"ol",n=function(e,t){var r=e.map((function(e){if(!e.content&&!e.items)return"
  • "+e+"
  • ";var r="";return e.items&&(r=n(e.items,t)),e.content?"
  • "+e.content+"
  • "+r:void 0}));return"<"+t+">"+r.join("")+""};return n(t.items,r)},image:function(e){var t=e.data,r=t.caption?t.caption:"Image";return''+r+''},quote:function(e){var t=e.data;return"
    "+t.text+"
    - "+t.caption},code:function(e){return"
    "+e.data.code+"
    "},embed:function(e){var t=e.data;switch(t.service){case"vimeo":return'';case"youtube":return'';default:throw new Error("Only Youtube and Vime Embeds are supported right now.")}}};function t(e){return new Error(' The Parser function of type "'+e+'" is not defined. \n\n Define your custom parser functions as: https://github.com/pavittarx/editorjs-html#extend-for-custom-blocks ')}var r=function(n){void 0===n&&(n={});var i=Object.assign({},e,n);return{parse:function(e){return e.blocks.map((function(e){return i[e.type]?i[e.type](e):t(e.type)}))},parseBlock:function(e){return i[e.type]?i[e.type](e):t(e.type)},parseStrict:function(e){var n=e.blocks,o=r(i).validate({blocks:n});if(o.length)throw new Error("Parser Functions missing for blocks: "+o.toString());for(var u=[],a=0;a"},header:function(e){var t=e.data;return""+t.text+""},paragraph:function(e){return"

    "+e.data.text+"

    "},list:function(e){var t=e.data,r="unordered"===t.style?"ul":"ol",n=function(e,t){var r=e.map((function(e){if(!e.content&&!e.items)return"
  • "+e+"
  • ";var r="";return e.items&&(r=n(e.items,t)),e.content?"
  • "+e.content+"
  • "+r:void 0}));return"<"+t+">"+r.join("")+""};return n(t.items,r)},image:function(e){var t=e.data,r=t.caption?t.caption:"Image";return''+r+''},quote:function(e){var t=e.data;return"
    "+t.text+"
    - "+t.caption},code:function(e){return"
    "+e.data.code+"
    "},embed:function(e){var t=e.data;switch(t.service){case"vimeo":return'';case"youtube":return'';default:throw new Error("Only Youtube and Vime Embeds are supported right now.")}},table:function(e){return""+function(e){return""+e.content.map((function(e){return""+e.reduce((function(e,t){return e+=""}),"")+""})).join("")+""}(e.data)+"
    "+t+"
    "}};function t(e){return new Error(' The Parser function of type "'+e+'" is not defined. \n\n Define your custom parser functions as: https://github.com/pavittarx/editorjs-html#extend-for-custom-blocks ')}var r=function(n){void 0===n&&(n={});var i=Object.assign({},e,n);return{parse:function(e){return e.blocks.map((function(e){return i[e.type]?i[e.type](e):t(e.type)}))},parseBlock:function(e){return i[e.type]?i[e.type](e):t(e.type)},parseStrict:function(e){var n=e.blocks,o=r(i).validate({blocks:n});if(o.length)throw new Error("Parser Functions missing for blocks: "+o.toString());for(var u=[],a=0;a"},header:function(e){var t=e.data;return""+t.text+""},paragraph:function(e){return"

    "+e.data.text+"

    "},list:function(e){var t=e.data,r="unordered"===t.style?"ul":"ol",n=function(e,t){var r=e.map((function(e){if(!e.content&&!e.items)return"
  • "+e+"
  • ";var r="";return e.items&&(r=n(e.items,t)),e.content?"
  • "+e.content+"
  • "+r:void 0}));return"<"+t+">"+r.join("")+""};return n(t.items,r)},image:function(e){var t=e.data,r=t.caption?t.caption:"Image";return''+r+''},quote:function(e){var t=e.data;return"
    "+t.text+"
    - "+t.caption},code:function(e){return"
    "+e.data.code+"
    "},embed:function(e){var t=e.data;switch(t.service){case"vimeo":return'';case"youtube":return'';default:throw new Error("Only Youtube and Vime Embeds are supported right now.")}}};function t(e){return new Error(' The Parser function of type "'+e+'" is not defined. \n\n Define your custom parser functions as: https://github.com/pavittarx/editorjs-html#extend-for-custom-blocks ')}var r=function(n){void 0===n&&(n={});var i=Object.assign({},e,n);return{parse:function(e){return e.blocks.map((function(e){return i[e.type]?i[e.type](e):t(e.type)}))},parseBlock:function(e){return i[e.type]?i[e.type](e):t(e.type)},parseStrict:function(e){var n=e.blocks,o=r(i).validate({blocks:n});if(o.length)throw new Error("Parser Functions missing for blocks: "+o.toString());for(var u=[],a=0;a"},header:function(e){var t=e.data;return""+t.text+""},paragraph:function(e){return"

    "+e.data.text+"

    "},list:function(e){var t=e.data,r="unordered"===t.style?"ul":"ol",n=function(e,t){var r=e.map((function(e){if(!e.content&&!e.items)return"
  • "+e+"
  • ";var r="";return e.items&&(r=n(e.items,t)),e.content?"
  • "+e.content+"
  • "+r:void 0}));return"<"+t+">"+r.join("")+""};return n(t.items,r)},image:function(e){var t=e.data,r=t.caption?t.caption:"Image";return''+r+''},quote:function(e){var t=e.data;return"
    "+t.text+"
    - "+t.caption},code:function(e){return"
    "+e.data.code+"
    "},embed:function(e){var t=e.data;switch(t.service){case"vimeo":return'';case"youtube":return'';default:throw new Error("Only Youtube and Vime Embeds are supported right now.")}},table:function(e){return""+function(e){return""+e.content.map((function(e){return""+e.reduce((function(e,t){return e+=""}),"")+""})).join("")+""}(e.data)+"
    "+t+"
    "}};function t(e){return new Error(' The Parser function of type "'+e+'" is not defined. \n\n Define your custom parser functions as: https://github.com/pavittarx/editorjs-html#extend-for-custom-blocks ')}var r=function(n){void 0===n&&(n={});var i=Object.assign({},e,n);return{parse:function(e){return e.blocks.map((function(e){return i[e.type]?i[e.type](e):t(e.type)}))},parseBlock:function(e){return i[e.type]?i[e.type](e):t(e.type)},parseStrict:function(e){var n=e.blocks,o=r(i).validate({blocks:n});if(o.length)throw new Error("Parser Functions missing for blocks: "+o.toString());for(var u=[],a=0;a { + const tableParser = (data: any) => { + const tableBody = data.content.map( + (elem: any) => + `${elem.reduce((acc: string, curr: string) => { + acc += `${curr}`; + return acc; + }, "")}` + ); + return `${tableBody.join("")}`; + }; + return `${tableParser(data)}
    `; + }, +}; export default transforms; diff --git a/test/data.json b/test/data.json index 17c8503..41fec79 100644 --- a/test/data.json +++ b/test/data.json @@ -158,6 +158,15 @@ } ] } + }, + { + "type": "table", + "data": { + "content": [ + ["td1", "td2"], + ["td3", "td4"] + ] + } } ], "version": "2.17.0" diff --git a/test/test.js b/test/test.js index 28a5606..838a8c9 100644 --- a/test/test.js +++ b/test/test.js @@ -8,24 +8,42 @@ console.log(edjsParser.validate(data)); // test for custom parser const customParser = edjsHTML({ - custom: function({data}) { - return `success`; - }, - paragraph: function({data}) { - return `

    override test

    `; - } -}) + custom: function ({ data }) { + return `success`; + }, + paragraph: function ({ data }) { + return `

    override test

    `; + }, +}); -console.log(customParser.parseStrict({ +console.log( + customParser.parseStrict({ blocks: [ - { - type: "custom", - data: {} - } - ] -})) + { + type: "custom", + data: {}, + }, + ], + }) +); console.log(customParser.parseStrict(data)); console.log(customParser.validate(data)); // test for issue #21 -console.log(edjsParser.parseBlock({type: "paragraph", data: {text: "foo bar"}}) === '

    foo bar

    '); +console.log( + edjsParser.parseBlock({ type: "paragraph", data: { text: "foo bar" } }) === + "

    foo bar

    " +); + +//test for table support +console.log( + edjsParser.parseBlock({ + type: "table", + data: { + content: [ + ["td1", "td2"], + ["td3", "td4"], + ], + }, + }) +);