@mdream/js 1.7.0 → 1.7.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.
@@ -552,7 +552,8 @@ function traverseUpToFirstBlockNode(node) {
552
552
  return parentsToIncrement;
553
553
  }
554
554
  const TRACKING_PARAM_RE = /^(?:utm_|fbclid|gclid|mc_eid|msclkid|oly_)/;
555
- const URL_SCHEME_RE = /^[\dA-Z+.-]+:/i;
555
+ const URL_SCHEME_RE = /^[A-Z][\dA-Z+.-]*:/i;
556
+ const SLASH_CHAR$1 = 47;
556
557
  function stripTrackingParams(url) {
557
558
  const queryStart = url.indexOf("?");
558
559
  const fragmentStart = url.indexOf("#");
@@ -561,15 +562,73 @@ function stripTrackingParams(url) {
561
562
  const query = url.slice(queryStart + 1, queryEnd).split("&").filter((parameter) => !TRACKING_PARAM_RE.test(parameter)).join("&");
562
563
  return `${url.slice(0, queryStart)}${query ? `?${query}` : ""}${url.slice(queryEnd)}`;
563
564
  }
565
+ function pathEnd(value, from) {
566
+ const query = value.indexOf("?", from);
567
+ const fragment = value.indexOf("#", from);
568
+ if (query === -1) return fragment === -1 ? value.length : fragment;
569
+ return fragment === -1 ? query : Math.min(query, fragment);
570
+ }
571
+ function hasDotSegment(path) {
572
+ return path.includes(".") && (path === "." || path === ".." || path.startsWith("./") || path.startsWith("../") || path.includes("/./") || path.includes("/../") || path.endsWith("/.") || path.endsWith("/.."));
573
+ }
574
+ function mergeDotSegments(dir, rest) {
575
+ let out = dir;
576
+ let index = 0;
577
+ for (;;) {
578
+ let end = rest.indexOf("/", index);
579
+ const last = end === -1;
580
+ if (last) end = rest.length;
581
+ const segment = rest.slice(index, end);
582
+ if (segment === "..") out = out.slice(0, out.lastIndexOf("/", out.length - 2) + 1);
583
+ else if (segment !== ".") out += last ? segment : `${segment}/`;
584
+ if (last) return out;
585
+ index = end + 1;
586
+ }
587
+ }
588
+ let baseOrigin;
589
+ let baseRoot = "";
590
+ let basePath = "";
591
+ let baseDir = "";
592
+ function loadBase(origin) {
593
+ if (origin !== baseOrigin) {
594
+ baseOrigin = origin;
595
+ baseRoot = "";
596
+ const authority = origin.indexOf("://");
597
+ if (authority !== -1) {
598
+ const end = pathEnd(origin, authority + 3);
599
+ const slash = origin.indexOf("/", authority + 3);
600
+ const rootEnd = slash === -1 || slash > end ? end : slash;
601
+ baseRoot = origin.slice(0, rootEnd);
602
+ basePath = origin.slice(rootEnd, end);
603
+ baseDir = basePath.slice(0, basePath.lastIndexOf("/") + 1) || "/";
604
+ }
605
+ }
606
+ return baseRoot !== "";
607
+ }
608
+ function resolveAgainstBase(origin, url) {
609
+ if (!loadBase(origin)) {
610
+ let prefix = origin;
611
+ while (prefix.endsWith("/")) prefix = prefix.slice(0, -1);
612
+ const path = url.startsWith("./") ? url.slice(2) : url;
613
+ return `${prefix}${path.charCodeAt(0) === SLASH_CHAR$1 ? "" : "/"}${path}`;
614
+ }
615
+ const end = pathEnd(url, 0);
616
+ const path = url.slice(0, end);
617
+ const suffix = end === url.length ? "" : url.slice(end);
618
+ if (!path) return `${baseRoot}${basePath}${suffix}`;
619
+ const dot = hasDotSegment(path);
620
+ if (path.charCodeAt(0) === SLASH_CHAR$1) return `${baseRoot}${dot ? mergeDotSegments("/", path.slice(1)) : path}${suffix}`;
621
+ return `${baseRoot}${dot ? mergeDotSegments(baseDir, path) : baseDir + path}${suffix}`;
622
+ }
564
623
  function resolveUrl(url, origin, clean) {
565
624
  if (!url || url[0] === "#") return url;
625
+ const isSlash = url.charCodeAt(0) === SLASH_CHAR$1;
566
626
  let resolved = url;
567
- if (url.startsWith("//")) resolved = `https:${url}`;
568
- else if (origin && !URL_SCHEME_RE.test(url)) {
569
- const path = url.startsWith("./") ? url.slice(2) : url;
570
- while (origin.endsWith("/")) origin = origin.slice(0, -1);
571
- resolved = `${origin}${path[0] === "/" ? "" : "/"}${path}`;
572
- }
627
+ if (isSlash && url.charCodeAt(1) === SLASH_CHAR$1) {
628
+ let scheme = "https";
629
+ if (origin && loadBase(origin)) scheme = baseRoot.slice(0, baseRoot.indexOf(":"));
630
+ resolved = `${scheme}:${url}`;
631
+ } else if (origin && (isSlash || !URL_SCHEME_RE.test(url))) resolved = resolveAgainstBase(origin, url);
573
632
  return (clean === true || !!clean && clean.urls === true) && resolved.includes("?") ? stripTrackingParams(resolved) : resolved;
574
633
  }
575
634
  function serializeMarkdownDestination(destination) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.7.0",
4
+ "version": "1.7.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",