@astrojs/markdown-remark 2.1.2 → 2.1.3

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.
@@ -1,5 +1,5 @@
1
- @astrojs/markdown-remark:build: cache hit, replaying output 56144f20e07435ef
1
+ @astrojs/markdown-remark:build: cache hit, replaying output d62fcf6af591535d
2
2
  @astrojs/markdown-remark:build: 
3
- @astrojs/markdown-remark:build: > @astrojs/markdown-remark@2.1.2 build /home/runner/work/astro/astro/packages/markdown/remark
3
+ @astrojs/markdown-remark:build: > @astrojs/markdown-remark@2.1.3 build /home/runner/work/astro/astro/packages/markdown/remark
4
4
  @astrojs/markdown-remark:build: > astro-scripts build "src/**/*.ts" && tsc -p tsconfig.json
5
5
  @astrojs/markdown-remark:build: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @astrojs/markdown-remark
2
2
 
3
+ ## 2.1.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#6744](https://github.com/withastro/astro/pull/6744) [`a1a4f45b5`](https://github.com/withastro/astro/commit/a1a4f45b51a80215fa7598da83bd0d9c5acd20d2) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Fix remote images in Markdown throwing errors when using `experimental.assets`
8
+
9
+ - Updated dependencies [[`489dd8d69`](https://github.com/withastro/astro/commit/489dd8d69cdd9d7c243cf8bec96051a914984b9c), [`a1a4f45b5`](https://github.com/withastro/astro/commit/a1a4f45b51a80215fa7598da83bd0d9c5acd20d2), [`a1108e037`](https://github.com/withastro/astro/commit/a1108e037115cdb67d03505286c7d3a4fc2a1ff5), [`8b88e4cf1`](https://github.com/withastro/astro/commit/8b88e4cf15c8bea7942b3985380164e0edf7250b), [`d54cbe413`](https://github.com/withastro/astro/commit/d54cbe41349e55f8544212ad9320705f07325920), [`4c347ab51`](https://github.com/withastro/astro/commit/4c347ab51e46f2319d614f8577fe502e3dc816e2), [`ff0430786`](https://github.com/withastro/astro/commit/ff043078630e678348ae4f4757b3015b3b862c16), [`2f2e572e9`](https://github.com/withastro/astro/commit/2f2e572e937fd25451bbc78a05d55b7caa1ca3ec), [`7116c021a`](https://github.com/withastro/astro/commit/7116c021a39eac15a6e1264dfbd11bef0f5d618a)]:
10
+ - astro@2.2.0
11
+
3
12
  ## 2.1.2
4
13
 
5
14
  ### Patch Changes
@@ -2,15 +2,13 @@ import { visit } from "unist-util-visit";
2
2
  function rehypeImages() {
3
3
  return () => function(tree, file) {
4
4
  visit(tree, (node) => {
5
- var _a;
5
+ var _a, _b;
6
6
  if (node.type !== "element")
7
7
  return;
8
8
  if (node.tagName !== "img")
9
9
  return;
10
10
  if ((_a = node.properties) == null ? void 0 : _a.src) {
11
- if (file.dirname) {
12
- if (!isRelativePath(node.properties.src) && !isAliasedPath(node.properties.src))
13
- return;
11
+ if ((_b = file.data.imagePaths) == null ? void 0 : _b.has(node.properties.src)) {
14
12
  node.properties["__ASTRO_IMAGE_"] = node.properties.src;
15
13
  delete node.properties.src;
16
14
  }
@@ -18,23 +16,6 @@ function rehypeImages() {
18
16
  });
19
17
  };
20
18
  }
21
- function isAliasedPath(path) {
22
- return path.startsWith("~/assets");
23
- }
24
- function isRelativePath(path) {
25
- return startsWithDotDotSlash(path) || startsWithDotSlash(path);
26
- }
27
- function startsWithDotDotSlash(path) {
28
- const c1 = path[0];
29
- const c2 = path[1];
30
- const c3 = path[2];
31
- return c1 === "." && c2 === "." && c3 === "/";
32
- }
33
- function startsWithDotSlash(path) {
34
- const c1 = path[0];
35
- const c2 = path[1];
36
- return c1 === "." && c2 === "/";
37
- }
38
19
  export {
39
20
  rehypeImages
40
21
  };
@@ -1,2 +1,2 @@
1
- import type { VFile } from 'vfile';
2
- export default function toRemarkCollectImages(): () => (tree: any, vfile: VFile) => Promise<void>;
1
+ import type { MarkdownVFile } from './types';
2
+ export default function toRemarkCollectImages(): () => (tree: any, vfile: MarkdownVFile) => Promise<void>;
@@ -4,12 +4,24 @@ function toRemarkCollectImages() {
4
4
  if (typeof (vfile == null ? void 0 : vfile.path) !== "string")
5
5
  return;
6
6
  const imagePaths = /* @__PURE__ */ new Set();
7
- visit(tree, "image", function raiseError(node) {
8
- imagePaths.add(node.url);
7
+ visit(tree, "image", (node) => {
8
+ if (shouldOptimizeImage(node.url))
9
+ imagePaths.add(node.url);
9
10
  });
10
- vfile.data.imagePaths = Array.from(imagePaths);
11
+ vfile.data.imagePaths = imagePaths;
11
12
  };
12
13
  }
14
+ function shouldOptimizeImage(src) {
15
+ return !isValidUrl(src) && !src.startsWith("/");
16
+ }
17
+ function isValidUrl(str) {
18
+ try {
19
+ new URL(str);
20
+ return true;
21
+ } catch {
22
+ return false;
23
+ }
24
+ }
13
25
  export {
14
26
  toRemarkCollectImages as default
15
27
  };
package/dist/types.d.ts CHANGED
@@ -61,10 +61,11 @@ export interface MarkdownMetadata {
61
61
  export interface MarkdownVFile extends VFile {
62
62
  data: {
63
63
  __astroHeadings?: MarkdownHeading[];
64
+ imagePaths?: Set<string>;
64
65
  };
65
66
  }
66
67
  export interface MarkdownRenderingResult {
67
68
  metadata: MarkdownMetadata;
68
- vfile: VFile;
69
+ vfile: MarkdownVFile;
69
70
  code: string;
70
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -17,7 +17,7 @@
17
17
  "./dist/internal.js": "./dist/internal.js"
18
18
  },
19
19
  "peerDependencies": {
20
- "astro": "^2.1.5"
20
+ "astro": "^2.2.0"
21
21
  },
22
22
  "dependencies": {
23
23
  "@astrojs/prism": "^2.1.0",
@@ -9,9 +9,7 @@ export function rehypeImages() {
9
9
  if (node.tagName !== 'img') return;
10
10
 
11
11
  if (node.properties?.src) {
12
- if (file.dirname) {
13
- if (!isRelativePath(node.properties.src) && !isAliasedPath(node.properties.src)) return;
14
-
12
+ if (file.data.imagePaths?.has(node.properties.src)) {
15
13
  node.properties['__ASTRO_IMAGE_'] = node.properties.src;
16
14
  delete node.properties.src;
17
15
  }
@@ -19,24 +17,3 @@ export function rehypeImages() {
19
17
  });
20
18
  };
21
19
  }
22
-
23
- function isAliasedPath(path: string) {
24
- return path.startsWith('~/assets');
25
- }
26
-
27
- function isRelativePath(path: string) {
28
- return startsWithDotDotSlash(path) || startsWithDotSlash(path);
29
- }
30
-
31
- function startsWithDotDotSlash(path: string) {
32
- const c1 = path[0];
33
- const c2 = path[1];
34
- const c3 = path[2];
35
- return c1 === '.' && c2 === '.' && c3 === '/';
36
- }
37
-
38
- function startsWithDotSlash(path: string) {
39
- const c1 = path[0];
40
- const c2 = path[1];
41
- return c1 === '.' && c2 === '/';
42
- }
@@ -1,17 +1,31 @@
1
1
  import type { Image } from 'mdast';
2
2
  import { visit } from 'unist-util-visit';
3
- import type { VFile } from 'vfile';
3
+ import type { MarkdownVFile } from './types';
4
4
 
5
5
  export default function toRemarkCollectImages() {
6
6
  return () =>
7
- async function (tree: any, vfile: VFile) {
7
+ async function (tree: any, vfile: MarkdownVFile) {
8
8
  if (typeof vfile?.path !== 'string') return;
9
9
 
10
10
  const imagePaths = new Set<string>();
11
- visit(tree, 'image', function raiseError(node: Image) {
12
- imagePaths.add(node.url);
11
+ visit(tree, 'image', (node: Image) => {
12
+ if (shouldOptimizeImage(node.url)) imagePaths.add(node.url);
13
13
  });
14
14
 
15
- vfile.data.imagePaths = Array.from(imagePaths);
15
+ vfile.data.imagePaths = imagePaths;
16
16
  };
17
17
  }
18
+
19
+ function shouldOptimizeImage(src: string) {
20
+ // Optimize anything that is NOT external or an absolute path to `public/`
21
+ return !isValidUrl(src) && !src.startsWith('/');
22
+ }
23
+
24
+ function isValidUrl(str: string): boolean {
25
+ try {
26
+ new URL(str);
27
+ return true;
28
+ } catch {
29
+ return false;
30
+ }
31
+ }
package/src/types.ts CHANGED
@@ -85,11 +85,12 @@ export interface MarkdownMetadata {
85
85
  export interface MarkdownVFile extends VFile {
86
86
  data: {
87
87
  __astroHeadings?: MarkdownHeading[];
88
+ imagePaths?: Set<string>;
88
89
  };
89
90
  }
90
91
 
91
92
  export interface MarkdownRenderingResult {
92
93
  metadata: MarkdownMetadata;
93
- vfile: VFile;
94
+ vfile: MarkdownVFile;
94
95
  code: string;
95
96
  }