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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

1 change: 1 addition & 0 deletions build/transforms.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export declare type transforms = {
quote(block: block): string;
code(block: block): string;
embed(block: block): string;
table(block: block): string;
};
declare type ListItem = {
content: string;
Expand Down
16 changes: 15 additions & 1 deletion src/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type transforms = {
quote(block: block): string;
code(block: block): string;
embed(block: block): string;
table(block: block): string;
};

type ListItem = {
Expand Down Expand Up @@ -97,6 +98,19 @@ const transforms: transforms = {
);
}
},
};

table: ({ data }) => {
const tableParser = (data: any) => {
const tableBody = data.content.map(
(elem: any) =>
`<tr>${elem.reduce((acc: string, curr: string) => {
acc += `<td>${curr}</td>`;
return acc;
}, "")}</tr>`
);
return `<tbody>${tableBody.join("")}</tbody>`;
};
return `<table>${tableParser(data)}</table>`;
},
};
export default transforms;
9 changes: 9 additions & 0 deletions test/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@
}
]
}
},
{
"type": "table",
"data": {
"content": [
["td1", "td2"],
["td3", "td4"]
]
}
}
],
"version": "2.17.0"
Expand Down
48 changes: 33 additions & 15 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,42 @@ console.log(edjsParser.validate(data));

// test for custom parser
const customParser = edjsHTML({
custom: function({data}) {
return `<custom>success</custom>`;
},
paragraph: function({data}) {
return `<p>override test</p>`;
}
})
custom: function ({ data }) {
return `<custom>success</custom>`;
},
paragraph: function ({ data }) {
return `<p>override test</p>`;
},
});

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"}}) === '<p>foo bar</p>');
console.log(
edjsParser.parseBlock({ type: "paragraph", data: { text: "foo bar" } }) ===
"<p>foo bar</p>"
);

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