feeds.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getFeed = void 0;
  4. var stringify_js_1 = require("./stringify.js");
  5. var legacy_js_1 = require("./legacy.js");
  6. /**
  7. * Get the feed object from the root of a DOM tree.
  8. *
  9. * @category Feeds
  10. * @param doc - The DOM to to extract the feed from.
  11. * @returns The feed.
  12. */
  13. function getFeed(doc) {
  14. var feedRoot = getOneElement(isValidFeed, doc);
  15. return !feedRoot
  16. ? null
  17. : feedRoot.name === "feed"
  18. ? getAtomFeed(feedRoot)
  19. : getRssFeed(feedRoot);
  20. }
  21. exports.getFeed = getFeed;
  22. /**
  23. * Parse an Atom feed.
  24. *
  25. * @param feedRoot The root of the feed.
  26. * @returns The parsed feed.
  27. */
  28. function getAtomFeed(feedRoot) {
  29. var _a;
  30. var childs = feedRoot.children;
  31. var feed = {
  32. type: "atom",
  33. items: (0, legacy_js_1.getElementsByTagName)("entry", childs).map(function (item) {
  34. var _a;
  35. var children = item.children;
  36. var entry = { media: getMediaElements(children) };
  37. addConditionally(entry, "id", "id", children);
  38. addConditionally(entry, "title", "title", children);
  39. var href = (_a = getOneElement("link", children)) === null || _a === void 0 ? void 0 : _a.attribs["href"];
  40. if (href) {
  41. entry.link = href;
  42. }
  43. var description = fetch("summary", children) || fetch("content", children);
  44. if (description) {
  45. entry.description = description;
  46. }
  47. var pubDate = fetch("updated", children);
  48. if (pubDate) {
  49. entry.pubDate = new Date(pubDate);
  50. }
  51. return entry;
  52. }),
  53. };
  54. addConditionally(feed, "id", "id", childs);
  55. addConditionally(feed, "title", "title", childs);
  56. var href = (_a = getOneElement("link", childs)) === null || _a === void 0 ? void 0 : _a.attribs["href"];
  57. if (href) {
  58. feed.link = href;
  59. }
  60. addConditionally(feed, "description", "subtitle", childs);
  61. var updated = fetch("updated", childs);
  62. if (updated) {
  63. feed.updated = new Date(updated);
  64. }
  65. addConditionally(feed, "author", "email", childs, true);
  66. return feed;
  67. }
  68. /**
  69. * Parse a RSS feed.
  70. *
  71. * @param feedRoot The root of the feed.
  72. * @returns The parsed feed.
  73. */
  74. function getRssFeed(feedRoot) {
  75. var _a, _b;
  76. var childs = (_b = (_a = getOneElement("channel", feedRoot.children)) === null || _a === void 0 ? void 0 : _a.children) !== null && _b !== void 0 ? _b : [];
  77. var feed = {
  78. type: feedRoot.name.substr(0, 3),
  79. id: "",
  80. items: (0, legacy_js_1.getElementsByTagName)("item", feedRoot.children).map(function (item) {
  81. var children = item.children;
  82. var entry = { media: getMediaElements(children) };
  83. addConditionally(entry, "id", "guid", children);
  84. addConditionally(entry, "title", "title", children);
  85. addConditionally(entry, "link", "link", children);
  86. addConditionally(entry, "description", "description", children);
  87. var pubDate = fetch("pubDate", children);
  88. if (pubDate)
  89. entry.pubDate = new Date(pubDate);
  90. return entry;
  91. }),
  92. };
  93. addConditionally(feed, "title", "title", childs);
  94. addConditionally(feed, "link", "link", childs);
  95. addConditionally(feed, "description", "description", childs);
  96. var updated = fetch("lastBuildDate", childs);
  97. if (updated) {
  98. feed.updated = new Date(updated);
  99. }
  100. addConditionally(feed, "author", "managingEditor", childs, true);
  101. return feed;
  102. }
  103. var MEDIA_KEYS_STRING = ["url", "type", "lang"];
  104. var MEDIA_KEYS_INT = [
  105. "fileSize",
  106. "bitrate",
  107. "framerate",
  108. "samplingrate",
  109. "channels",
  110. "duration",
  111. "height",
  112. "width",
  113. ];
  114. /**
  115. * Get all media elements of a feed item.
  116. *
  117. * @param where Nodes to search in.
  118. * @returns Media elements.
  119. */
  120. function getMediaElements(where) {
  121. return (0, legacy_js_1.getElementsByTagName)("media:content", where).map(function (elem) {
  122. var attribs = elem.attribs;
  123. var media = {
  124. medium: attribs["medium"],
  125. isDefault: !!attribs["isDefault"],
  126. };
  127. for (var _i = 0, MEDIA_KEYS_STRING_1 = MEDIA_KEYS_STRING; _i < MEDIA_KEYS_STRING_1.length; _i++) {
  128. var attrib = MEDIA_KEYS_STRING_1[_i];
  129. if (attribs[attrib]) {
  130. media[attrib] = attribs[attrib];
  131. }
  132. }
  133. for (var _a = 0, MEDIA_KEYS_INT_1 = MEDIA_KEYS_INT; _a < MEDIA_KEYS_INT_1.length; _a++) {
  134. var attrib = MEDIA_KEYS_INT_1[_a];
  135. if (attribs[attrib]) {
  136. media[attrib] = parseInt(attribs[attrib], 10);
  137. }
  138. }
  139. if (attribs["expression"]) {
  140. media.expression = attribs["expression"];
  141. }
  142. return media;
  143. });
  144. }
  145. /**
  146. * Get one element by tag name.
  147. *
  148. * @param tagName Tag name to look for
  149. * @param node Node to search in
  150. * @returns The element or null
  151. */
  152. function getOneElement(tagName, node) {
  153. return (0, legacy_js_1.getElementsByTagName)(tagName, node, true, 1)[0];
  154. }
  155. /**
  156. * Get the text content of an element with a certain tag name.
  157. *
  158. * @param tagName Tag name to look for.
  159. * @param where Node to search in.
  160. * @param recurse Whether to recurse into child nodes.
  161. * @returns The text content of the element.
  162. */
  163. function fetch(tagName, where, recurse) {
  164. if (recurse === void 0) { recurse = false; }
  165. return (0, stringify_js_1.textContent)((0, legacy_js_1.getElementsByTagName)(tagName, where, recurse, 1)).trim();
  166. }
  167. /**
  168. * Adds a property to an object if it has a value.
  169. *
  170. * @param obj Object to be extended
  171. * @param prop Property name
  172. * @param tagName Tag name that contains the conditionally added property
  173. * @param where Element to search for the property
  174. * @param recurse Whether to recurse into child nodes.
  175. */
  176. function addConditionally(obj, prop, tagName, where, recurse) {
  177. if (recurse === void 0) { recurse = false; }
  178. var val = fetch(tagName, where, recurse);
  179. if (val)
  180. obj[prop] = val;
  181. }
  182. /**
  183. * Checks if an element is a feed root node.
  184. *
  185. * @param value The name of the element to check.
  186. * @returns Whether an element is a feed root node.
  187. */
  188. function isValidFeed(value) {
  189. return value === "rss" || value === "feed" || value === "rdf:RDF";
  190. }
  191. //# sourceMappingURL=feeds.js.map