|
| 1 | +import { compile } from "html-to-text"; |
| 2 | + |
1 | 3 | import { escapeMrkdwn } from "../postingGate.js"; |
2 | 4 |
|
3 | 5 | const MSG_NOT_FOUND = (term) => `:red-x: No definition found for "${escapeMrkdwn(term)}".`; |
4 | 6 | const moreSensesLine = (n, term) => |
5 | 7 | `<https://en.wiktionary.org/wiki/${encodeURIComponent(term)}|+${n} more sense${n === 1 ? "" : "s"} not shown>`; |
6 | 8 |
|
| 9 | +const wikiHtmlToText = compile({ |
| 10 | + wordwrap: false, |
| 11 | + selectors: [ |
| 12 | + { selector: "a", format: "inline" }, |
| 13 | + { selector: "ol", format: "skip" }, |
| 14 | + { selector: "script", format: "skip" }, |
| 15 | + { selector: "style", format: "skip" }, |
| 16 | + ], |
| 17 | +}); |
| 18 | + |
7 | 19 | function stripWikiHtml(html) { |
8 | | - return (html || "") |
9 | | - .replace(/<style[^>]*>[\s\S]*?<\/style>/gi, "") |
10 | | - .replace(/<ol[\s>][\s\S]*$/i, "") |
11 | | - .replace(/<[^>]+>/g, "") |
12 | | - .replace(/&/g, "&") |
13 | | - .replace(/</g, "<") |
14 | | - .replace(/>/g, ">") |
15 | | - .replace(/'/g, "'") |
16 | | - .replace(/"/g, '"') |
17 | | - .trim(); |
| 20 | + return wikiHtmlToText(html || "").trim(); |
18 | 21 | } |
19 | 22 |
|
20 | 23 | function englishFirst(sections) { |
@@ -72,11 +75,6 @@ export default { |
72 | 75 | }); |
73 | 76 | if (rest.length) |
74 | 77 | return respond({ response_type: "ephemeral", text: ":red-x: One word at a time, please." }); |
75 | | - if (term.length > 50) |
76 | | - return respond({ |
77 | | - response_type: "ephemeral", |
78 | | - text: ":red-x: One word at a time, please.", |
79 | | - }); |
80 | 78 | if (term.length > 50) |
81 | 79 | return respond({ |
82 | 80 | response_type: "ephemeral", |
|
0 commit comments