feeds.js 6.3 KB

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