social-embeds 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,20 +7,24 @@ import { platforms } from "./platforms/index.js";
7
7
  * comments (docs/decisions.md D14).
8
8
  */
9
9
  function parseEmbed(html) {
10
+ return parseEmbedWith(platforms, html);
11
+ }
12
+ /** `parseEmbed` for these platforms only. */
13
+ function parseEmbedWith(parsers, html) {
10
14
  if (typeof html !== "string") return null;
11
15
  const nodes = parseHtml(html);
12
- return nodes ? parseNodes(nodes) : parseRepaired(html);
16
+ return nodes ? parseNodes(parsers, nodes) : parseRepaired(parsers, html);
13
17
  }
14
- function parseRepaired(html) {
15
- for (const platform of platforms) {
18
+ function parseRepaired(parsers, html) {
19
+ for (const platform of parsers) {
16
20
  const repaired = platform.repair?.(html);
17
21
  const nodes = repaired === void 0 || repaired === null ? null : parseHtml(repaired);
18
- const embed = nodes && parseNodes(nodes);
22
+ const embed = nodes && parseNodes(parsers, nodes);
19
23
  if (embed?.platform === platform.platform) return embed;
20
24
  }
21
25
  return null;
22
26
  }
23
- function parseNodes(nodes) {
27
+ function parseNodes(parsers, nodes) {
24
28
  let root;
25
29
  let extra;
26
30
  const scripts = [];
@@ -42,13 +46,13 @@ function parseNodes(nodes) {
42
46
  const [script] = scripts;
43
47
  if (fbRoot || scripts.length !== 1 || !script) return null;
44
48
  if (script.children.length !== 0) return null;
45
- for (const platform of platforms) {
49
+ for (const platform of parsers) {
46
50
  const embed = platform.parseScript?.(script);
47
51
  if (embed) return embed;
48
52
  }
49
53
  return null;
50
54
  }
51
- for (const platform of platforms) {
55
+ for (const platform of parsers) {
52
56
  const embed = platform.parse(root);
53
57
  if (!embed) continue;
54
58
  if (fbRoot && !platform.allowsFbRoot) return null;
@@ -66,4 +70,4 @@ function parseNodes(nodes) {
66
70
  function isFbRoot(element) {
67
71
  return element.tag === "div" && element.attrs.get("id") === "fb-root" && element.attrs.size === 1 && element.children.length === 0;
68
72
  }
69
- export { parseEmbed };
73
+ export { parseEmbed, parseEmbedWith };
@@ -0,0 +1,26 @@
1
+ import { HtmlElement, Whitelist } from "./html.js";
2
+ import { ParsedEmbed, Platform } from "./types.js";
3
+ import { ParsedUrl } from "./url.js";
4
+ export type Options = ParsedEmbed['options'];
5
+ export type PlatformParser = {
6
+ platform: Platform;
7
+ /** Elements and attributes allowed in `fallbackHtml`. */
8
+ fallback?: Whitelist;
9
+ /** The embed element (blockquote, div or iframe) of the platform's own embed code. */
10
+ parse: (root: HtmlElement) => ParsedEmbed | null;
11
+ /** Embed code that is a single `<script>` tag, as Telegram's is. */
12
+ parseScript?: (script: HtmlElement) => ParsedEmbed | null;
13
+ /**
14
+ * For code the HTML parser rejects: the same code without the stray markup this platform's own
15
+ * tool writes, or `null`. The result must still parse as this platform's embed.
16
+ */
17
+ repair?: (html: string) => string | null;
18
+ isOwnScript: (src: string) => boolean;
19
+ scriptOptions?: (src: string) => Options;
20
+ allowsFbRoot?: boolean;
21
+ /** A sibling element after the embed that is the platform's own markup, such as a credit; it is dropped. */
22
+ isOwnExtra?: (element: HtmlElement, embed: ParsedEmbed) => boolean;
23
+ match: (url: ParsedUrl) => ParsedEmbed | null;
24
+ /** True also for frames `parse` rejects, so generic iframe code never bypasses the platform's checks. */
25
+ ownsFrame?: (src: ParsedUrl) => boolean;
26
+ };
@@ -0,0 +1,6 @@
1
+ export type ParsedUrl = {
2
+ host: string;
3
+ path: string;
4
+ query: ReadonlyMap<string, string>;
5
+ fragment?: string;
6
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "social-embeds",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "Turn social media embed code or post URLs into working React components.",
5
5
  "keywords": [
6
6
  "react",