@geekity/cms 0.56.1 → 0.57.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.
Files changed (241) hide show
  1. package/admin/pages/tools/export.njk +6 -0
  2. package/admin/pages/tools/personal-data.njk +9 -0
  3. package/admin/static/admin.css +812 -1406
  4. package/admin/static/editor.js +26 -26
  5. package/dist/admin/federation.d.ts.map +1 -1
  6. package/dist/admin/federation.js +2 -1
  7. package/dist/admin/federation.js.map +1 -1
  8. package/dist/admin/indieauth-consent.js +1 -1
  9. package/dist/admin/indieauth-consent.js.map +1 -1
  10. package/dist/admin/personal-data.d.ts.map +1 -1
  11. package/dist/admin/personal-data.js +11 -2
  12. package/dist/admin/personal-data.js.map +1 -1
  13. package/dist/cli/export.d.ts.map +1 -1
  14. package/dist/cli/export.js +5 -1
  15. package/dist/cli/export.js.map +1 -1
  16. package/dist/cms/services.d.ts.map +1 -1
  17. package/dist/cms/services.js +12 -1
  18. package/dist/cms/services.js.map +1 -1
  19. package/dist/comments/conversation.d.ts +4 -3
  20. package/dist/comments/conversation.d.ts.map +1 -1
  21. package/dist/comments/intake.d.ts +19 -12
  22. package/dist/comments/intake.d.ts.map +1 -1
  23. package/dist/comments/intake.js +46 -16
  24. package/dist/comments/intake.js.map +1 -1
  25. package/dist/comments/migrations.d.ts.map +1 -1
  26. package/dist/comments/migrations.js +16 -2
  27. package/dist/comments/migrations.js.map +1 -1
  28. package/dist/comments/records.d.ts +24 -11
  29. package/dist/comments/records.d.ts.map +1 -1
  30. package/dist/comments/records.js +168 -63
  31. package/dist/comments/records.js.map +1 -1
  32. package/dist/config-entry.d.ts +1 -1
  33. package/dist/config-entry.d.ts.map +1 -1
  34. package/dist/config.d.ts +47 -0
  35. package/dist/config.d.ts.map +1 -1
  36. package/dist/config.js +16 -2
  37. package/dist/config.js.map +1 -1
  38. package/dist/content/image-tags.js +2 -2
  39. package/dist/content/image-tags.js.map +1 -1
  40. package/dist/content/sanitize.d.ts.map +1 -1
  41. package/dist/content/sanitize.js +27 -13
  42. package/dist/content/sanitize.js.map +1 -1
  43. package/dist/content/search.d.ts.map +1 -1
  44. package/dist/content/search.js +1 -28
  45. package/dist/content/search.js.map +1 -1
  46. package/dist/content/sqlite/catalog.js +3 -0
  47. package/dist/content/sqlite/catalog.js.map +1 -1
  48. package/dist/content/sqlite/schema.d.ts.map +1 -1
  49. package/dist/content/sqlite/schema.js +13 -0
  50. package/dist/content/sqlite/schema.js.map +1 -1
  51. package/dist/content/store.d.ts +2 -0
  52. package/dist/content/store.d.ts.map +1 -1
  53. package/dist/content/store.js.map +1 -1
  54. package/dist/export/site-export.d.ts +1 -0
  55. package/dist/export/site-export.d.ts.map +1 -1
  56. package/dist/export/site-export.js +2 -0
  57. package/dist/export/site-export.js.map +1 -1
  58. package/dist/export/site-import.d.ts.map +1 -1
  59. package/dist/export/site-import.js +1 -0
  60. package/dist/export/site-import.js.map +1 -1
  61. package/dist/exports.d.ts +1 -1
  62. package/dist/exports.d.ts.map +1 -1
  63. package/dist/exports.js.map +1 -1
  64. package/dist/federation/federation.d.ts +3 -0
  65. package/dist/federation/federation.d.ts.map +1 -1
  66. package/dist/federation/federation.js +5 -2
  67. package/dist/federation/federation.js.map +1 -1
  68. package/dist/federation/profiles.d.ts.map +1 -1
  69. package/dist/federation/profiles.js +5 -1
  70. package/dist/federation/profiles.js.map +1 -1
  71. package/dist/federation/public-loaders.d.ts +14 -0
  72. package/dist/federation/public-loaders.d.ts.map +1 -0
  73. package/dist/federation/public-loaders.js +41 -0
  74. package/dist/federation/public-loaders.js.map +1 -0
  75. package/dist/files/data-files.d.ts +1 -0
  76. package/dist/files/data-files.d.ts.map +1 -1
  77. package/dist/files/data-files.js +6 -0
  78. package/dist/files/data-files.js.map +1 -1
  79. package/dist/following/list.d.ts.map +1 -1
  80. package/dist/following/list.js +4 -3
  81. package/dist/following/list.js.map +1 -1
  82. package/dist/forms/protection.d.ts +8 -0
  83. package/dist/forms/protection.d.ts.map +1 -1
  84. package/dist/forms/protection.js +8 -0
  85. package/dist/forms/protection.js.map +1 -1
  86. package/dist/indieauth/client.js +2 -2
  87. package/dist/indieauth/client.js.map +1 -1
  88. package/dist/mcp/endpoint.d.ts.map +1 -1
  89. package/dist/mcp/endpoint.js +3 -0
  90. package/dist/mcp/endpoint.js.map +1 -1
  91. package/dist/mcp/inspect/remote.js +2 -2
  92. package/dist/mcp/inspect/remote.js.map +1 -1
  93. package/dist/mcp/profile.d.ts +3 -0
  94. package/dist/mcp/profile.d.ts.map +1 -0
  95. package/dist/mcp/profile.js +143 -0
  96. package/dist/mcp/profile.js.map +1 -0
  97. package/dist/mcp/tools.d.ts +1 -1
  98. package/dist/mcp/tools.d.ts.map +1 -1
  99. package/dist/mcp/tools.js.map +1 -1
  100. package/dist/notifications/comments.d.ts +3 -2
  101. package/dist/notifications/comments.d.ts.map +1 -1
  102. package/dist/notifications/comments.js +10 -7
  103. package/dist/notifications/comments.js.map +1 -1
  104. package/dist/privacy/erase.d.ts +6 -1
  105. package/dist/privacy/erase.d.ts.map +1 -1
  106. package/dist/privacy/erase.js +24 -9
  107. package/dist/privacy/erase.js.map +1 -1
  108. package/dist/privacy/retention.d.ts.map +1 -1
  109. package/dist/privacy/retention.js +7 -2
  110. package/dist/privacy/retention.js.map +1 -1
  111. package/dist/site/users.d.ts +14 -0
  112. package/dist/site/users.d.ts.map +1 -1
  113. package/dist/site/users.js +23 -1
  114. package/dist/site/users.js.map +1 -1
  115. package/dist/store/admin-store.d.ts +1 -1
  116. package/dist/store/admin-store.d.ts.map +1 -1
  117. package/dist/store/admin-store.js +1 -1
  118. package/dist/store/admin-store.js.map +1 -1
  119. package/dist/store/comments.d.ts +14 -0
  120. package/dist/store/comments.d.ts.map +1 -1
  121. package/dist/store/comments.js +40 -2
  122. package/dist/store/comments.js.map +1 -1
  123. package/dist/store/schema.d.ts.map +1 -1
  124. package/dist/store/schema.js +4 -0
  125. package/dist/store/schema.js.map +1 -1
  126. package/dist/util/html-entities.d.ts +5 -0
  127. package/dist/util/html-entities.d.ts.map +1 -0
  128. package/dist/util/html-entities.js +2239 -0
  129. package/dist/util/html-entities.js.map +1 -0
  130. package/dist/util/html-tree.d.ts +13 -6
  131. package/dist/util/html-tree.d.ts.map +1 -1
  132. package/dist/util/html-tree.js +171 -44
  133. package/dist/util/html-tree.js.map +1 -1
  134. package/dist/util/html.d.ts +7 -0
  135. package/dist/util/html.d.ts.map +1 -1
  136. package/dist/util/html.js +68 -19
  137. package/dist/util/html.js.map +1 -1
  138. package/dist/util/public-address.d.ts +11 -2
  139. package/dist/util/public-address.d.ts.map +1 -1
  140. package/dist/util/public-address.js +86 -13
  141. package/dist/util/public-address.js.map +1 -1
  142. package/dist/util/queue.d.ts +5 -0
  143. package/dist/util/queue.d.ts.map +1 -1
  144. package/dist/util/queue.js +18 -0
  145. package/dist/util/queue.js.map +1 -1
  146. package/dist/util/url.d.ts +11 -0
  147. package/dist/util/url.d.ts.map +1 -1
  148. package/dist/util/url.js +23 -0
  149. package/dist/util/url.js.map +1 -1
  150. package/dist/web/activity-folding.d.ts.map +1 -1
  151. package/dist/web/activity-folding.js +7 -3
  152. package/dist/web/activity-folding.js.map +1 -1
  153. package/dist/web/bounded-body.d.ts +12 -0
  154. package/dist/web/bounded-body.d.ts.map +1 -0
  155. package/dist/web/bounded-body.js +55 -0
  156. package/dist/web/bounded-body.js.map +1 -0
  157. package/dist/web/comment-routes.d.ts.map +1 -1
  158. package/dist/web/comment-routes.js +8 -2
  159. package/dist/web/comment-routes.js.map +1 -1
  160. package/dist/web/contact-routes.d.ts.map +1 -1
  161. package/dist/web/contact-routes.js +7 -1
  162. package/dist/web/contact-routes.js.map +1 -1
  163. package/dist/web/conversation.d.ts.map +1 -1
  164. package/dist/web/conversation.js +15 -7
  165. package/dist/web/conversation.js.map +1 -1
  166. package/dist/web/pingback-routes.d.ts +2 -1
  167. package/dist/web/pingback-routes.d.ts.map +1 -1
  168. package/dist/web/pingback-routes.js +59 -34
  169. package/dist/web/pingback-routes.js.map +1 -1
  170. package/dist/web/request.d.ts +0 -5
  171. package/dist/web/request.d.ts.map +1 -1
  172. package/dist/web/request.js +2 -13
  173. package/dist/web/request.js.map +1 -1
  174. package/dist/web/routes.d.ts.map +1 -1
  175. package/dist/web/routes.js +5 -3
  176. package/dist/web/routes.js.map +1 -1
  177. package/dist/web/site-view.d.ts +15 -0
  178. package/dist/web/site-view.d.ts.map +1 -1
  179. package/dist/web/site-view.js +1 -1
  180. package/dist/web/site-view.js.map +1 -1
  181. package/dist/web/throttle.d.ts +6 -0
  182. package/dist/web/throttle.d.ts.map +1 -1
  183. package/dist/web/throttle.js +1 -1
  184. package/dist/web/throttle.js.map +1 -1
  185. package/dist/web/webmention-routes.d.ts +14 -2
  186. package/dist/web/webmention-routes.d.ts.map +1 -1
  187. package/dist/web/webmention-routes.js +65 -19
  188. package/dist/web/webmention-routes.js.map +1 -1
  189. package/dist/webmention/checks.d.ts +43 -0
  190. package/dist/webmention/checks.d.ts.map +1 -0
  191. package/dist/webmention/checks.js +103 -0
  192. package/dist/webmention/checks.js.map +1 -0
  193. package/dist/webmention/discovery.js +2 -2
  194. package/dist/webmention/discovery.js.map +1 -1
  195. package/dist/webmention/endpoint.d.ts +2 -0
  196. package/dist/webmention/endpoint.d.ts.map +1 -1
  197. package/dist/webmention/endpoint.js +2 -0
  198. package/dist/webmention/endpoint.js.map +1 -1
  199. package/dist/webmention/fetch-public.d.ts +5 -0
  200. package/dist/webmention/fetch-public.d.ts.map +1 -1
  201. package/dist/webmention/fetch-public.js +19 -1
  202. package/dist/webmention/fetch-public.js.map +1 -1
  203. package/dist/webmention/index.d.ts +2 -1
  204. package/dist/webmention/index.d.ts.map +1 -1
  205. package/dist/webmention/index.js.map +1 -1
  206. package/dist/webmention/links.d.ts.map +1 -1
  207. package/dist/webmention/links.js +4 -11
  208. package/dist/webmention/links.js.map +1 -1
  209. package/dist/webmention/microformats.d.ts +32 -9
  210. package/dist/webmention/microformats.d.ts.map +1 -1
  211. package/dist/webmention/microformats.js +341 -110
  212. package/dist/webmention/microformats.js.map +1 -1
  213. package/dist/webmention/pingback.d.ts +7 -0
  214. package/dist/webmention/pingback.d.ts.map +1 -1
  215. package/dist/webmention/pingback.js +108 -34
  216. package/dist/webmention/pingback.js.map +1 -1
  217. package/dist/webmention/receive.d.ts +28 -21
  218. package/dist/webmention/receive.d.ts.map +1 -1
  219. package/dist/webmention/receive.js +286 -65
  220. package/dist/webmention/receive.js.map +1 -1
  221. package/dist/webmention/reply-context/feed.js +2 -2
  222. package/dist/webmention/reply-context/feed.js.map +1 -1
  223. package/dist/webmention/reply-context/page.js +2 -2
  224. package/dist/webmention/reply-context/page.js.map +1 -1
  225. package/dist/webmention/service.d.ts +51 -12
  226. package/dist/webmention/service.d.ts.map +1 -1
  227. package/dist/webmention/service.js +202 -38
  228. package/dist/webmention/service.js.map +1 -1
  229. package/dist/webmention/syndication.d.ts +2 -0
  230. package/dist/webmention/syndication.d.ts.map +1 -1
  231. package/dist/webmention/syndication.js +57 -20
  232. package/dist/webmention/syndication.js.map +1 -1
  233. package/package.json +5 -5
  234. package/themes/default/mail/comment-digest.txt.njk +1 -1
  235. package/themes/default/mail/comment-pending.html.njk +20 -0
  236. package/themes/default/mail/comment-pending.txt.njk +31 -4
  237. package/themes/default/mail/comment-reply.txt.njk +1 -1
  238. package/themes/default/mail/contact-message.txt.njk +4 -2
  239. package/themes/default/mail/password-changed.txt.njk +1 -1
  240. package/themes/default/mail/password-reset.txt.njk +1 -1
  241. package/themes/default/mail/test.txt.njk +1 -1
