Ver código fonte

Fix HTMLElement not defined

ghorsington 4 anos atrás
pai
commit
382306c370

+ 5 - 2
bot/src/commands/forums_news_checker.ts

@@ -3,7 +3,7 @@ import interval from "interval-promise";
 import { client, FORUMS_DOMAIN } from "../client";
 import sha1 from "sha1";
 import { TextChannel, Message, ReactionCollector, MessageReaction, Collector, User, Channel } from "discord.js";
-import { Dict } from "../util";
+import { Dict, isHtmlNode } from "../util";
 import { getRepository, Not, IsNull } from "typeorm";
 import { PostedForumNewsItem } from "@shared/db/entity/PostedForumsNewsItem";
 import { KnownChannel } from "@shared/db/entity/KnownChannel";
@@ -34,7 +34,10 @@ export class ForumsNewsChecker {
         });
         this.turndown.addRule("link", {
             filter: (node: HTMLElement) => node.nodeName === "A" && node.getAttribute("href") != null,
-            replacement: (content: string, node: Node) => (node instanceof HTMLElement ? node.getAttribute("href") : null) ?? ""
+            replacement: (content: string, node: Node) => {
+                logger.info("Node type: %s, htmlelement type: %s", node.nodeType, Node.ELEMENT_NODE);
+                return (isHtmlNode(node) ? node.getAttribute("href") : null) ?? "";
+            }
         });
     }
 

+ 2 - 2
bot/src/commands/news_aggregator.ts

@@ -5,7 +5,7 @@ import sha1 from "sha1";
 import * as path from "path";
 import * as fs from "fs";
 import { HTML2BBCode } from "html2bbcode";
-import { Dict } from "../util";
+import { Dict, isHtmlNode } from "../util";
 import { IAggregator, NewsPostItem } from "./aggregators/aggregator";
 import { TextChannel, Message, Channel, ReactionCollector, MessageReaction, User, Collector, MessageEmbed } from "discord.js";
 import { getRepository, IsNull, Not } from "typeorm";
@@ -39,7 +39,7 @@ export class NewsAggregator {
         });
         this.turndown.addRule("link", {
             filter: (node: HTMLElement) => node.nodeName === "A" && node.getAttribute("href") != null,
-            replacement: (content: string, node: Node) => (node instanceof HTMLElement ? node.getAttribute("href") : null) ?? ""
+            replacement: (content: string, node: Node) => (isHtmlNode(node) ? node.getAttribute("href") : null) ?? ""
         });
     }
 

+ 4 - 0
bot/src/util.ts

@@ -75,4 +75,8 @@ export function getNumberEnums<E>(e : Record<keyof E, number>) : number[] {
 
 export function formatString(str: string, vars: Record<string, string>): string {
     return Object.keys(vars).filter(s => Object.prototype.hasOwnProperty.call(vars, s)).reduce((s, cur) => s.replace(`{${cur}}`, vars[cur]), str);
+}
+
+export function isHtmlNode(node: Node): node is HTMLElement {
+    return node.nodeType == Node.ELEMENT_NODE;
 }