@astrojs/markdown-remark 2.1.0 → 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 a9a85fd17740b5e6
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.0 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,25 @@
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
+
12
+ ## 2.1.1
13
+
14
+ ### Patch Changes
15
+
16
+ - [#6559](https://github.com/withastro/astro/pull/6559) [`90e5f87d0`](https://github.com/withastro/astro/commit/90e5f87d03215a833bb6ac91f9548670a25ce659) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Vendor `image-size` to fix CJS-related issues
17
+
18
+ - [#6555](https://github.com/withastro/astro/pull/6555) [`f5fddafc2`](https://github.com/withastro/astro/commit/f5fddafc248bb1ef57b7349bfecc25539ae2b5ea) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Add a `validateOptions` hook to the Image Service API in order to set default options and validate the passed options
19
+
20
+ - Updated dependencies [[`04dddd783`](https://github.com/withastro/astro/commit/04dddd783da3235aa9ed523d2856adf86b792b5f), [`ea9b3dd72`](https://github.com/withastro/astro/commit/ea9b3dd72b98b3f5a542ca24a275f673faa6c7c5), [`bf024cb34`](https://github.com/withastro/astro/commit/bf024cb3429c5929d98378108230bc946a376b17), [`22955b895`](https://github.com/withastro/astro/commit/22955b895ce4343e282355db64b3a5c1415f3944), [`f413446a8`](https://github.com/withastro/astro/commit/f413446a859e497395b3612e44d1540cc6b9dad7), [`90e5f87d0`](https://github.com/withastro/astro/commit/90e5f87d03215a833bb6ac91f9548670a25ce659), [`388190102`](https://github.com/withastro/astro/commit/3881901028cbb586f5a4de1b4953e2d6730458ab), [`035c0c4df`](https://github.com/withastro/astro/commit/035c0c4df2a623bcc2f2a1cb9e490df35fa29adc), [`f112c12b1`](https://github.com/withastro/astro/commit/f112c12b15dfbb278d66699f54809674dd1bded0), [`689884251`](https://github.com/withastro/astro/commit/68988425119255382f94c983796574050006f003), [`fa132e35c`](https://github.com/withastro/astro/commit/fa132e35c23f2cfe368fd0a7239584a2bc5c4f12), [`f5fddafc2`](https://github.com/withastro/astro/commit/f5fddafc248bb1ef57b7349bfecc25539ae2b5ea), [`283734525`](https://github.com/withastro/astro/commit/28373452503bc6ca88221ffd39a5590e015e4d71), [`66858f1f2`](https://github.com/withastro/astro/commit/66858f1f238a0edf6ded2b0f693bc738785d5aa3), [`6c465e958`](https://github.com/withastro/astro/commit/6c465e958e088ff55e5b895e67c64c0dfd4277a6)]:
21
+ - astro@2.1.4
22
+
3
23
  ## 2.1.0
4
24
 
5
25
  ### Minor 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));
89
+ parser.use(rehypeImages());
90
90
  }
91
91
  if (!isPerformanceBenchmark) {
92
92
  parser.use([rehypeHeadingIds]);
@@ -1,2 +1,2 @@
1
- import * as unified from 'unified';
1
+ import type * as unified from 'unified';
2
2
  export declare function loadPlugins(items: (string | [string, any] | unified.Plugin<any[], any> | [unified.Plugin<any[], any>, any])[]): Promise<[unified.Plugin, any?]>[];
@@ -1,2 +1,2 @@
1
1
  import type { MarkdownVFile } from './types.js';
2
- export declare function rehypeImages(imageService: any, assetsDir: URL | undefined): () => (tree: any, file: MarkdownVFile) => void;
2
+ export declare function rehypeImages(): () => (tree: any, file: MarkdownVFile) => void;
@@ -1,14 +1,8 @@
1
- import sizeOf from "image-size";
2
- import { join as pathJoin } from "node:path";
3
- import { fileURLToPath } from "node:url";
4
1
  import { visit } from "unist-util-visit";
5
- import { pathToFileURL } from "url";
6
- function rehypeImages(imageService, assetsDir) {
2
+ function rehypeImages() {
7
3
  return () => function(tree, file) {
8
4
  visit(tree, (node) => {
9
5
  var _a;
10
- if (!assetsDir)
11
- return;
12
6
  if (node.type !== "element")
13
7
  return;
14
8
  if (node.tagName !== "img")
@@ -17,30 +11,8 @@ function rehypeImages(imageService, assetsDir) {
17
11
  if (file.dirname) {
18
12
  if (!isRelativePath(node.properties.src) && !isAliasedPath(node.properties.src))
19
13
  return;
20
- let fileURL;
21
- if (isAliasedPath(node.properties.src)) {
22
- fileURL = new URL(stripAliasPath(node.properties.src), assetsDir);
23
- } else {
24
- fileURL = pathToFileURL(pathJoin(file.dirname, node.properties.src));
25
- }
26
- const fileData = sizeOf(fileURLToPath(fileURL));
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
- let options = {
31
- src: {
32
- src: fileURL,
33
- width: fileData.width,
34
- height: fileData.height,
35
- format: fileData.type
36
- },
37
- alt: node.properties.alt
38
- };
39
- const imageURL = imageService.getURL(options);
40
- node.properties = Object.assign(node.properties, {
41
- src: imageURL,
42
- ...imageService.getHTMLAttributes !== void 0 ? imageService.getHTMLAttributes(options) : {}
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) {
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
- declare 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
  }
@@ -1,3 +1,3 @@
1
- declare type MaybeString = string | null | undefined;
1
+ type MaybeString = string | null | undefined;
2
2
  declare function plugin(className: MaybeString): () => (tree: any) => void;
3
3
  export default plugin;
package/dist/types.d.ts CHANGED
@@ -5,14 +5,14 @@ import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
5
5
  import type * as unified from 'unified';
6
6
  import type { VFile } from 'vfile';
7
7
  export type { Node } from 'unist';
8
- export declare type MarkdownAstroData = {
8
+ export type MarkdownAstroData = {
9
9
  frontmatter: Record<string, any>;
10
10
  };
11
- export declare type RemarkPlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, mdast.Root>;
12
- export declare type RemarkPlugins = (string | [string, any] | RemarkPlugin | [RemarkPlugin, any])[];
13
- export declare type RehypePlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, hast.Root>;
14
- export declare type RehypePlugins = (string | [string, any] | RehypePlugin | [RehypePlugin, any])[];
15
- export declare type RemarkRehype = Omit<RemarkRehypeOptions, 'handlers' | 'unknownHandler'> & {
11
+ export type RemarkPlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, mdast.Root>;
12
+ export type RemarkPlugins = (string | [string, any] | RemarkPlugin | [RemarkPlugin, any])[];
13
+ export type RehypePlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, hast.Root>;
14
+ export type RehypePlugins = (string | [string, any] | RehypePlugin | [RehypePlugin, any])[];
15
+ export type RemarkRehype = Omit<RemarkRehypeOptions, 'handlers' | 'unknownHandler'> & {
16
16
  handlers?: typeof Handlers;
17
17
  handler?: typeof Handler;
18
18
  };
@@ -31,6 +31,12 @@ export interface AstroMarkdownOptions {
31
31
  gfm?: boolean;
32
32
  smartypants?: boolean;
33
33
  }
34
+ export interface ImageMetadata {
35
+ src: string;
36
+ width: number;
37
+ height: number;
38
+ type: string;
39
+ }
34
40
  export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
35
41
  /** @internal */
36
42
  fileURL?: URL;
@@ -41,9 +47,6 @@ export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
41
47
  /** Used for frontmatter injection plugins */
42
48
  frontmatter?: Record<string, any>;
43
49
  experimentalAssets?: boolean;
44
- imageService?: any;
45
- assetsDir?: URL;
46
- resolveImage?: (path: string) => Promise<string>;
47
50
  }
48
51
  export interface MarkdownHeading {
49
52
  depth: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -17,12 +17,11 @@
17
17
  "./dist/internal.js": "./dist/internal.js"
18
18
  },
19
19
  "peerDependencies": {
20
- "astro": "^2.1.0"
20
+ "astro": "^2.1.5"
21
21
  },
22
22
  "dependencies": {
23
23
  "@astrojs/prism": "^2.1.0",
24
24
  "github-slugger": "^1.4.0",
25
- "image-size": "^1.0.2",
26
25
  "import-meta-resolve": "^2.1.0",
27
26
  "rehype-raw": "^6.1.1",
28
27
  "rehype-stringify": "^9.0.3",
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));
119
+ parser.use(rehypeImages());
120
120
  }
121
121
  if (!isPerformanceBenchmark) {
122
122
  parser.use([rehypeHeadingIds]);
@@ -1,6 +1,6 @@
1
1
  import { resolve as importMetaResolve } from 'import-meta-resolve';
2
2
  import path from 'path';
3
- import * as unified from 'unified';
3
+ import type * as unified from 'unified';
4
4
  import { pathToFileURL } from 'url';
5
5
 
6
6
  const cwdUrlStr = pathToFileURL(path.join(process.cwd(), 'package.json')).toString();
@@ -1,7 +1,7 @@
1
- import { type Expression, type Super } from 'estree';
1
+ import type { Expression, Super } from 'estree';
2
2
  import Slugger from 'github-slugger';
3
- import { type MdxTextExpression } from 'mdast-util-mdx-expression';
4
- import { type Node } from 'unist';
3
+ import type { MdxTextExpression } from 'mdast-util-mdx-expression';
4
+ import type { Node } from 'unist';
5
5
  import { visit } from 'unist-util-visit';
6
6
 
7
7
  import { InvalidAstroDataError, safelyGetAstroData } from './frontmatter-injection.js';
@@ -1,15 +1,10 @@
1
- import sizeOf from 'image-size';
2
- import { join as pathJoin } from 'node:path';
3
- import { fileURLToPath } from 'node:url';
4
1
  import { visit } from 'unist-util-visit';
5
- import { pathToFileURL } from 'url';
6
2
  import type { MarkdownVFile } from './types.js';
7
3
 
8
- export function rehypeImages(imageService: any, assetsDir: URL | undefined) {
4
+ export function rehypeImages() {
9
5
  return () =>
10
6
  function (tree: any, file: MarkdownVFile) {
11
7
  visit(tree, (node) => {
12
- if (!assetsDir) return;
13
8
  if (node.type !== 'element') return;
14
9
  if (node.tagName !== 'img') return;
15
10
 
@@ -17,35 +12,8 @@ export function rehypeImages(imageService: any, assetsDir: URL | undefined) {
17
12
  if (file.dirname) {
18
13
  if (!isRelativePath(node.properties.src) && !isAliasedPath(node.properties.src)) return;
19
14
 
20
- let fileURL: URL;
21
- if (isAliasedPath(node.properties.src)) {
22
- fileURL = new URL(stripAliasPath(node.properties.src), assetsDir);
23
- } else {
24
- fileURL = pathToFileURL(pathJoin(file.dirname, node.properties.src));
25
- }
26
-
27
- const fileData = sizeOf(fileURLToPath(fileURL));
28
- fileURL.searchParams.append('origWidth', fileData.width!.toString());
29
- fileURL.searchParams.append('origHeight', fileData.height!.toString());
30
- fileURL.searchParams.append('origFormat', fileData.type!.toString());
31
-
32
- let options = {
33
- src: {
34
- src: fileURL,
35
- width: fileData.width,
36
- height: fileData.height,
37
- format: fileData.type,
38
- },
39
- alt: node.properties.alt,
40
- };
41
-
42
- const imageURL = imageService.getURL(options);
43
- node.properties = Object.assign(node.properties, {
44
- src: imageURL,
45
- ...(imageService.getHTMLAttributes !== undefined
46
- ? imageService.getHTMLAttributes(options)
47
- : {}),
48
- });
15
+ node.properties['__ASTRO_IMAGE_'] = node.properties.src;
16
+ delete node.properties.src;
49
17
  }
50
18
  }
51
19
  });
@@ -56,10 +24,6 @@ function isAliasedPath(path: string) {
56
24
  return path.startsWith('~/assets');
57
25
  }
58
26
 
59
- function stripAliasPath(path: string) {
60
- return path.replace('~/assets/', '');
61
- }
62
-
63
27
  function isRelativePath(path: string) {
64
28
  return startsWithDotDotSlash(path) || startsWithDotSlash(path);
65
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
@@ -51,6 +51,13 @@ export interface AstroMarkdownOptions {
51
51
  smartypants?: boolean;
52
52
  }
53
53
 
54
+ export interface ImageMetadata {
55
+ src: string;
56
+ width: number;
57
+ height: number;
58
+ type: string;
59
+ }
60
+
54
61
  export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
55
62
  /** @internal */
56
63
  fileURL?: URL;
@@ -61,9 +68,6 @@ export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
61
68
  /** Used for frontmatter injection plugins */
62
69
  frontmatter?: Record<string, any>;
63
70
  experimentalAssets?: boolean;
64
- imageService?: any;
65
- assetsDir?: URL;
66
- resolveImage?: (path: string) => Promise<string>;
67
71
  }
68
72
 
69
73
  export interface MarkdownHeading {