From f24e580dbea458cd1d05536232b6c5ca7ab66ca8 Mon Sep 17 00:00:00 2001 From: pavittarx <29155477+pavittarx@users.noreply.github.com> Date: Tue, 18 May 2021 11:46:58 +0530 Subject: [PATCH] feature updates and bug fixes --- build/app.d.ts | 8 +- build/edjsHTML.browser.js | 2 +- build/edjsHTML.js | 2 +- build/edjsHTML.node.js | 2 +- build/transforms.d.ts | 6 + src/app.ts | 51 ++++++-- test/data.json | 242 +++++++++++++++++++------------------- test/shittyData.json | 125 ++++++++++++++++++++ test/test.js | 8 +- 9 files changed, 308 insertions(+), 138 deletions(-) create mode 100644 test/shittyData.json diff --git a/build/app.d.ts b/build/app.d.ts index 2c2557e..5f34c3c 100644 --- a/build/app.d.ts +++ b/build/app.d.ts @@ -1,8 +1,10 @@ -import { OutputData } from '@editorjs/editorjs'; +import { OutputData } from "@editorjs/editorjs"; import { block } from "./transforms"; declare type parser = { - parse(OutputData: OutputData): any; - parseBlock(block: block): any; + parse(OutputData: OutputData): Array; + parseStrict(OutputData: OutputData): Array | Error; + parseBlock(block: block): string; + validate(OutputData: OutputData): Array; }; declare const parser: (plugins?: {}) => parser; export default parser; diff --git a/build/edjsHTML.browser.js b/build/edjsHTML.browser.js index 9b51629..85ef751 100644 --- a/build/edjsHTML.browser.js +++ b/build/edjsHTML.browser.js @@ -1 +1 @@ -var edjsHTML=function(){"use strict";var t={delimiter:function(){return"
"},header:function(t){var e=t.data;return""+e.text+""},paragraph:function(t){return"

"+t.data.text+"

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

    "+t.data.text+"

    "},list:function(t){var e=t.data,n="unordered"===e.style?"ul":"ol",r="";return e.items&&(r=e.items.map((function(t){return"
  • "+t+"
  • "})).reduce((function(t,e){return t+e}),"")),"<"+n+">"+r+""},image:function(t){var e=t.data,n=e.caption?e.caption:"Image";return''+n+''},quote:function(t){var e=t.data;return"
    "+e.text+"
    - "+e.caption},code:function(t){return"
    "+t.data.code+"
    "}};function e(t){return new Error(' The Parser function of type "'+t+'" is not defined. \n\n Define your custom parser functions as: https://github.com/pavittarx/editorjs-html#extend-for-custom-blocks ')}return function(n){return void 0===n&&(n={}),Object.assign(t,n),{parse:function(n){return n.blocks.map((function(n){return t[n.type]?t[n.type](n):e(n.type)}))},parseBlock:function(n){return t[n.type]?t[n.type](n):e(n.type)}}}})); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).edjsHTML=t()}(this,(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="";return t.items&&(n=t.items.map((function(e){return"
  • "+e+"
  • "})).reduce((function(e,t){return e+t}),"")),"<"+r+">"+n+""},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('Unsupported embed service type. Supported are "youtube" and "vimeo"')}}};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){return void 0===n&&(n={}),Object.assign(n,e),{parse:function(r){return r.blocks.map((function(r){return e[r.type]?e[r.type](r):t(r.type)}))},parseBlock:function(r){return e[r.type]?e[r.type](r):t(r.type)},parseStrict:function(i){var o=i.blocks,u=r(n).validate({blocks:o});if(u.length)return new Error("Parser Functions missing for blocks: "+u.toString());for(var a=[],c=0;c"},header:function(t){var e=t.data;return""+e.text+""},paragraph:function(t){return"

    "+t.data.text+"

    "},list:function(t){var e=t.data,r="unordered"===e.style?"ul":"ol",n="";return e.items&&(n=e.items.map((function(t){return"
  • "+t+"
  • "})).reduce((function(t,e){return t+e}),"")),"<"+r+">"+n+""},image:function(t){var e=t.data,r=e.caption?e.caption:"Image";return''+r+''},quote:function(t){var e=t.data;return"
    "+e.text+"
    - "+e.caption},code:function(t){return"
    "+t.data.code+"
    "}};function e(t){return new Error(' The Parser function of type "'+t+'" is not defined. \n\n Define your custom parser functions as: https://github.com/pavittarx/editorjs-html#extend-for-custom-blocks ')}module.exports=function(r){return void 0===r&&(r={}),Object.assign(t,r),{parse:function(r){return r.blocks.map((function(r){return t[r.type]?t[r.type](r):e(r.type)}))},parseBlock:function(r){return t[r.type]?t[r.type](r):e(r.type)}}}; +"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="";return t.items&&(n=t.items.map((function(e){return"
  • "+e+"
  • "})).reduce((function(e,t){return e+t}),"")),"<"+r+">"+n+""},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('Unsupported embed service type. Supported are "youtube" and "vimeo"')}}};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){return void 0===n&&(n={}),Object.assign(n,e),{parse:function(r){return r.blocks.map((function(r){return e[r.type]?e[r.type](r):t(r.type)}))},parseBlock:function(r){return e[r.type]?e[r.type](r):t(r.type)},parseStrict:function(i){var o=i.blocks,u=r(n).validate({blocks:o});if(u.length)return new Error("Parser Functions missing for blocks: "+u.toString());for(var a=[],c=0;c; + parseStrict(OutputData: OutputData): Array | Error; + parseBlock(block: block): string; + validate(OutputData: OutputData): Array; +}; const parser = (plugins = {}): parser => { - Object.assign(transforms, plugins); + Object.assign(plugins, transforms); return { parse: ({ blocks }) => { return blocks.map((block) => { - return transforms[block.type]? - transforms[block.type](block) + return transforms[block.type] + ? transforms[block.type](block) : ParseFunctionError(block.type); }); }, @@ -24,7 +26,40 @@ const parser = (plugins = {}): parser => { ? transforms[block.type](block) : ParseFunctionError(block.type); }, + + parseStrict: ({ blocks }) => { + const parserFreeBlocks = parser(plugins).validate({ blocks }); + + if (parserFreeBlocks.length) + return new Error( + `Parser Functions missing for blocks: ${parserFreeBlocks.toString()}` + ); + + const parsed = []; + + for (let i = 0; i < blocks.length; i++) { + if (!transforms[blocks[i].type]) + throw ParseFunctionError(blocks[i].type); + + parsed.push(transforms[blocks[i].type](blocks[i])); + } + + return parsed; + }, + + validate: ({ blocks }) => { + const types = blocks + .map((item: block) => item.type) + .filter( + (item: string, index: number, blocksArr: Array) => + blocksArr.indexOf(item) === index + ); + + const parsers = Object.keys(transforms); + + return types.filter((type) => !parsers.includes(type)); + }, }; }; -export default parser \ No newline at end of file +export default parser; diff --git a/test/data.json b/test/data.json index 8160cdd..2a0cd8a 100644 --- a/test/data.json +++ b/test/data.json @@ -1,125 +1,125 @@ { - "time" : 1590210069249, - "blocks" : [ - { - "type" : "header", - "data" : { - "text" : "Editor.js", - "level" : 2 - } - }, - { - "type" : "paragraph", - "data" : { - "text" : "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text." - } - }, - { - "type" : "header", - "data" : { - "text" : "Key features", - "level" : 3 - } - }, - { - "type" : "list", - "data" : { - "style" : "unordered", - "items" : [ - "It is a block-styled editor", - "It returns clean data output in JSON", - "Designed to be extendable and pluggable with a simple API" - ] - } - }, - { - "type" : "header", - "data" : { - "text" : "What does it mean «block-styled editor»", - "level" : 3 - } - }, - { - "type" : "paragraph", - "data" : { - "text" : "Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core." - } - }, - { - "type" : "paragraph", - "data" : { - "text" : "There are dozens of ready-to-use Blocks and the simple API for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games." - } - }, - { - "type" : "header", - "data" : { - "text" : "What does it mean clean data output", - "level" : 3 - } - }, - { - "type" : "paragraph", - "data" : { - "text" : "Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below" - } - }, - { - "type" : "paragraph", - "data" : { - "text" : "Given data can be used as you want: render with HTML for Web clients, render natively for mobile apps, create markup for Facebook Instant Articles or Google AMP, generate an audio version and so on." - } - }, - { - "type" : "paragraph", - "data" : { - "text" : "Clean data is useful to sanitize, validate and process on the backend." - } - }, - { - "type" : "delimiter", - "data" : {} - }, - { - "type" : "paragraph", - "data" : { - "text" : "We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make it's core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏" - } - }, - { - "type" : "image", - "data" : { - "file" : { - "url" : "https://codex.so/public/app/img/external/codex2x.png" - }, - "caption" : "", - "withBorder" : false, - "stretched" : false, - "withBackground" : false - } - }, - { - "type" : "embed", - "data" : { - "service" : "youtube", - "source" : "https://www.youtube.com/watch?v=v7eMYQCNWeE", - "embed" : "https://www.youtube.com/embed/v7eMYQCNWeE", - "width" : 580, - "height" : 320, - "caption" : "" - } - }, - { - "type" : "embed", - "data" : { - "service" : "vimeo", - "source" : "https://vimeo.com/165206543", - "embed" : "https://player.vimeo.com/video/165206543?title=0&byline=0", - "width" : 580, - "height" : 320, - "caption" : "" - } + "time": 1590210069249, + "blocks": [ + { + "type": "header", + "data": { + "text": "Editor.js", + "level": 2 } + }, + { + "type": "paragraph", + "data": { + "text": "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text." + } + }, + { + "type": "header", + "data": { + "text": "Key features", + "level": 3 + } + }, + { + "type": "list", + "data": { + "style": "unordered", + "items": [ + "It is a block-styled editor", + "It returns clean data output in JSON", + "Designed to be extendable and pluggable with a simple API" + ] + } + }, + { + "type": "header", + "data": { + "text": "What does it mean «block-styled editor»", + "level": 3 + } + }, + { + "type": "paragraph", + "data": { + "text": "Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core." + } + }, + { + "type": "paragraph", + "data": { + "text": "There are dozens of ready-to-use Blocks and the simple API for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games." + } + }, + { + "type": "header", + "data": { + "text": "What does it mean clean data output", + "level": 3 + } + }, + { + "type": "paragraph", + "data": { + "text": "Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below" + } + }, + { + "type": "paragraph", + "data": { + "text": "Given data can be used as you want: render with HTML for Web clients, render natively for mobile apps, create markup for Facebook Instant Articles or Google AMP, generate an audio version and so on." + } + }, + { + "type": "paragraph", + "data": { + "text": "Clean data is useful to sanitize, validate and process on the backend." + } + }, + { + "type": "delimiter", + "data": {} + }, + { + "type": "paragraph", + "data": { + "text": "We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make it's core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏" + } + }, + { + "type": "image", + "data": { + "file": { + "url": "https://codex.so/public/app/img/external/codex2x.png" + }, + "caption": "", + "withBorder": false, + "stretched": false, + "withBackground": false + } + }, + { + "type": "embed", + "data": { + "service": "youtube", + "source": "https://www.youtube.com/watch?v=v7eMYQCNWeE", + "embed": "https://www.youtube.com/embed/v7eMYQCNWeE", + "width": 580, + "height": 320, + "caption": "" + } + }, + { + "type": "embed", + "data": { + "service": "vimeo", + "source": "https://vimeo.com/165206543", + "embed": "https://player.vimeo.com/video/165206543?title=0&byline=0", + "width": 580, + "height": 320, + "caption": "" + } + } ], - "version" : "2.17.0" + "version": "2.17.0" } diff --git a/test/shittyData.json b/test/shittyData.json new file mode 100644 index 0000000..de80aec --- /dev/null +++ b/test/shittyData.json @@ -0,0 +1,125 @@ +{ + "time": 1590210069249, + "blocks": [ + { + "type": "header", + "data": { + "text": "Editor.js", + "level": 2 + } + }, + { + "type": "paragraph", + "data": { + "text": "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text." + } + }, + { + "type": "header", + "data": { + "text": "Key features", + "level": 3 + } + }, + { + "type": "list", + "data": { + "style": "unordered", + "items": [ + "It is a block-styled editor", + "It returns clean data output in JSON", + "Designed to be extendable and pluggable with a simple API" + ] + } + }, + { + "type": "header", + "data": { + "text": "What does it mean «block-styled editor»", + "level": 3 + } + }, + { + "type": "paragraph", + "data": { + "text": "Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core." + } + }, + { + "type": "paragraph", + "data": { + "text": "There are dozens of ready-to-use Blocks and the simple API for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games." + } + }, + { + "type": "header", + "data": { + "text": "What does it mean clean data output", + "level": 3 + } + }, + { + "type": "paragraph", + "data": { + "text": "Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below" + } + }, + { + "type": "paragraph", + "data": { + "text": "Given data can be used as you want: render with HTML for Web clients, render natively for mobile apps, create markup for Facebook Instant Articles or Google AMP, generate an audio version and so on." + } + }, + { + "type": "paragraph", + "data": { + "text": "Clean data is useful to sanitize, validate and process on the backend." + } + }, + { + "type": "delimiter", + "data": {} + }, + { + "type": "paragraph", + "data": { + "text": "We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make it's core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏" + } + }, + { + "type": "image", + "data": { + "file": { + "url": "https://codex.so/public/app/img/external/codex2x.png" + }, + "caption": "", + "withBorder": false, + "stretched": false, + "withBackground": false + } + }, + { + "type": "squirrel", + "data": { + "service": "youtube", + "source": "https://www.youtube.com/watch?v=v7eMYQCNWeE", + "embed": "https://www.youtube.com/embed/v7eMYQCNWeE", + "width": 580, + "height": 320, + "caption": "" + } + }, + { + "type": "kaboom", + "data": { + "service": "vimeo", + "source": "https://vimeo.com/165206543", + "embed": "https://player.vimeo.com/video/165206543?title=0&byline=0", + "width": 580, + "height": 320, + "caption": "" + } + } + ], + "version": "2.17.0" +} diff --git a/test/test.js b/test/test.js index fa729bc..6500b40 100644 --- a/test/test.js +++ b/test/test.js @@ -1,5 +1,7 @@ -const edjsHTML = require('../build/edjsHTML'); -const data = require('./data.json'); +const edjsHTML = require("../build/edjsHTML"); +const data = require("./data.json"); const edjsParser = edjsHTML(); -console.log(edjsParser.parse(data)); \ No newline at end of file +console.log(edjsParser.parseStrict(data)); + +console.log(edjsParser.validate(data));