@ampless/plugin-x-embed 1.0.0-alpha.6 → 1.0.0-alpha.7

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.
Files changed (2) hide show
  1. package/dist/editor.js +66 -1
  2. package/package.json +2 -2
package/dist/editor.js CHANGED
@@ -6,6 +6,23 @@ import {
6
6
 
7
7
  // src/editor.tsx
8
8
  import { Node, mergeAttributes } from "@tiptap/core";
9
+ function getBareUrlLinkHref(el) {
10
+ if (el.tagName.toLowerCase() === "a") {
11
+ return el.getAttribute("href");
12
+ }
13
+ if (el.tagName.toLowerCase() !== "p") return null;
14
+ const children = Array.from(el.children);
15
+ if (children.length !== 1) return null;
16
+ const link = children[0];
17
+ if (!(link instanceof HTMLElement)) return null;
18
+ if (link.tagName.toLowerCase() !== "a") return null;
19
+ const href = link.getAttribute("href")?.trim();
20
+ if (!href) return null;
21
+ const linkText = link.textContent?.trim();
22
+ if (linkText !== href) return null;
23
+ if (el.textContent?.trim() !== linkText) return null;
24
+ return href;
25
+ }
9
26
  var AmplessTweetNode = Node.create({
10
27
  name: "amplessTweet",
11
28
  group: "block",
@@ -16,7 +33,12 @@ var AmplessTweetNode = Node.create({
16
33
  return {
17
34
  tweetId: {
18
35
  default: "",
19
- parseHTML: (el) => el.getAttribute("data-tweet-id") ?? "",
36
+ parseHTML: (el) => {
37
+ const dataTweetId = el.getAttribute("data-tweet-id");
38
+ if (dataTweetId != null) return dataTweetId;
39
+ const href = getBareUrlLinkHref(el);
40
+ return href ? parseTweetUrl(href) ?? "" : "";
41
+ },
20
42
  renderHTML: (attrs) => ({
21
43
  "data-tweet-id": String(attrs.tweetId ?? "")
22
44
  })
@@ -27,6 +49,49 @@ var AmplessTweetNode = Node.create({
27
49
  return [
28
50
  {
29
51
  tag: "div[data-ampless-tweet]"
52
+ },
53
+ {
54
+ tag: "p",
55
+ priority: 100,
56
+ getAttrs: (el) => {
57
+ const href = getBareUrlLinkHref(el) ?? "";
58
+ const tweetId = parseTweetUrl(href);
59
+ if (!tweetId) return false;
60
+ return { tweetId };
61
+ }
62
+ },
63
+ {
64
+ // Defensive fallback for genuinely top-level bare-URL `<a>` tags
65
+ // (no parent block, or directly under `<body>` — = HTML fragments
66
+ // without a paragraph wrapper at all). The primary case is the
67
+ // markdown bare URL line `<p><a href=URL>URL</a></p>`, which is
68
+ // already handled by the `tag: 'p'` rule above (priority 100,
69
+ // `getBareUrlLinkHref`). This rule covers the rare standalone-link
70
+ // shape that bypasses that rule entirely.
71
+ //
72
+ // Scope is deliberately narrow:
73
+ // 1. Link text equals href — `<a href="URL">caption</a>` stays
74
+ // a captioned Link, not silently swallowed into an embed.
75
+ // 2. Parent is null or `<body>` only. Inside any content block
76
+ // (`<p>`, `<li>`, `<blockquote>`, `<div>`, …) the autolink
77
+ // stays an inline Link mark to preserve the surrounding
78
+ // structure. Promoting it would split `<li>` into an empty
79
+ // paragraph item plus a list-external embed, break
80
+ // blockquotes, etc.
81
+ tag: "a[href]",
82
+ priority: 100,
83
+ getAttrs: (el) => {
84
+ const link = el;
85
+ const parent = link.parentElement;
86
+ if (parent && parent.tagName.toLowerCase() !== "body") return false;
87
+ const href = link.getAttribute("href")?.trim() ?? "";
88
+ if (!href) return false;
89
+ const linkText = link.textContent?.trim() ?? "";
90
+ if (linkText !== href) return false;
91
+ const tweetId = parseTweetUrl(href);
92
+ if (!tweetId) return false;
93
+ return { tweetId };
94
+ }
30
95
  }
31
96
  ];
32
97
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ampless/plugin-x-embed",
3
- "version": "1.0.0-alpha.6",
3
+ "version": "1.0.0-alpha.7",
4
4
  "description": "x.com (Twitter) embed plugin for ampless — renders tweet URLs inline as blockquotes hydrated by widgets.js",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -32,7 +32,7 @@
32
32
  "peerDependencies": {
33
33
  "@tiptap/core": "^3",
34
34
  "react": "^18 || ^19",
35
- "ampless": "1.0.0-alpha.45"
35
+ "ampless": "1.0.0-alpha.46"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@tiptap/core": "^3.23.6",