frontity
Version:
Frontity cli and entry point to other packages
28 lines (27 loc) • 1.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const simple_entity_decode_1 = __importDefault(require("simple-entity-decode"));
const containsHTMLEntities_1 = __importDefault(require("./containsHTMLEntities"));
const decode = (text) => {
// If we are free of HTML entities, just return the text
if (!(0, containsHTMLEntities_1.default)(text)) {
return text;
}
// simpleDecode only decodes the most common entities
// We check if it escaped all possible entities and if not,
// we fall back on full decoding
const decodedText = (0, simple_entity_decode_1.default)(text);
if (!(0, containsHTMLEntities_1.default)(decodedText)) {
return decodedText;
}
// We find all the leading whitespace
const whitespaceMatch = text.match(/^[ \t]+/);
const whitespace = whitespaceMatch ? whitespaceMatch[0] : "";
const doc = new DOMParser().parseFromString(text, "text/html");
// We restore the whitespace in the result, because DOMParser will strip it out
return whitespace + doc.documentElement.textContent;
};
exports.default = decode;