@astrojs/markdown-remark 2.1.1 → 2.1.2

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 16e4e4cd9a08743c
1
+ @astrojs/markdown-remark:build: cache hit, replaying output 56144f20e07435ef
2
2
  @astrojs/markdown-remark:build: 
3
- @astrojs/markdown-remark:build: > @astrojs/markdown-remark@2.1.1 build /home/runner/work/astro/astro/packages/markdown/remark
3
+ @astrojs/markdown-remark:build: > @astrojs/markdown-remark@2.1.2 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.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#6604](https://github.com/withastro/astro/pull/6604) [`7f7a8504b`](https://github.com/withastro/astro/commit/7f7a8504b5c2df4c99d3025931860c0d50992510) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Fix using optimized images in Markdown not working
8
+
9
+ - Updated dependencies [[`7f7a8504b`](https://github.com/withastro/astro/commit/7f7a8504b5c2df4c99d3025931860c0d50992510), [`38e6ec21e`](https://github.com/withastro/astro/commit/38e6ec21e266ad8765d8ca2293034123b34e839a), [`f42f47dc6`](https://github.com/withastro/astro/commit/f42f47dc6a91cdb6534dab0ecbf9e8e85f00ba40)]:
10
+ - astro@2.1.5
11
+
3
12
  ## 2.1.1
4
13
 
5
14
  ### Patch Changes
package/dist/index.js CHANGED
@@ -69,7 +69,7 @@ async function renderMarkdown(content, opts) {
69
69
  parser.use([remarkPrism(scopedClassName)]);
70
70
  }
71
71
  if (opts.experimentalAssets) {
72
- parser.use([toRemarkCollectImages(opts.resolveImage)]);
72
+ parser.use([toRemarkCollectImages()]);
73
73
  }
74
74
  }
75
75
  parser.use([
@@ -86,7 +86,7 @@ async function renderMarkdown(content, opts) {
86
86
  parser.use([[plugin, pluginOpts]]);
87
87
  });
88
88
  if (opts.experimentalAssets) {
89
- parser.use(rehypeImages(await opts.imageService, opts.assetsDir, opts.getImageMetadata));
89
+ parser.use(rehypeImages());
90
90
  }
91
91
  if (!isPerformanceBenchmark) {
92
92
  parser.use([rehypeHeadingIds]);
@@ -1,2 +1,2 @@
1
1
  import type { MarkdownVFile } from './types.js';
2
- export declare function rehypeImages(imageService: any, assetsDir: URL | undefined, getImageMetadata: any): () => (tree: any, file: MarkdownVFile) => void;
2
+ export declare function rehypeImages(): () => (tree: any, file: MarkdownVFile) => void;
@@ -1,13 +1,8 @@
1
- import { join as pathJoin } from "node:path";
2
- import { fileURLToPath } from "node:url";
3
1
  import { visit } from "unist-util-visit";
4
- import { pathToFileURL } from "url";
5
- function rehypeImages(imageService, assetsDir, getImageMetadata) {
2
+ function rehypeImages() {
6
3
  return () => function(tree, file) {
7
4
  visit(tree, (node) => {
8
5
  var _a;
9
- if (!assetsDir)
10
- return;
11
6
  if (node.type !== "element")
12
7
  return;
13
8
  if (node.tagName !== "img")
@@ -16,31 +11,8 @@ function rehypeImages(imageService, assetsDir, getImageMetadata) {
16
11
  if (file.dirname) {
17
12
  if (!isRelativePath(node.properties.src) && !isAliasedPath(node.properties.src))
18
13
  return;
19
- let fileURL;
20
- if (isAliasedPath(node.properties.src)) {
21
- fileURL = new URL(stripAliasPath(node.properties.src), assetsDir);
22
- } else {
23
- fileURL = pathToFileURL(pathJoin(file.dirname, node.properties.src));
24
- }
25
- const fileData = getImageMetadata(fileURLToPath(fileURL));
26
- fileURL.searchParams.append("origWidth", fileData.width.toString());
27
- fileURL.searchParams.append("origHeight", fileData.height.toString());
28
- fileURL.searchParams.append("origFormat", fileData.type.toString());
29
- let options = {
30
- src: {
31
- src: fileURL,
32
- width: fileData.width,
33
- height: fileData.height,
34
- format: fileData.type
35
- },
36
- alt: node.properties.alt
37
- };
38
- const validatedOptions = imageService.validateOptions ? imageService.validateOptions(options) : options;
39
- const imageURL = imageService.getURL(validatedOptions);
40
- node.properties = Object.assign(node.properties, {
41
- src: imageURL,
42
- ...imageService.getHTMLAttributes !== void 0 ? imageService.getHTMLAttributes(validatedOptions) : {}
43
- });
14
+ node.properties["__ASTRO_IMAGE_"] = node.properties.src;
15
+ delete node.properties.src;
44
16
  }
45
17
  }
46
18
  });
@@ -49,9 +21,6 @@ function rehypeImages(imageService, assetsDir, getImageMetadata) {
49
21
  function isAliasedPath(path) {
50
22
  return path.startsWith("~/assets");
51
23
  }
52
- function stripAliasPath(path) {
53
- return path.replace("~/assets/", "");
54
- }
55
24
  function isRelativePath(path) {
56
25
  return startsWithDotDotSlash(path) || startsWithDotSlash(path);
57
26
  }
@@ -1,4 +1,2 @@
1
1
  import type { VFile } from 'vfile';
2
- type OptionalResolveImage = ((path: string) => Promise<string>) | undefined;
3
- export default function toRemarkCollectImages(resolveImage: OptionalResolveImage): () => (tree: any, vfile: VFile) => Promise<void>;
4
- export {};
2
+ export default function toRemarkCollectImages(): () => (tree: any, vfile: VFile) => Promise<void>;
@@ -1,5 +1,5 @@
1
1
  import { visit } from "unist-util-visit";
2
- function toRemarkCollectImages(resolveImage) {
2
+ function toRemarkCollectImages() {
3
3
  return () => async function(tree, vfile) {
4
4
  if (typeof (vfile == null ? void 0 : vfile.path) !== "string")
5
5
  return;
@@ -7,19 +7,6 @@ function toRemarkCollectImages(resolveImage) {
7
7
  visit(tree, "image", function raiseError(node) {
8
8
  imagePaths.add(node.url);
9
9
  });
10
- if (imagePaths.size === 0) {
11
- vfile.data.imagePaths = [];
12
- return;
13
- } else if (resolveImage) {
14
- const mapping = /* @__PURE__ */ new Map();
15
- for (const path of Array.from(imagePaths)) {
16
- const id = await resolveImage(path);
17
- mapping.set(path, id);
18
- }
19
- visit(tree, "image", function raiseError(node) {
20
- node.url = mapping.get(node.url);
21
- });
22
- }
23
10
  vfile.data.imagePaths = Array.from(imagePaths);
24
11
  };
25
12
  }
package/dist/types.d.ts CHANGED
@@ -47,10 +47,6 @@ export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
47
47
  /** Used for frontmatter injection plugins */
48
48
  frontmatter?: Record<string, any>;
49
49
  experimentalAssets?: boolean;
50
- imageService?: any;
51
- assetsDir?: URL;
52
- resolveImage?: (path: string) => Promise<string>;
53
- getImageMetadata?: any;
54
50
  }
55
51
  export interface MarkdownHeading {
56
52
  depth: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
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.4"
20
+ "astro": "^2.1.5"
21
21
  },
22
22
  "dependencies": {
23
23
  "@astrojs/prism": "^2.1.0",
package/src/index.ts CHANGED
@@ -96,7 +96,7 @@ export async function renderMarkdown(
96
96
 
97
97
  if (opts.experimentalAssets) {
98
98
  // Apply later in case user plugins resolve relative image paths
99
- parser.use([toRemarkCollectImages(opts.resolveImage)]);
99
+ parser.use([toRemarkCollectImages()]);
100
100
  }
101
101
  }
102
102
 
@@ -116,7 +116,7 @@ export async function renderMarkdown(
116
116
  });
117
117
 
118
118
  if (opts.experimentalAssets) {
119
- parser.use(rehypeImages(await opts.imageService, opts.assetsDir, opts.getImageMetadata));
119
+ parser.use(rehypeImages());
120
120
  }
121
121
  if (!isPerformanceBenchmark) {
122
122
  parser.use([rehypeHeadingIds]);
@@ -1,14 +1,10 @@
1
- import { join as pathJoin } from 'node:path';
2
- import { fileURLToPath } from 'node:url';
3
1
  import { visit } from 'unist-util-visit';
4
- import { pathToFileURL } from 'url';
5
- import type { ImageMetadata, MarkdownVFile } from './types.js';
2
+ import type { MarkdownVFile } from './types.js';
6
3
 
7
- export function rehypeImages(imageService: any, assetsDir: URL | undefined, getImageMetadata: any) {
4
+ export function rehypeImages() {
8
5
  return () =>
9
6
  function (tree: any, file: MarkdownVFile) {
10
7
  visit(tree, (node) => {
11
- if (!assetsDir) return;
12
8
  if (node.type !== 'element') return;
13
9
  if (node.tagName !== 'img') return;
14
10
 
@@ -16,39 +12,8 @@ export function rehypeImages(imageService: any, assetsDir: URL | undefined, getI
16
12
  if (file.dirname) {
17
13
  if (!isRelativePath(node.properties.src) && !isAliasedPath(node.properties.src)) return;
18
14
 
19
- let fileURL: URL;
20
- if (isAliasedPath(node.properties.src)) {
21
- fileURL = new URL(stripAliasPath(node.properties.src), assetsDir);
22
- } else {
23
- fileURL = pathToFileURL(pathJoin(file.dirname, node.properties.src));
24
- }
25
-
26
- const fileData = getImageMetadata!(fileURLToPath(fileURL)) as ImageMetadata;
27
- fileURL.searchParams.append('origWidth', fileData.width.toString());
28
- fileURL.searchParams.append('origHeight', fileData.height.toString());
29
- fileURL.searchParams.append('origFormat', fileData.type.toString());
30
-
31
- let options = {
32
- src: {
33
- src: fileURL,
34
- width: fileData.width,
35
- height: fileData.height,
36
- format: fileData.type,
37
- },
38
- alt: node.properties.alt,
39
- };
40
-
41
- const validatedOptions = imageService.validateOptions
42
- ? imageService.validateOptions(options)
43
- : options;
44
-
45
- const imageURL = imageService.getURL(validatedOptions);
46
- node.properties = Object.assign(node.properties, {
47
- src: imageURL,
48
- ...(imageService.getHTMLAttributes !== undefined
49
- ? imageService.getHTMLAttributes(validatedOptions)
50
- : {}),
51
- });
15
+ node.properties['__ASTRO_IMAGE_'] = node.properties.src;
16
+ delete node.properties.src;
52
17
  }
53
18
  }
54
19
  });
@@ -59,10 +24,6 @@ function isAliasedPath(path: string) {
59
24
  return path.startsWith('~/assets');
60
25
  }
61
26
 
62
- function stripAliasPath(path: string) {
63
- return path.replace('~/assets/', '');
64
- }
65
-
66
27
  function isRelativePath(path: string) {
67
28
  return startsWithDotDotSlash(path) || startsWithDotSlash(path);
68
29
  }
@@ -2,9 +2,7 @@ import type { Image } from 'mdast';
2
2
  import { visit } from 'unist-util-visit';
3
3
  import type { VFile } from 'vfile';
4
4
 
5
- type OptionalResolveImage = ((path: string) => Promise<string>) | undefined;
6
-
7
- export default function toRemarkCollectImages(resolveImage: OptionalResolveImage) {
5
+ export default function toRemarkCollectImages() {
8
6
  return () =>
9
7
  async function (tree: any, vfile: VFile) {
10
8
  if (typeof vfile?.path !== 'string') return;
@@ -13,19 +11,6 @@ export default function toRemarkCollectImages(resolveImage: OptionalResolveImage
13
11
  visit(tree, 'image', function raiseError(node: Image) {
14
12
  imagePaths.add(node.url);
15
13
  });
16
- if (imagePaths.size === 0) {
17
- vfile.data.imagePaths = [];
18
- return;
19
- } else if (resolveImage) {
20
- const mapping = new Map<string, string>();
21
- for (const path of Array.from(imagePaths)) {
22
- const id = await resolveImage(path);
23
- mapping.set(path, id);
24
- }
25
- visit(tree, 'image', function raiseError(node: Image) {
26
- node.url = mapping.get(node.url)!;
27
- });
28
- }
29
14
 
30
15
  vfile.data.imagePaths = Array.from(imagePaths);
31
16
  };
package/src/types.ts CHANGED
@@ -68,10 +68,6 @@ export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
68
68
  /** Used for frontmatter injection plugins */
69
69
  frontmatter?: Record<string, any>;
70
70
  experimentalAssets?: boolean;
71
- imageService?: any;
72
- assetsDir?: URL;
73
- resolveImage?: (path: string) => Promise<string>;
74
- getImageMetadata?: any;
75
71
  }
76
72
 
77
73
  export interface MarkdownHeading {