Skip to content

Commit

Permalink
Added --normalize-html-whitespace CLI option
Browse files Browse the repository at this point in the history
  • Loading branch information
nippur72 committed Jul 8, 2016
1 parent d80de30 commit 6348e53
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"escodegen": "^1.8.0",
"esprima": "^2.7.1",
"lodash": "^4.11.1",
"normalize-html-whitespace": "^0.2.0",
"optionator": "^0.8.0",
"text-table": "^0.2.0"
},
Expand Down
5 changes: 5 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,10 @@ $ rt <filename> [<filename> ...] [<args>]`,
enum: Object.keys(reactNativeSupport),
default: reactNativeSupport.default,
description: `React native version to generate code for (${Object.keys(reactNativeSupport).join(', ')})`
}, {
option: 'normalize-html-whitespace',
type: 'Boolean',
default: 'false',
description: 'Remove repeating whitespace from HTML text.'
}]
});
12 changes: 11 additions & 1 deletion src/reactTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const cheerio = require('cheerio');
const _ = require('lodash');
const esprima = require('esprima');
const escodegen = require('escodegen');
const normalizeHtmlWhitespace = require('normalize-html-whitespace');
const reactDOMSupport = require('./reactDOMSupport');
const reactNativeSupport = require('./reactNativeSupport');
const reactPropTemplates = require('./reactPropTemplates');
Expand Down Expand Up @@ -55,6 +56,7 @@ const includeSrcAttr = 'src';
const requireAttr = 'rt-require';
const importAttr = 'rt-import';
const statelessAttr = 'rt-stateless';
const preAttr = 'rt-pre';

const reactTemplatesSelfClosingTags = [includeNode];

Expand Down Expand Up @@ -438,7 +440,15 @@ function convertHtmlToReact(node, context) {
const sanitizedComment = node.data.split('*/').join('* /');
return commentTemplate({data: sanitizedComment});
} else if (node.type === 'text') {
return trimHtmlText(node.data) ? utils.convertText(node, context, node.data) : '';
let text = node.data;
const parentNode = node.parent;
if (parentNode !== undefined) {
const preserveWhitespaces = parentNode.name === 'pre' || parentNode.name === 'textarea' || _.has(parentNode.attribs, preAttr);
if (context.options.normalizeHtmlWhitespace && !preserveWhitespaces) {
text = normalizeHtmlWhitespace(text);
}
}
return trimHtmlText(text) ? utils.convertText(node, context, text) : '';
}
}

Expand Down

0 comments on commit 6348e53

Please sign in to comment.