@mdream/js 1.1.2 → 1.2.1

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.
@@ -10,6 +10,14 @@ function resolveUrl(url, origin) {
10
10
  }
11
11
  return url;
12
12
  }
13
+ function isAutolinkUri(s) {
14
+ if (!(s.startsWith("http://") || s.startsWith("https://") || s.startsWith("ftp://") || s.startsWith("mailto:"))) return false;
15
+ for (let i = 0; i < s.length; i++) {
16
+ const c = s.charCodeAt(i);
17
+ if (c === 32 || c === 60 || c === 62 || c === 10 || c === 13 || c === 9) return false;
18
+ }
19
+ return true;
20
+ }
13
21
  function isInsideTableCell(node) {
14
22
  return (node.depthMap[32] || 0) > 0 || (node.depthMap[31] || 0) > 0;
15
23
  }
@@ -201,6 +209,24 @@ const tagHandlers = {
201
209
  const href = resolveUrl(node.attributes?.href || "", state.options?.origin);
202
210
  let title = node.attributes?.title;
203
211
  if (state.lastContentCache === title) title = "";
212
+ if (!title && isAutolinkUri(href)) {
213
+ const buf = state.buffer;
214
+ let i = buf.length - 1;
215
+ let textLen = 0;
216
+ while (i >= 0) {
217
+ const entry = buf[i];
218
+ if (entry === "[") break;
219
+ textLen += entry.length;
220
+ i--;
221
+ }
222
+ if (i >= 0 && textLen === href.length && buf.slice(i + 1).join("") === href) {
223
+ buf.length = i;
224
+ const auto = `<${href}>`;
225
+ buf.push(auto);
226
+ state.lastContentCache = auto;
227
+ return "";
228
+ }
229
+ }
204
230
  return title ? `](${href} "${title}")` : `](${href})`;
205
231
  },
206
232
  collapsesInnerWhiteSpace: true,
@@ -164,18 +164,37 @@ function cleanEmptyLinks(md) {
164
164
  }
165
165
  return result;
166
166
  }
167
+ function isAutolinkUri(s) {
168
+ if (!(s.startsWith("http://") || s.startsWith("https://") || s.startsWith("ftp://") || s.startsWith("mailto:"))) return false;
169
+ for (let i = 0; i < s.length; i++) {
170
+ const c = s.charCodeAt(i);
171
+ if (c === 32 || c === 60 || c === 62 || c === 10 || c === 13 || c === 9) return false;
172
+ }
173
+ return true;
174
+ }
167
175
  function cleanRedundantLinks(md) {
168
176
  const len = md.length;
169
177
  let result = "";
170
178
  let i = 0;
171
179
  while (i < len) {
172
- if (md.charCodeAt(i) === 91) {
180
+ const code = md.charCodeAt(i);
181
+ if (code === 91) {
173
182
  const link = parseLink(md, i);
174
183
  if (link && link.text === link.url) {
175
184
  result += link.text;
176
185
  i = link.end;
177
186
  continue;
178
187
  }
188
+ } else if (code === 60) {
189
+ const close = md.indexOf(">", i + 1);
190
+ if (close !== -1) {
191
+ const inner = md.slice(i + 1, close);
192
+ if (isAutolinkUri(inner)) {
193
+ result += inner;
194
+ i = close + 1;
195
+ continue;
196
+ }
197
+ }
179
198
  }
180
199
  result += md[i];
181
200
  i++;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.1.2",
4
+ "version": "1.2.1",
5
5
  "description": "JavaScript HTML-to-Markdown engine for mdream. Escape hatch for hooks and edge runtimes.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",