Skip to content
This repository has been archived by the owner on Nov 25, 2019. It is now read-only.

Commit

Permalink
make JSXFromHTML sync
Browse files Browse the repository at this point in the history
  • Loading branch information
RafalFilipek committed Apr 11, 2017
1 parent c1a374c commit 144d849
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
16 changes: 3 additions & 13 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,11 @@ type ConvertFunc = (
mapElements?: ElementsMap,
mapInline?: string | Function,
mapBlock?: string | Function
) => Promise<*>;
) => any;

const convert: ConvertFunc = (html, mapElements, mapInline, mapBlock) => {
const getTag = createGetTagFunction(mapElements, mapInline, mapBlock);

return new Promise((resolve, reject) => {
textToDOM(html)
.then(dom => {
try {
resolve(toJSX(dom, getTag));
} catch (e) {
reject(e);
}
})
.catch(reject);
});
const dom = textToDOM(html);
return toJSX(dom, getTag);
};
export default convert;
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ class JsxHtml extends PureComponent {
}

setContent() {
convert(
const content = convert(
this.props.html,
this.props.mapElements,
this.props.mapInline,
this.props.mapBlock
).then(content => {
if (this.mounted) {
this.setState({ content });
}
});
);
if (this.mounted) {
this.setState({ content });
}
}

render() {
Expand Down
16 changes: 7 additions & 9 deletions src/textToDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import { Parser, DomHandler } from 'htmlparser2';

const textToDOM = (html: string): Promise<*> =>
new Promise((resolve, reject) => {
const handler = new DomHandler((error, dom) => {
error ? reject(error) : resolve(dom);
});
const parser = new Parser(handler);
parser.write(html);
parser.done();
});
const textToDOM = (html: string): any => {
const handler = new DomHandler();
const parser = new Parser(handler);
parser.write(html);
parser.done();
return handler.dom;
};

export default textToDOM;

0 comments on commit 144d849

Please sign in to comment.