@astrojs/markdown-remark 2.1.2 → 2.1.4

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 8b8e166000163d0b
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.4 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,23 @@
1
1
  # @astrojs/markdown-remark
2
2
 
3
+ ## 2.1.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#6824](https://github.com/withastro/astro/pull/6824) [`2511d58d5`](https://github.com/withastro/astro/commit/2511d58d586af080a78e5ef8a63020b3e17770db) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Add support for using optimized and relative images in MDX files with `experimental.assets`
8
+
9
+ - Updated dependencies [[`8539eb164`](https://github.com/withastro/astro/commit/8539eb1643864ae7e0f5a080915cd75535f7101b), [`a9c22994e`](https://github.com/withastro/astro/commit/a9c22994e41f92a586d8946988d29e3c62148778), [`948a6d7be`](https://github.com/withastro/astro/commit/948a6d7be0c76fd1dd8550270bd29821075f799c)]:
10
+ - astro@2.3.0
11
+
12
+ ## 2.1.3
13
+
14
+ ### Patch Changes
15
+
16
+ - [#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`
17
+
18
+ - 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)]:
19
+ - astro@2.2.0
20
+
3
21
  ## 2.1.2
4
22
 
5
23
  ### Patch Changes
@@ -1,4 +1,4 @@
1
- import type { Data, VFile } from 'vfile';
1
+ import type { VFile, VFileData as Data } from 'vfile';
2
2
  import type { MarkdownAstroData } from './types.js';
3
3
  export declare class InvalidAstroDataError extends TypeError {
4
4
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { AstroMarkdownOptions, MarkdownRenderingOptions, MarkdownRenderingResult } from './types';
2
2
  export { rehypeHeadingIds } from './rehype-collect-headings.js';
3
+ export { remarkCollectImages } from './remark-collect-images.js';
3
4
  export * from './types.js';
4
5
  export declare const markdownConfigDefaults: Omit<Required<AstroMarkdownOptions>, 'drafts'>;
5
6
  /** Shared utility for rendering markdown */
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { toRemarkInitializeAstroData } from "./frontmatter-injection.js";
2
2
  import { loadPlugins } from "./load-plugins.js";
3
3
  import { rehypeHeadingIds } from "./rehype-collect-headings.js";
4
- import toRemarkCollectImages from "./remark-collect-images.js";
4
+ import { remarkCollectImages } from "./remark-collect-images.js";
5
5
  import remarkPrism from "./remark-prism.js";
6
6
  import scopedStyles from "./remark-scoped-styles.js";
7
7
  import remarkShiki from "./remark-shiki.js";
@@ -15,6 +15,7 @@ import { unified } from "unified";
15
15
  import { VFile } from "vfile";
16
16
  import { rehypeImages } from "./rehype-images.js";
17
17
  import { rehypeHeadingIds as rehypeHeadingIds2 } from "./rehype-collect-headings.js";
18
+ import { remarkCollectImages as remarkCollectImages2 } from "./remark-collect-images.js";
18
19
  export * from "./types.js";
19
20
  const markdownConfigDefaults = {
20
21
  syntaxHighlight: "shiki",
@@ -69,7 +70,7 @@ async function renderMarkdown(content, opts) {
69
70
  parser.use([remarkPrism(scopedClassName)]);
70
71
  }
71
72
  if (opts.experimentalAssets) {
72
- parser.use([toRemarkCollectImages()]);
73
+ parser.use([remarkCollectImages]);
73
74
  }
74
75
  }
75
76
  parser.use([
@@ -127,5 +128,6 @@ ${err.message}`;
127
128
  export {
128
129
  markdownConfigDefaults,
129
130
  rehypeHeadingIds2 as rehypeHeadingIds,
131
+ remarkCollectImages2 as remarkCollectImages,
130
132
  renderMarkdown
131
133
  };
@@ -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 declare function remarkCollectImages(): (tree: any, vfile: MarkdownVFile) => void;
@@ -1,15 +1,27 @@
1
1
  import { visit } from "unist-util-visit";
2
- function toRemarkCollectImages() {
3
- return () => async function(tree, vfile) {
2
+ function remarkCollectImages() {
3
+ return function(tree, vfile) {
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
- toRemarkCollectImages as default
26
+ remarkCollectImages
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.4",
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.3.0"
21
21
  },
22
22
  "dependencies": {
23
23
  "@astrojs/prism": "^2.1.0",
@@ -42,10 +42,10 @@
42
42
  "@types/mdast": "^3.0.10",
43
43
  "@types/mocha": "^9.1.1",
44
44
  "@types/unist": "^2.0.6",
45
- "astro-scripts": "0.0.14",
46
45
  "chai": "^4.3.6",
47
46
  "mdast-util-mdx-expression": "^1.3.1",
48
- "mocha": "^9.2.2"
47
+ "mocha": "^9.2.2",
48
+ "astro-scripts": "0.0.14"
49
49
  },
50
50
  "scripts": {
51
51
  "prepublish": "pnpm build",
@@ -1,4 +1,4 @@
1
- import type { Data, VFile } from 'vfile';
1
+ import type { VFile, VFileData as Data } from 'vfile';
2
2
  import type { MarkdownAstroData } from './types.js';
3
3
 
4
4
  function isValidAstroData(obj: unknown): obj is MarkdownAstroData {
package/src/index.ts CHANGED
@@ -8,7 +8,7 @@ import type {
8
8
  import { toRemarkInitializeAstroData } from './frontmatter-injection.js';
9
9
  import { loadPlugins } from './load-plugins.js';
10
10
  import { rehypeHeadingIds } from './rehype-collect-headings.js';
11
- import toRemarkCollectImages from './remark-collect-images.js';
11
+ import { remarkCollectImages } from './remark-collect-images.js';
12
12
  import remarkPrism from './remark-prism.js';
13
13
  import scopedStyles from './remark-scoped-styles.js';
14
14
  import remarkShiki from './remark-shiki.js';
@@ -24,6 +24,7 @@ import { VFile } from 'vfile';
24
24
  import { rehypeImages } from './rehype-images.js';
25
25
 
26
26
  export { rehypeHeadingIds } from './rehype-collect-headings.js';
27
+ export { remarkCollectImages } from './remark-collect-images.js';
27
28
  export * from './types.js';
28
29
 
29
30
  export const markdownConfigDefaults: Omit<Required<AstroMarkdownOptions>, 'drafts'> = {
@@ -96,7 +97,7 @@ export async function renderMarkdown(
96
97
 
97
98
  if (opts.experimentalAssets) {
98
99
  // Apply later in case user plugins resolve relative image paths
99
- parser.use([toRemarkCollectImages()]);
100
+ parser.use([remarkCollectImages]);
100
101
  }
101
102
  }
102
103
 
@@ -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,30 @@
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
- export default function toRemarkCollectImages() {
6
- return () =>
7
- async function (tree: any, vfile: VFile) {
8
- if (typeof vfile?.path !== 'string') return;
5
+ export function remarkCollectImages() {
6
+ return function (tree: any, vfile: MarkdownVFile) {
7
+ if (typeof vfile?.path !== 'string') return;
9
8
 
10
- const imagePaths = new Set<string>();
11
- visit(tree, 'image', function raiseError(node: Image) {
12
- imagePaths.add(node.url);
13
- });
9
+ const imagePaths = new Set<string>();
10
+ visit(tree, 'image', (node: Image) => {
11
+ if (shouldOptimizeImage(node.url)) imagePaths.add(node.url);
12
+ });
14
13
 
15
- vfile.data.imagePaths = Array.from(imagePaths);
16
- };
14
+ vfile.data.imagePaths = imagePaths;
15
+ };
16
+ }
17
+
18
+ function shouldOptimizeImage(src: string) {
19
+ // Optimize anything that is NOT external or an absolute path to `public/`
20
+ return !isValidUrl(src) && !src.startsWith('/');
21
+ }
22
+
23
+ function isValidUrl(str: string): boolean {
24
+ try {
25
+ new URL(str);
26
+ return true;
27
+ } catch {
28
+ return false;
29
+ }
17
30
  }
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
  }