utils.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const utils = module.exports = {};
  2. const entities = require('entities');
  3. const xml2js = require('xml2js');
  4. utils.stripHtml = function(str) {
  5. str = str.replace(/([^\n])<\/?(h|br|p|ul|ol|li|blockquote|section|table|tr|div)(?:.|\n)*?>([^\n])/gm, '$1\n$3')
  6. str = str.replace(/<(?:.|\n)*?>/gm, '');
  7. return str;
  8. }
  9. utils.getSnippet = function(str) {
  10. return entities.decodeHTML(utils.stripHtml(str)).trim();
  11. }
  12. utils.getLink = function(links, rel, fallbackIdx) {
  13. if (!links) return;
  14. for (let i = 0; i < links.length; ++i) {
  15. if (links[i].$.rel === rel) return links[i].$.href;
  16. }
  17. if (links[fallbackIdx]) return links[fallbackIdx].$.href;
  18. }
  19. utils.getContent = function(content) {
  20. if (typeof content._ === 'string') {
  21. return content._;
  22. } else if (typeof content === 'object') {
  23. let builder = new xml2js.Builder({headless: true, explicitRoot: true, rootName: 'div', renderOpts: {pretty: false}});
  24. return builder.buildObject(content);
  25. } else {
  26. return content;
  27. }
  28. }
  29. utils.copyFromXML = function(xml, dest, fields) {
  30. fields.forEach(function(f) {
  31. let from = f;
  32. let to = f;
  33. let options = {};
  34. if (Array.isArray(f)) {
  35. from = f[0];
  36. to = f[1];
  37. if (f.length > 2) {
  38. options = f[2];
  39. }
  40. }
  41. const { keepArray, includeSnippet } = options;
  42. if (xml[from] !== undefined){
  43. dest[to] = keepArray ? xml[from] : xml[from][0];
  44. }
  45. if (dest[to] && typeof dest[to]._ === 'string') {
  46. dest[to]=dest[to]._;
  47. }
  48. if (includeSnippet && dest[to] && typeof dest[to] === 'string') {
  49. dest[to + 'Snippet'] = utils.getSnippet(dest[to]);
  50. }
  51. })
  52. }
  53. utils.maybePromisify = function(callback, promise) {
  54. if (!callback) return promise;
  55. return promise.then(
  56. data => setTimeout(() => callback(null, data)),
  57. err => setTimeout(() => callback(err))
  58. );
  59. }
  60. const DEFAULT_ENCODING = 'utf8';
  61. const ENCODING_REGEX = /(encoding|charset)\s*=\s*(\S+)/;
  62. const SUPPORTED_ENCODINGS = ['ascii', 'utf8', 'utf16le', 'ucs2', 'base64', 'latin1', 'binary', 'hex'];
  63. const ENCODING_ALIASES = {
  64. 'utf-8': 'utf8',
  65. 'iso-8859-1': 'latin1',
  66. }
  67. utils.getEncodingFromContentType = function(contentType) {
  68. contentType = contentType || '';
  69. let match = contentType.match(ENCODING_REGEX);
  70. let encoding = (match || [])[2] || '';
  71. encoding = encoding.toLowerCase();
  72. encoding = ENCODING_ALIASES[encoding] || encoding;
  73. if (!encoding || SUPPORTED_ENCODINGS.indexOf(encoding) === -1) {
  74. encoding = DEFAULT_ENCODING;
  75. }
  76. return encoding;
  77. }