react-htmlcontent
Advanced tools
Comparing version 1.2.10 to 2.0.0
@@ -8,62 +8,79 @@ "use strict"; | ||
}); | ||
exports.default = void 0; | ||
exports.default = enhanceTypoInHTML; | ||
var _typography = _interopRequireDefault(require("./typography")); | ||
var containsHtmlPatt = /([^<]*<([A-z]){1,}[^>]*>[^<]*<[^>]*>[^<]*)+/gi; | ||
var replaceHtmlContentPatt = /^(.+)?(<([A-z]{1,})[^>]*>)(.+?)(<\/\3>)([^<]*)?$/; | ||
var _dompurify = _interopRequireDefault(require("dompurify")); | ||
function handleNbspBeforeTag(str) { | ||
var arr = str.split(/\s/); | ||
var _himalaya = require("himalaya"); | ||
if (arr.length >= 2) { | ||
return arr[arr.length - 1] === '' && arr[arr.length - 2].length === 1 || arr[0] === '' && arr[1].length === 1 ? ' ' : ' '; | ||
} else { | ||
return ''; | ||
} | ||
} | ||
var enhance = function enhance(text) { | ||
return (0, _typography.default)(text, 'cs'); | ||
}; | ||
var loc = function loc(str) { | ||
return process.stdout.write(str + '\n\n'); | ||
var isText = function isText(_ref) { | ||
var type = _ref.type; | ||
return type === 'text'; | ||
}; | ||
var enhanceTypo = function enhanceTypo(data) { | ||
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cs'; | ||
var config = arguments.length > 2 ? arguments[2] : undefined; | ||
var first = function first(arr) { | ||
if (Array.isArray(arr) && arr.length > 0) return arr[0];else throw new Error('Bad input. Expected array.'); | ||
}; | ||
if (data && typeof data === 'string') { | ||
if (containsHtmlPatt.test(data)) { | ||
return data.replace(replaceHtmlContentPatt, function (_) { | ||
var beforeTag = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var startTag = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; | ||
var tagName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ''; | ||
var body = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : ''; | ||
var endTag = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : ''; | ||
var afterTag = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : ''; | ||
var last = function last(arr) { | ||
if (Array.isArray(arr) && arr.length > 0) return arr[arr.length - 1];else throw new Error('Bad input. Expected array.'); | ||
}; | ||
/** | ||
* Walks HTML AST | ||
* @param {Object} node Parsed HTML AST | ||
* @param {Function} callback AST node transformer | ||
*/ | ||
if (containsHtmlPatt.test(beforeTag)) { | ||
return enhanceTypo(beforeTag, locale, config); | ||
} else { | ||
return enhanceTypo(beforeTag, locale, config) + handleNbspBeforeTag(beforeTag) + startTag + enhanceTypo(body, locale, config) + endTag + enhanceTypo(afterTag, locale, config) + handleNbspBeforeTag(afterTag); | ||
} | ||
}); | ||
} else { | ||
var before = data.replace(/^(\s*).+/, '$1'); | ||
var after = data.replace(/^.+(\s*)$/, '$1'); | ||
return "".concat(before).concat((0, _typography.default)(data, locale, config)).concat(after); | ||
} | ||
function walk(node, callback) { | ||
var children; | ||
if (Array.isArray(node)) { | ||
children = node; | ||
} else { | ||
callback(node); | ||
children = node.children; | ||
} | ||
return ''; | ||
}; | ||
if (children) { | ||
var child = children[0]; | ||
var i = 0; | ||
var _default = function _default(str) { | ||
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
rest[_key - 1] = arguments[_key]; | ||
while (child) { | ||
walk(child, callback); | ||
child = children[++i]; | ||
} | ||
} | ||
} | ||
/** | ||
* Enhances text in HTML | ||
* @param {String} sourceContent | ||
* @returns {String} enhanced text | ||
*/ | ||
var r = enhanceTypo.apply(void 0, [str].concat(rest)); | ||
return r.replace(/\s\s/g, ' ').replace(/([A-z]{1,})\s?(\([A-z]{1}\))\s?([A-z]{1,})/, '$1$2$3'); | ||
}; | ||
exports.default = _default; | ||
function enhanceTypoInHTML(sourceContent) { | ||
var content = _dompurify.default.sanitize(sourceContent, { | ||
ADD_ATTR: ['target'], | ||
USE_PROFILES: { | ||
html: true | ||
} | ||
}); | ||
content = (0, _himalaya.parse)(content); | ||
walk(content, function (node) { | ||
if (isText(node)) { | ||
var before = first(node.content.split(/\s/)) === '' ? ' ' : ''; | ||
var after = last(node.content.split(/\s/)) === '' ? ' ' : ''; | ||
console.log("\"".concat(node.content, "\" -> before: \"").concat(before, "\" after: \"").concat(after, "\"")); | ||
node.content = "".concat(before).concat(enhance(node.content).replace('Po (d)klady', 'Po(d)klady')).concat(after); | ||
} | ||
}); | ||
content = (0, _himalaya.stringify)(content); | ||
return content; | ||
} |
@@ -61,3 +61,3 @@ "use strict"; | ||
function _default(string) { | ||
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-us'; | ||
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cs'; | ||
var configuration = arguments.length > 2 ? arguments[2] : undefined; | ||
@@ -64,0 +64,0 @@ var currentLocale = new _locale.default(locale); |
{ | ||
"name": "react-htmlcontent", | ||
"version": "1.2.10", | ||
"version": "2.0.0", | ||
"description": "React HTMLContent Component", | ||
@@ -49,6 +49,7 @@ "repository": { | ||
"peerDependencies": { | ||
"react": "^16.8.6" | ||
"react": ">=16.0.0" | ||
}, | ||
"dependencies": { | ||
"sanitize-html": "^1.20.1" | ||
"himalaya": "^1.1.0", | ||
"dompurify": "1.0.11" | ||
}, | ||
@@ -55,0 +56,0 @@ "main": "dist/index.js", |
@@ -27,5 +27,5 @@ import enhanceTypo from '../src/enhanceTypo' | ||
).toEqual( | ||
'Se založením účtu a převodem hypotéky vám rádi poradíme i na <a href="https://www.airbank.cz/mapa-pobocek-a-bankomatu/" target="_blank">našich pobočkách</a>.', | ||
"Se založením účtu a převodem hypotéky vám rádi poradíme i na <a target='_blank' href='https://www.airbank.cz/mapa-pobocek-a-bankomatu/'>našich pobočkách</a>.", | ||
) | ||
}) | ||
}) |
@@ -21,7 +21,9 @@ import { enhanceTypo } from '../src/index' | ||
describe(' in html string', () => { | ||
const source = '<p>officia <a href="https://ib.airbank.cz" target="_blank">dolore</a></p>' | ||
const expectedOutput = '<p>officia <a href="https://ib.airbank.cz" target="_blank">dolore</a></p>' | ||
const source = | ||
'<p>officia <a href="https://ib.airbank.cz" target="_blank">dolore</a></p>' | ||
const expectedOutput = | ||
"<p>officia <a target='_blank' href='https://ib.airbank.cz'>dolore</a></p>" | ||
it('foobar', () => { | ||
assert.equal(enhanceTypo(source, 'cs'), expectedOutput) | ||
}) | ||
}) | ||
}) |
2944
655607
3
+ Addeddompurify@1.0.11
+ Addedhimalaya@^1.1.0
+ Addeddompurify@1.0.11(transitive)
+ Addedhimalaya@1.1.0(transitive)
+ Addedreact@19.0.0(transitive)
- Removedsanitize-html@^1.20.1
- Removeddom-serializer@1.4.1(transitive)
- Removeddomelementtype@2.3.0(transitive)
- Removeddomhandler@3.3.04.3.1(transitive)
- Removeddomutils@2.8.0(transitive)
- Removedentities@2.2.0(transitive)
- Removedhtmlparser2@4.1.0(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedloose-envify@1.4.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedparse-srcset@1.0.2(transitive)
- Removedpicocolors@0.2.1(transitive)
- Removedpostcss@7.0.39(transitive)
- Removedprop-types@15.8.1(transitive)
- Removedreact@16.14.0(transitive)
- Removedreact-is@16.13.1(transitive)
- Removedsanitize-html@1.27.5(transitive)
- Removedsource-map@0.6.1(transitive)