@astrojs/markdown-remark 2.1.0 → 2.1.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.
@@ -1,5 +1,5 @@
1
- @astrojs/markdown-remark:build: cache hit, replaying output a9a85fd17740b5e6
1
+ @astrojs/markdown-remark:build: cache hit, replaying output 16e4e4cd9a08743c
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.1 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,16 @@
1
1
  # @astrojs/markdown-remark
2
2
 
3
+ ## 2.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#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
8
+
9
+ - [#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
10
+
11
+ - 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)]:
12
+ - astro@2.1.4
13
+
3
14
  ## 2.1.0
4
15
 
5
16
  ### Minor Changes
package/dist/index.js CHANGED
@@ -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(await opts.imageService, opts.assetsDir, opts.getImageMetadata));
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(imageService: any, assetsDir: URL | undefined, getImageMetadata: any): () => (tree: any, file: MarkdownVFile) => void;
@@ -1,9 +1,8 @@
1
- import sizeOf from "image-size";
2
1
  import { join as pathJoin } from "node:path";
3
2
  import { fileURLToPath } from "node:url";
4
3
  import { visit } from "unist-util-visit";
5
4
  import { pathToFileURL } from "url";
6
- function rehypeImages(imageService, assetsDir) {
5
+ function rehypeImages(imageService, assetsDir, getImageMetadata) {
7
6
  return () => function(tree, file) {
8
7
  visit(tree, (node) => {
9
8
  var _a;
@@ -23,7 +22,7 @@ function rehypeImages(imageService, assetsDir) {
23
22
  } else {
24
23
  fileURL = pathToFileURL(pathJoin(file.dirname, node.properties.src));
25
24
  }
26
- const fileData = sizeOf(fileURLToPath(fileURL));
25
+ const fileData = getImageMetadata(fileURLToPath(fileURL));
27
26
  fileURL.searchParams.append("origWidth", fileData.width.toString());
28
27
  fileURL.searchParams.append("origHeight", fileData.height.toString());
29
28
  fileURL.searchParams.append("origFormat", fileData.type.toString());
@@ -36,10 +35,11 @@ function rehypeImages(imageService, assetsDir) {
36
35
  },
37
36
  alt: node.properties.alt
38
37
  };
39
- const imageURL = imageService.getURL(options);
38
+ const validatedOptions = imageService.validateOptions ? imageService.validateOptions(options) : options;
39
+ const imageURL = imageService.getURL(validatedOptions);
40
40
  node.properties = Object.assign(node.properties, {
41
41
  src: imageURL,
42
- ...imageService.getHTMLAttributes !== void 0 ? imageService.getHTMLAttributes(options) : {}
42
+ ...imageService.getHTMLAttributes !== void 0 ? imageService.getHTMLAttributes(validatedOptions) : {}
43
43
  });
44
44
  }
45
45
  }
@@ -1,4 +1,4 @@
1
1
  import type { VFile } from 'vfile';
2
- declare type OptionalResolveImage = ((path: string) => Promise<string>) | undefined;
2
+ type OptionalResolveImage = ((path: string) => Promise<string>) | undefined;
3
3
  export default function toRemarkCollectImages(resolveImage: OptionalResolveImage): () => (tree: any, vfile: VFile) => Promise<void>;
4
4
  export {};
@@ -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;
@@ -44,6 +50,7 @@ export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
44
50
  imageService?: any;
45
51
  assetsDir?: URL;
46
52
  resolveImage?: (path: string) => Promise<string>;
53
+ getImageMetadata?: any;
47
54
  }
48
55
  export interface MarkdownHeading {
49
56
  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.1",
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.4"
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
@@ -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(await opts.imageService, opts.assetsDir, opts.getImageMetadata));
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,11 +1,10 @@
1
- import sizeOf from 'image-size';
2
1
  import { join as pathJoin } from 'node:path';
3
2
  import { fileURLToPath } from 'node:url';
4
3
  import { visit } from 'unist-util-visit';
5
4
  import { pathToFileURL } from 'url';
6
- import type { MarkdownVFile } from './types.js';
5
+ import type { ImageMetadata, MarkdownVFile } from './types.js';
7
6
 
8
- export function rehypeImages(imageService: any, assetsDir: URL | undefined) {
7
+ export function rehypeImages(imageService: any, assetsDir: URL | undefined, getImageMetadata: any) {
9
8
  return () =>
10
9
  function (tree: any, file: MarkdownVFile) {
11
10
  visit(tree, (node) => {
@@ -24,10 +23,10 @@ export function rehypeImages(imageService: any, assetsDir: URL | undefined) {
24
23
  fileURL = pathToFileURL(pathJoin(file.dirname, node.properties.src));
25
24
  }
26
25
 
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());
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());
31
30
 
32
31
  let options = {
33
32
  src: {
@@ -39,11 +38,15 @@ export function rehypeImages(imageService: any, assetsDir: URL | undefined) {
39
38
  alt: node.properties.alt,
40
39
  };
41
40
 
42
- const imageURL = imageService.getURL(options);
41
+ const validatedOptions = imageService.validateOptions
42
+ ? imageService.validateOptions(options)
43
+ : options;
44
+
45
+ const imageURL = imageService.getURL(validatedOptions);
43
46
  node.properties = Object.assign(node.properties, {
44
47
  src: imageURL,
45
48
  ...(imageService.getHTMLAttributes !== undefined
46
- ? imageService.getHTMLAttributes(options)
49
+ ? imageService.getHTMLAttributes(validatedOptions)
47
50
  : {}),
48
51
  });
49
52
  }
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;
@@ -64,6 +71,7 @@ export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
64
71
  imageService?: any;
65
72
  assetsDir?: URL;
66
73
  resolveImage?: (path: string) => Promise<string>;
74
+ getImageMetadata?: any;
67
75
  }
68
76
 
69
77
  export interface MarkdownHeading {