@@ -12,7 +12,24 @@ const KIND_PROPERTIES = {
12
12
  * page is as long as they like, and a thread is rarely more than a few deep.
13
13
  */
14
14
  const NESTED_DEPTH = 8;
15
- const NESTED_LIMIT = 200;
15
+ const NESTED_LIMIT = 20;
16
+ /**
17
+ * How many microformats are read off one page, nested ones counted. A blog's
18
+ * front page carries a few hundred at most; anything past this is passed over.
19
+ */
20
+ export const MICROFORMAT_ITEMS_PER_PAGE = 1_000;
21
+ /**
22
+ * How many characters of one value's text, or of its markup, are kept. A reply
23
+ * is a few paragraphs; a page that sends more is cut short here.
24
+ */
25
+ export const MICROFORMAT_VALUE_LENGTH = 65_536;
26
+ /**
27
+ * How many of an entry's `in-reply-to` values are kept. A reply answers one
28
+ * thing, or a few; each one kept is looked for in the post's conversation.
29
+ */
30
+ const IN_REPLY_TO_PER_ENTRY = 8;
31
+ /** How many property classes are read off one element. */
32
+ const PROPERTIES_PER_ELEMENT = 16;
16
33
  /** The attributes that make an element a link to somewhere. */
17
34
  const LINK_ATTRIBUTES = ['href', 'src', 'data', 'poster', 'cite'];
18
35
  /**
@@ -22,16 +39,22 @@ const LINK_ATTRIBUTES = ['href', 'src', 'data', 'poster', 'cite'];
22
39
  * text: a page that merely writes the target's address out is not talking to
23
40
  * it, and one that links to it in an `<img>`, a `<video>` or a `<link>` is.
24
41
  * The comparison is on the whole URL after both sides are resolved and
25
- * normalised, so a link written relative counts and a link to a different page
26
- * on the same site does not.
42
+ * normalised, less any fragment, so a link written relative or to an anchor on
43
+ * the page counts and a link to a different page on the same site does not.
27
44
  */
28
- export function linksTo(html, sourceUrl, target) {
29
- const root = parseHtml(html);
30
- const base = baseOf(root, sourceUrl);
31
- const wanted = normalize(target, target);
32
- if (wanted === undefined)
45
+ export function linksTo(page, target) {
46
+ const resolved = normalize(target, target);
47
+ if (resolved === undefined)
33
48
  return false;
34
- return linkedFrom(root, base).has(wanted);
49
+ return linkedFrom(page.root, page.base).has(withoutFragment(resolved));
50
+ }
51
+ /** Every URL a source page links to, resolved and normalised as {@link linksTo} reads them. */
52
+ export function linksIn(page) {
53
+ return linkedFrom(page.root, page.base);
54
+ }
55
+ export function readSourcePage(html, sourceUrl) {
56
+ const root = parseHtml(html);
57
+ return { root, url: sourceUrl, base: baseOf(root, sourceUrl) };
35
58
  }
36
59
  /** Every URL an element and everything under it links to, normalised. */
37
60
  function linkedFrom(root, base) {
@@ -43,7 +66,7 @@ function linkedFrom(root, base) {
43
66
  continue;
44
67
  const resolved = normalize(value, base);
45
68
  if (resolved !== undefined)
46
- found.add(resolved);
69
+ found.add(withoutFragment(resolved));
47
70
  }
48
71
  }
49
72
  return found;
@@ -51,22 +74,23 @@ function linkedFrom(root, base) {
51
74
  /**
52
75
  * What a source page is, read out of its microformats.
53
76
  *
54
- * The entry chosen is the one that is actually about the target: the first
55
- * `h-entry` whose `in-reply-to`, `like-of` or `repost-of` names it, else the
56
- * first that links to it at all, else the first on the page. A page with no
57
- * `h-entry` is still a mention — plenty of pages have no microformats and
58
- * still link — and it is described from its `<title>` and its host.
77
+ * `isTarget` says whether a URL is the page being mentioned, which may answer
78
+ * to more than one URL. The entry chosen is the one that is actually about
79
+ * it: the first `h-entry` whose `in-reply-to`, `like-of` or `repost-of` names
80
+ * it, else the first that links to it at all, else the first on the page. A
81
+ * page with no `h-entry` is still a mention — plenty of pages have no
82
+ * microformats and still link — and it is described from its `<title>` and
83
+ * its host.
59
84
  */
60
- export function sourceEntry(html, sourceUrl, target) {
61
- const root = parseHtml(html);
62
- const base = baseOf(root, sourceUrl);
85
+ export function sourceEntry(page, isTarget, now) {
86
+ const { root, base, url: sourceUrl } = page;
63
87
  const items = itemsIn(root, base);
64
88
  const entries = itemsOfType(items, 'h-entry');
65
- const wanted = normalize(target, target);
66
- const chosen = chooseEntry(entries, wanted, base);
67
- const author = authorOf(chosen, items, sourceUrl);
89
+ const named = (url) => isTarget(withoutFragment(url));
90
+ const chosen = chooseEntry(root, entries, named, base);
91
+ const author = authorOf(chosen, items, root, sourceUrl, base);
68
92
  const content = contentOf(chosen, root);
69
- const kind = chosen === undefined ? 'mention' : kindOf(chosen, wanted);
93
+ const kind = chosen === undefined ? 'mention' : kindOf(chosen, named);
70
94
  const url = (chosen === undefined ? undefined : first(chosen, 'url')?.text) ?? sourceUrl;
71
95
  const rsvp = chosen === undefined || kind !== 'reply' ? undefined : rsvpValue(first(chosen, 'rsvp')?.text);
72
96
  return {
@@ -74,16 +98,18 @@ export function sourceEntry(html, sourceUrl, target) {
74
98
  ...(rsvp === undefined ? {} : { rsvp }),
75
99
  author,
76
100
  content,
77
- published: chosen === undefined ? null : instantOf(first(chosen, 'published')),
101
+ published: chosen === undefined ? null : instantOf(first(chosen, 'published'), now),
78
102
  url,
79
- replies: chosen === undefined ? [] : nestedReplies(chosen, url, sourceUrl),
103
+ replies: chosen === undefined ? [] : nestedReplies(chosen, url, sourceUrl, now),
80
104
  inReplyTo: chosen === undefined
81
105
  ? []
82
106
  : (chosen.properties['in-reply-to'] ?? [])
83
107
  .map((value) => value.item === undefined
84
108
  ? value.text
85
109
  : (first(value.item, 'url')?.text ?? value.text))
86
- .filter((url) => url !== ''),
110
+ .filter((url) => url !== '')
111
+ .filter((url, index, urls) => urls.indexOf(url) === index)
112
+ .slice(0, IN_REPLY_TO_PER_ENTRY),
87
113
  };
88
114
  }
89
115
  /**
@@ -92,8 +118,9 @@ export function sourceEntry(html, sourceUrl, target) {
92
118
  * way in turn. A reply is known by its page, so one with no `u-url`, or one
93
119
  * already read on this page, is passed over.
94
120
  */
95
- function nestedReplies(entry, url, sourceUrl) {
121
+ function nestedReplies(entry, url, sourceUrl, now) {
96
122
  const seen = new Set([url, sourceUrl]);
123
+ let replied = 0;
97
124
  const read = (item, itsUrl, depth) => {
98
125
  if (depth >= NESTED_DEPTH)
99
126
  return [];
@@ -106,8 +133,9 @@ function nestedReplies(entry, url, sourceUrl) {
106
133
  const replies = [];
107
134
  for (const reply of carried) {
108
135
  const at = first(reply, 'url')?.text;
109
- if (at === undefined || !isWebUrl(at) || seen.has(at) || seen.size > NESTED_LIMIT)
136
+ if (at === undefined || !isWebUrl(at) || seen.has(at) || replied >= NESTED_LIMIT)
110
137
  continue;
138
+ replied += 1;
111
139
  seen.add(at);
112
140
  const author = first(reply, 'author');
113
141
  replies.push({
@@ -116,7 +144,7 @@ function nestedReplies(entry, url, sourceUrl) {
116
144
  ? cardOf(author.item)
117
145
  : { name: author?.text ?? '', url: null, photo: null },
118
146
  content: nestedContent(reply),
119
- published: instantOf(first(reply, 'published')),
147
+ published: instantOf(first(reply, 'published'), now),
120
148
  replies: read(reply, at, depth + 1),
121
149
  });
122
150
  }
@@ -183,30 +211,67 @@ function valuesOf(item, property) {
183
211
  return (item.properties[property] ?? []).map((value) => value.text).filter((text) => text !== '');
184
212
  }
185
213
  /** The entry a webmention is about, or `undefined` when the page has none. */
186
- function chooseEntry(entries, target, base) {
187
- if (target !== undefined) {
188
- for (const entry of entries) {
189
- if (kindOf(entry, target) !== 'mention')
190
- return entry;
214
+ function chooseEntry(root, entries, isTarget, base) {
215
+ for (const entry of entries) {
216
+ if (kindOf(entry, isTarget) !== 'mention')
217
+ return entry;
218
+ }
219
+ const linking = entriesLinkingTo(root, entries, isTarget, base);
220
+ return entries.find((entry) => linking.has(entry.element)) ?? entries[0];
221
+ }
222
+ /**
223
+ * The elements of those entries that have a link to the target somewhere
224
+ * inside them, found in one walk of the page however deeply they nest.
225
+ */
226
+ function entriesLinkingTo(root, entries, isTarget, base) {
227
+ const isEntry = new Set(entries.map((entry) => entry.element));
228
+ const linking = new Set();
229
+ const open = [];
230
+ const pending = [{ element: root, leaving: false }];
231
+ for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
232
+ const { element, leaving } = next;
233
+ if (leaving) {
234
+ open.pop();
235
+ continue;
191
236
  }
192
- for (const entry of entries) {
193
- if (linkedFrom(entry.element, base).has(target))
194
- return entry;
237
+ if (element !== root && linksToTarget(element, isTarget, base)) {
238
+ for (let index = open.length - 1; index >= 0; index -= 1) {
239
+ const enclosing = open[index];
240
+ if (linking.has(enclosing))
241
+ break;
242
+ linking.add(enclosing);
243
+ }
244
+ }
245
+ if (isEntry.has(element)) {
246
+ open.push(element);
247
+ pending.push({ element, leaving: true });
248
+ }
249
+ for (let index = element.children.length - 1; index >= 0; index -= 1) {
250
+ const child = element.children[index];
251
+ if (child !== undefined && isElement(child))
252
+ pending.push({ element: child, leaving: false });
195
253
  }
196
254
  }
197
- return entries[0];
255
+ return linking;
256
+ }
257
+ /** Whether one element, itself and not what is inside it, links to the target. */
258
+ function linksToTarget(element, isTarget, base) {
259
+ return LINK_ATTRIBUTES.some((attribute) => {
260
+ const value = element.attributes[attribute];
261
+ const resolved = value === undefined ? undefined : normalize(value, base);
262
+ return resolved !== undefined && isTarget(resolved);
263
+ });
198
264
  }
199
265
  /** Which of the four an entry is, given what the target is. */
200
- function kindOf(entry, target) {
201
- if (target === undefined)
202
- return 'mention';
266
+ function kindOf(entry, isTarget) {
203
267
  for (const [property, kind] of Object.entries(KIND_PROPERTIES)) {
204
268
  for (const value of entry.properties[property] ?? []) {
205
- if (value.text === target)
269
+ if (isTarget(value.text))
206
270
  return kind;
207
271
  // A property written as a nested `h-cite` names the target with its own
208
272
  // `url` rather than with the value the property carries.
209
- if (value.item !== undefined && first(value.item, 'url')?.text === target)
273
+ const cited = value.item === undefined ? undefined : first(value.item, 'url')?.text;
274
+ if (cited !== undefined && isTarget(cited))
210
275
  return kind;
211
276
  }
212
277
  }
@@ -215,18 +280,32 @@ function kindOf(entry, target) {
215
280
  /**
216
281
  * Who wrote an entry.
217
282
  *
218
- * The entry's own `p-author` first, then a representative `h-card` on the page,
219
- * then the source's host — which is not a name but is the truest thing the site
220
- * knows, and is better on a page than an empty byline.
283
+ * The entry's own `p-author` first, then the `p-author` of the `h-feed` that
284
+ * holds it, then the page's representative `h-card`, then the source's host —
285
+ * which is not a name but is the truest thing the site knows, and is better on
286
+ * a page than an empty byline.
221
287
  */
222
- function authorOf(entry, items, sourceUrl) {
223
- const named = entry === undefined ? undefined : first(entry, 'author');
224
- if (named?.item !== undefined)
225
- return cardOf(named.item);
226
- if (named !== undefined && named.text !== '') {
227
- return { name: named.text, url: null, photo: null };
288
+ function authorOf(entry, items, root, sourceUrl, base) {
289
+ const feed = entry === undefined
290
+ ? undefined
291
+ : items.find((item) => item.types.includes('h-feed') && item.children.includes(entry));
292
+ for (const holder of [entry, feed]) {
293
+ const named = holder === undefined ? undefined : first(holder, 'author');
294
+ if (named?.item !== undefined)
295
+ return cardOf(named.item);
296
+ if (named !== undefined && isWebUrl(named.text)) {
297
+ const label = named.label ?? '';
298
+ return {
299
+ name: label === '' || isWebUrl(label) ? hostOf(named.text) : label,
300
+ url: named.text,
301
+ photo: null,
302
+ };
303
+ }
304
+ if (named !== undefined && named.text !== '') {
305
+ return { name: named.text, url: null, photo: null };
306
+ }
228
307
  }
229
- const card = itemsOfType(items, 'h-card')[0];
308
+ const card = representativeCard(items, root, sourceUrl, base);
230
309
  if (card !== undefined) {
231
310
  const read = cardOf(card);
232
311
  if (read.name !== '')
@@ -234,6 +313,32 @@ function authorOf(entry, items, sourceUrl) {
234
313
  }
235
314
  return { name: hostOf(sourceUrl), url: null, photo: null };
236
315
  }
316
+ /**
317
+ * The `h-card` that stands for the page's owner: one at the top of the page,
318
+ * not inside an entry or a citation, whose `u-url` is the page itself or a
319
+ * page it links `rel=me`.
320
+ */
321
+ function representativeCard(items, root, sourceUrl, base) {
322
+ const page = normalize(sourceUrl, sourceUrl);
323
+ const me = relMeOf(root, base);
324
+ return items.find((item) => item.types.includes('h-card') &&
325
+ (item.properties['url'] ?? []).some((url) => url.text === page || me.has(url.text)));
326
+ }
327
+ /** The pages a page links `rel=me`, resolved. */
328
+ function relMeOf(root, base) {
329
+ const me = new Set();
330
+ for (const element of elementsIn(root)) {
331
+ if (element.name !== 'a' && element.name !== 'link' && element.name !== 'area')
332
+ continue;
333
+ const rels = (element.attributes['rel'] ?? '').toLowerCase().split(/\s+/);
334
+ if (!rels.includes('me'))
335
+ continue;
336
+ const href = normalize(element.attributes['href'] ?? '', base);
337
+ if (href !== undefined)
338
+ me.add(href);
339
+ }
340
+ return me;
341
+ }
237
342
  /** One `h-card` as a name, a page and an avatar. */
238
343
  function cardOf(card) {
239
344
  return {
@@ -261,12 +366,48 @@ function titleOf(root) {
261
366
  }
262
367
  return '';
263
368
  }
264
- /** A `dt-published` as an ISO instant, or `null` when it is not a date. */
265
- function instantOf(value) {
266
- if (value === undefined || value.text === '')
369
+ const DATETIME = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,9}))?)?(Z|[+-]\d{2}:?\d{2})?)?$/i;
370
+ const EARLIEST_PUBLISHED = Date.UTC(1990, 0, 1);
371
+ const PUBLISHED_CLOCK_SKEW_MS = 24 * 60 * 60 * 1000;
372
+ /**
373
+ * A `dt-published` as an ISO instant, or `null` when it is not a date, is
374
+ * older than the web, or is more than a day after `now`. A time with no
375
+ * offset is UTC.
376
+ */
377
+ function instantOf(value, now) {
378
+ const match = DATETIME.exec(value?.text.trim() ?? '');
379
+ if (match === null)
380
+ return null;
381
+ const [, year = '', month = '', day = '', hour = '0', minute = '0', second = '0', fraction = '', zone,] = match;
382
+ const y = Number(year);
383
+ const mo = Number(month) - 1;
384
+ const d = Number(day);
385
+ const h = Number(hour);
386
+ const mi = Number(minute);
387
+ const s = Number(second);
388
+ const local = Date.UTC(y, mo, d, h, mi, s, Number(fraction.padEnd(3, '0').slice(0, 3)));
389
+ const date = new Date(local);
390
+ const sameDay = date.getUTCFullYear() === y && date.getUTCMonth() === mo && date.getUTCDate() === d;
391
+ if (!sameDay || h > 23 || mi > 59 || s > 59)
392
+ return null;
393
+ const offset = offsetMinutes(zone);
394
+ if (offset === null)
395
+ return null;
396
+ const at = local - offset * 60 * 1000;
397
+ if (at < EARLIEST_PUBLISHED)
398
+ return null;
399
+ if (now !== undefined && at > now.getTime() + PUBLISHED_CLOCK_SKEW_MS)
400
+ return null;
401
+ return new Date(at).toISOString();
402
+ }
403
+ function offsetMinutes(zone) {
404
+ if (zone === undefined || zone.toUpperCase() === 'Z')
405
+ return 0;
406
+ const hours = Number(zone.slice(1, 3));
407
+ const minutes = Number(zone.slice(-2));
408
+ if (hours > 23 || minutes > 59)
267
409
  return null;
268
- const at = new Date(value.text);
269
- return Number.isNaN(at.getTime()) ? null : at.toISOString();
410
+ return (zone.startsWith('-') ? -1 : 1) * (hours * 60 + minutes);
270
411
  }
271
412
  /** The host a source is on, for a page that never says who wrote it. */
272
413
  function hostOf(sourceUrl) {
@@ -281,18 +422,26 @@ function hostOf(sourceUrl) {
281
422
  function first(item, property) {
282
423
  return item.properties[property]?.[0];
283
424
  }
284
- /** Every item of one type, at any depth. */
425
+ /**
426
+ * Every item of one type, at any depth, in document order. One nested item can
427
+ * be the value of several properties at once, and it is listed once.
428
+ */
285
429
  function itemsOfType(items, type) {
286
430
  const found = [];
287
- for (const item of items) {
431
+ const seen = new Set();
432
+ const pending = [...items].reverse();
433
+ for (let item = pending.pop(); item !== undefined; item = pending.pop()) {
434
+ if (seen.has(item))
435
+ continue;
436
+ seen.add(item);
288
437
  if (item.types.includes(type))
289
438
  found.push(item);
290
- found.push(...itemsOfType(item.children, type));
291
- for (const values of Object.values(item.properties)) {
292
- for (const value of values) {
293
- if (value.item !== undefined)
294
- found.push(...itemsOfType([value.item], type));
295
- }
439
+ const inside = [
440
+ ...item.children,
441
+ ...Object.values(item.properties).flatMap((values) => values.flatMap((value) => (value.item === undefined ? [] : [value.item]))),
442
+ ];
443
+ for (let index = inside.length - 1; index >= 0; index -= 1) {
444
+ pending.push(inside[index]);
296
445
  }
297
446
  }
298
447
  return found;
@@ -300,14 +449,19 @@ function itemsOfType(items, type) {
300
449
  /** The microformats at the top of a page: roots that are inside no other root. */
301
450
  export function itemsIn(root, base) {
302
451
  const items = [];
452
+ const reading = {
453
+ left: MICROFORMAT_ITEMS_PER_PAGE,
454
+ texts: new Map(),
455
+ markup: new Map(),
456
+ };
303
457
  const walk = (element) => {
304
458
  for (const child of element.children) {
305
459
  if (!isElement(child))
306
460
  continue;
307
- if (rootTypesOf(child).length > 0)
308
- items.push(parseItem(child, base));
309
- else
461
+ if (rootTypesOf(child).length === 0)
310
462
  walk(child);
463
+ else if (reading.left > 0)
464
+ items.push(parseItem(child, base, reading));
311
465
  }
312
466
  };
313
467
  walk(root);
@@ -317,16 +471,42 @@ export function itemsIn(root, base) {
317
471
  function rootTypesOf(element) {
318
472
  return classesOf(element).filter((name) => name.startsWith('h-') && name.length > 2);
319
473
  }
320
- /** The property classes an element carries, as `{prefix, name}` pairs. */
474
+ /**
475
+ * The property classes an element carries, as `{prefix, name}` pairs, each
476
+ * once and no more than {@link PROPERTIES_PER_ELEMENT} of them.
477
+ */
321
478
  function propertiesOf(element) {
322
479
  const found = [];
480
+ const seen = new Set();
323
481
  for (const value of classesOf(element)) {
482
+ if (found.length >= PROPERTIES_PER_ELEMENT)
483
+ break;
324
484
  const match = /^(p|u|e|dt)-(.+)$/.exec(value);
325
- if (match !== null)
326
- found.push({ prefix: match[1] ?? '', name: match[2] ?? '' });
485
+ if (match === null || seen.has(value))
486
+ continue;
487
+ seen.add(value);
488
+ found.push({ prefix: match[1] ?? '', name: match[2] ?? '' });
327
489
  }
328
490
  return found;
329
491
  }
492
+ /** An element's text, read once per page and no longer than a value is kept. */
493
+ function textIn(element, reading) {
494
+ let text = reading.texts.get(element);
495
+ if (text === undefined) {
496
+ text = textOf(element, MICROFORMAT_VALUE_LENGTH);
497
+ reading.texts.set(element, text);
498
+ }
499
+ return text;
500
+ }
501
+ /** An element's inner markup, read once per page and no longer than a value is kept. */
502
+ function markupIn(element, base, reading) {
503
+ let markup = reading.markup.get(element);
504
+ if (markup === undefined) {
505
+ markup = innerHtmlOf(element, MICROFORMAT_VALUE_LENGTH, base);
506
+ reading.markup.set(element, markup);
507
+ }
508
+ return markup;
509
+ }
330
510
  /**
331
511
  * One microformat, with the properties written inside it.
332
512
  *
@@ -335,14 +515,20 @@ function propertiesOf(element) {
335
515
  * entry's. That one rule is what keeps an author's name out of the entry's
336
516
  * title.
337
517
  */
338
- function parseItem(element, base) {
339
- const properties = {};
518
+ function parseItem(element, base, reading) {
519
+ reading.left -= 1;
520
+ const found = new Map();
340
521
  const children = [];
341
522
  // Whether anything inside is a `p-` or `e-` property or a microformat of its
342
523
  // own, which is when mf2 stops implying a name from the whole text.
343
524
  let textual = false;
525
+ let explicitUrl = false;
344
526
  const add = (name, value) => {
345
- (properties[name] ??= []).push(value);
527
+ const values = found.get(name);
528
+ if (values === undefined)
529
+ found.set(name, [value]);
530
+ else
531
+ values.push(value);
346
532
  };
347
533
  const walk = (parent) => {
348
534
  for (const child of parent.children) {
@@ -352,12 +538,16 @@ function parseItem(element, base) {
352
538
  const named = propertiesOf(child);
353
539
  if (roots.length > 0) {
354
540
  textual = true;
355
- const nested = parseItem(child, base);
541
+ if (reading.left <= 0)
542
+ continue;
543
+ const nested = parseItem(child, base, reading);
356
544
  if (named.length === 0)
357
545
  children.push(nested);
358
546
  else {
359
547
  for (const { prefix, name } of named) {
360
- add(name, { text: nestedText(prefix, nested, child, base), item: nested });
548
+ if (prefix === 'u')
549
+ explicitUrl = true;
550
+ add(name, { text: nestedText(prefix, nested, child, base, reading), item: nested });
361
551
  }
362
552
  }
363
553
  continue;
@@ -365,23 +555,26 @@ function parseItem(element, base) {
365
555
  for (const { prefix, name } of named) {
366
556
  if (prefix === 'p' || prefix === 'e')
367
557
  textual = true;
368
- add(name, valueOf(prefix, child, base));
558
+ if (prefix === 'u')
559
+ explicitUrl = true;
560
+ add(name, valueOf(prefix, child, base, reading));
369
561
  }
370
562
  walk(child);
371
563
  }
372
564
  };
373
565
  walk(element);
374
- implied(element, properties, base, !textual);
566
+ const properties = Object.fromEntries(found);
567
+ implied(element, properties, base, reading, !textual, !explicitUrl);
375
568
  return { types: rootTypesOf(element), properties, children, element };
376
569
  }
377
570
  /** What a nested microformat is worth as a value of the property holding it. */
378
- function nestedText(prefix, item, element, base) {
571
+ function nestedText(prefix, item, element, base, reading) {
379
572
  if (prefix === 'u')
380
- return first(item, 'url')?.text ?? valueOf('u', element, base).text;
381
- return first(item, 'name')?.text ?? textOf(element);
573
+ return first(item, 'url')?.text ?? valueOf('u', element, base, reading).text;
574
+ return first(item, 'name')?.text ?? textIn(element, reading);
382
575
  }
383
576
  /** One element as the value of one property. */
384
- function valueOf(prefix, element, base) {
577
+ function valueOf(prefix, element, base, reading) {
385
578
  const attributes = element.attributes;
386
579
  if (prefix === 'u') {
387
580
  const raw = attributeFor(element, ['a', 'area', 'link'], 'href') ??
@@ -389,25 +582,26 @@ function valueOf(prefix, element, base) {
389
582
  attributeFor(element, ['object'], 'data') ??
390
583
  attributeFor(element, ['video'], 'poster') ??
391
584
  attributeFor(element, ['data'], 'value') ??
392
- textOf(element);
393
- return { text: normalize(raw, base) ?? raw.trim() };
585
+ textIn(element, reading);
586
+ return { text: normalize(raw, base) ?? raw.trim(), label: textIn(element, reading) };
394
587
  }
395
588
  if (prefix === 'dt') {
396
589
  return {
397
590
  text: attributeFor(element, ['time', 'ins', 'del'], 'datetime') ??
398
591
  attributeFor(element, ['abbr'], 'title') ??
399
592
  attributeFor(element, ['data'], 'value') ??
400
- textOf(element),
593
+ textIn(element, reading),
401
594
  };
402
595
  }
403
- if (prefix === 'e')
404
- return { text: textOf(element), html: innerHtmlOf(element) };
596
+ if (prefix === 'e') {
597
+ return { text: textIn(element, reading), html: markupIn(element, base, reading) };
598
+ }
405
599
  return {
406
600
  text: attributeFor(element, ['img', 'area'], 'alt') ??
407
601
  attributeFor(element, ['abbr'], 'title') ??
408
602
  attributeFor(element, ['data', 'input'], 'value') ??
409
603
  (element.name === 'meta' ? attributes['content'] : undefined) ??
410
- textOf(element),
604
+ textIn(element, reading),
411
605
  };
412
606
  }
413
607
  /** One attribute, when the element is one of these and carries it. */
@@ -426,52 +620,85 @@ function attributeFor(element, names, attribute) {
426
620
  * actually arrives in and stops short of guessing. A name is implied only when
427
621
  * nothing inside is a `p-` or `e-` property or a nested microformat, as mf2
428
622
  * says, so an entry with an `e-content` and no `p-name` has no name rather
429
- * than the text of everything around it.
623
+ * than the text of everything around it. A url is implied from a link inside
624
+ * only when nothing inside is a `u-` property, so a reply's `u-in-reply-to` is
625
+ * not read as the page the reply lives at.
430
626
  */
431
- function implied(element, properties, base, impliesName) {
627
+ function implied(element, properties, base, reading, impliesName, impliesChildUrl) {
432
628
  if (impliesName && properties['name'] === undefined) {
433
629
  const name = attributeFor(element, ['img', 'area'], 'alt') ??
434
630
  attributeFor(element, ['abbr'], 'title') ??
435
- textOf(element);
631
+ impliedChildName(element) ??
632
+ textIn(element, reading);
436
633
  if (name !== '')
437
634
  properties['name'] = [{ text: name }];
438
635
  }
439
636
  if (properties['url'] === undefined) {
440
637
  const href = attributeFor(element, ['a', 'area', 'link'], 'href') ??
441
- onlyAttribute(element, ['a', 'area', 'link'], 'href');
638
+ (impliesChildUrl ? impliedAttribute(element, ['a', 'area'], 'href') : undefined);
442
639
  const url = href === undefined ? undefined : normalize(href, base);
443
640
  if (url !== undefined)
444
641
  properties['url'] = [{ text: url }];
445
642
  }
446
643
  if (properties['photo'] === undefined) {
447
- const src = attributeFor(element, ['img'], 'src') ?? onlyAttribute(element, ['img'], 'src');
644
+ const src = attributeFor(element, ['img'], 'src') ?? impliedAttribute(element, ['img'], 'src');
448
645
  const photo = src === undefined ? undefined : normalize(src, base);
449
646
  if (photo !== undefined)
450
647
  properties['photo'] = [{ text: photo }];
451
648
  }
452
649
  }
453
650
  /**
454
- * The attribute of the one such element inside this one, when there is exactly
455
- * one and it is not a microformat of its own.
651
+ * The name an item implies from what is directly inside it, as mf2 reads it:
652
+ * the alt of its only child `img` or `area`, or the title of an only `abbr`,
653
+ * else the same of its only child's only child. An empty one does not count,
654
+ * so a card that is an avatar with a blank alt still reads its text.
655
+ */
656
+ function impliedChildName(element) {
657
+ const child = onlyChild(element);
658
+ const grandchild = child === undefined ? undefined : onlyChild(child);
659
+ for (const sole of [child, grandchild]) {
660
+ if (sole === undefined)
661
+ continue;
662
+ const name = (attributeFor(sole, ['img', 'area'], 'alt') ?? attributeFor(sole, ['abbr'], 'title'))?.trim();
663
+ if (name !== undefined && name !== '')
664
+ return name;
665
+ }
666
+ return undefined;
667
+ }
668
+ /**
669
+ * The attribute an item implies from what is directly inside it, as mf2 reads
670
+ * an implied `url` or `photo`: its only child of one of these types, else the
671
+ * only child of that type of its only child element. Nothing that is a
672
+ * microformat of its own counts, and nothing deeper is looked at.
456
673
  *
457
- * Exactly one, because two links inside a card give no reason to prefer
674
+ * Only one of a type, because two links inside a card give no reason to prefer
458
675
  * either, and a guess in a byline is worse than an empty one.
459
676
  */
460
- function onlyAttribute(element, names, attribute) {
461
- let found;
462
- for (const candidate of elementsIn(element)) {
463
- if (!names.includes(candidate.name))
464
- continue;
465
- if (rootTypesOf(candidate).length > 0)
466
- continue;
467
- const value = candidate.attributes[attribute];
468
- if (value === undefined)
469
- continue;
470
- if (found !== undefined)
471
- return undefined;
472
- found = value;
677
+ function impliedAttribute(element, names, attribute) {
678
+ const sole = onlyChild(element);
679
+ for (const parent of sole === undefined ? [element] : [element, sole]) {
680
+ for (const name of names) {
681
+ const value = onlyOfType(parent, name)?.attributes[attribute];
682
+ if (value !== undefined)
683
+ return value;
684
+ }
473
685
  }
474
- return found;
686
+ return undefined;
687
+ }
688
+ /** The one child element of this one, when it has exactly one and it is not a microformat. */
689
+ function onlyChild(element) {
690
+ return soleOf(element.children.filter(isElement));
691
+ }
692
+ /** The one child element of this name, when there is exactly one and it is not a microformat. */
693
+ function onlyOfType(element, name) {
694
+ return soleOf(element.children.filter((child) => isElement(child) && child.name === name));
695
+ }
696
+ /** The only element of these, unless there are more or it is a microformat. */
697
+ function soleOf(elements) {
698
+ const [only] = elements;
699
+ return elements.length === 1 && only !== undefined && rootTypesOf(only).length === 0
700
+ ? only
701
+ : undefined;
475
702
  }
476
703
  /** The `<base href>` a page declares, or the URL it was fetched from. */
477
704
  function baseOf(root, sourceUrl) {
@@ -487,6 +714,10 @@ function baseOf(root, sourceUrl) {
487
714
  }
488
715
  return sourceUrl;
489
716
  }
717
+ function withoutFragment(url) {
718
+ const hash = url.indexOf('#');
719
+ return hash === -1 ? url : url.slice(0, hash);
720
+ }
490
721
  /** One href as an absolute URL, or `undefined` when it is not one. */
491
722
  function normalize(href, base) {
492
723
  const trimmed = href.trim();