Skip to content

Commit

Permalink
feature updates and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pavittarx committed May 18, 2021
1 parent 9e20c83 commit f24e580
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 138 deletions.
8 changes: 5 additions & 3 deletions build/app.d.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
parseStrict(OutputData: OutputData): Array<string> | Error;
parseBlock(block: block): string;
validate(OutputData: OutputData): Array<string>;
};
declare const parser: (plugins?: {}) => parser;
export default parser;
2 changes: 1 addition & 1 deletion build/edjsHTML.browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/edjsHTML.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/edjsHTML.node.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions build/transforms.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export declare type transforms = {
image(block: block): string;
quote(block: block): string;
code(block: block): string;
embed(block: block): string;
};
export declare type block = {
type: string;
Expand All @@ -23,6 +24,11 @@ export declare type block = {
items?: string[];
style?: string;
code?: string;
service?: "vimeo" | "youtube";
source?: string;
embed?: string;
width?: number;
height?: number;
};
};
declare const transforms: transforms;
Expand Down
51 changes: 43 additions & 8 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { OutputData } from '@editorjs/editorjs';
import { OutputData } from "@editorjs/editorjs";
import transforms, { block } from "./transforms";
import { ParseFunctionError } from "./errors";

type parser = {
parse(OutputData: OutputData): any;
parseBlock(block: block): any;
}
parse(OutputData: OutputData): Array<string>;
parseStrict(OutputData: OutputData): Array<string> | Error;
parseBlock(block: block): string;
validate(OutputData: OutputData): Array<string>;
};

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);
});
},
Expand All @@ -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<string>) =>
blocksArr.indexOf(item) === index
);

const parsers = Object.keys(transforms);

return types.filter((type) => !parsers.includes(type));
},
};
};

export default parser
export default parser;
Loading

0 comments on commit f24e580

Please sign in to comment.