feeds.js 5.8 KB

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