@astrojs/markdoc 0.8.3 → 0.9.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,9 +1,9 @@
1
- import Markdoc from "@markdoc/markdoc";
2
- import { emitESMImage } from "astro/assets/utils";
3
- import matter from "gray-matter";
4
1
  import fs from "node:fs";
5
2
  import path from "node:path";
6
3
  import { fileURLToPath } from "node:url";
4
+ import Markdoc from "@markdoc/markdoc";
5
+ import { emitESMImage } from "astro/assets/utils";
6
+ import matter from "gray-matter";
7
7
  import { htmlTokenTransform } from "./html/transform/html-token-transform.js";
8
8
  import { setupConfig } from "./runtime.js";
9
9
  import { getMarkdocTokenizer } from "./tokenizer.js";
@@ -137,21 +137,34 @@ function getEntryInfo({ fileUrl, contents }) {
137
137
  }
138
138
  async function emitOptimizedImages(nodeChildren, ctx) {
139
139
  for (const node of nodeChildren) {
140
- if (node.type === "image" && typeof node.attributes.src === "string" && shouldOptimizeImage(node.attributes.src)) {
141
- const resolved = await ctx.pluginContext.resolve(node.attributes.src, ctx.filePath);
142
- if (resolved?.id && fs.existsSync(new URL(prependForwardSlash(resolved.id), "file://"))) {
143
- const src = await emitESMImage(
144
- resolved.id,
145
- ctx.pluginContext.meta.watchMode,
146
- ctx.pluginContext.emitFile
147
- );
148
- node.attributes.__optimizedSrc = src;
149
- } else {
150
- throw new MarkdocError({
151
- message: `Could not resolve image ${JSON.stringify(
152
- node.attributes.src
153
- )} from ${JSON.stringify(ctx.filePath)}. Does the file exist?`
154
- });
140
+ let isComponent = node.type === "tag" && node.tag === "image";
141
+ if ((node.type === "image" || isComponent) && typeof node.attributes.src === "string") {
142
+ let attributeName = isComponent ? "src" : "__optimizedSrc";
143
+ if (shouldOptimizeImage(node.attributes.src)) {
144
+ const resolved = await ctx.pluginContext.resolve(node.attributes.src, ctx.filePath);
145
+ if (resolved?.id && fs.existsSync(new URL(prependForwardSlash(resolved.id), "file://"))) {
146
+ const src = await emitESMImage(
147
+ resolved.id,
148
+ ctx.pluginContext.meta.watchMode,
149
+ ctx.pluginContext.emitFile
150
+ );
151
+ const fsPath = resolved.id;
152
+ if (src) {
153
+ if (ctx.astroConfig.output === "static") {
154
+ if (globalThis.astroAsset.referencedImages)
155
+ globalThis.astroAsset.referencedImages.add(fsPath);
156
+ }
157
+ node.attributes[attributeName] = { ...src, fsPath };
158
+ }
159
+ } else {
160
+ throw new MarkdocError({
161
+ message: `Could not resolve image ${JSON.stringify(
162
+ node.attributes.src
163
+ )} from ${JSON.stringify(ctx.filePath)}. Does the file exist?`
164
+ });
165
+ }
166
+ } else if (isComponent) {
167
+ node.attributes[attributeName] = node.attributes.src;
155
168
  }
156
169
  }
157
170
  await emitOptimizedImages(node.children, ctx);
@@ -11,7 +11,7 @@ function parseInlineCSSToReactLikeObject(css) {
11
11
  return void 0;
12
12
  }
13
13
  function convertCssDirectiveNameToReactCamelCase(original) {
14
- const replaced = original.replace(/-([a-z0-9])/gi, (_match, char) => {
14
+ const replaced = original.replace(/-([a-z\d])/gi, (_match, char) => {
15
15
  return char.toUpperCase();
16
16
  });
17
17
  return replaced;
@@ -14,9 +14,9 @@
14
14
  const COMMENT_REGEX = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g;
15
15
  const NEWLINE_REGEX = /\n/g;
16
16
  const WHITESPACE_REGEX = /^\s*/;
17
- const PROPERTY_REGEX = /^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/;
17
+ const PROPERTY_REGEX = /^([-#/*\\\w]+(\[[\da-z_-]+\])?)\s*/;
18
18
  const COLON_REGEX = /^:\s*/;
19
- const VALUE_REGEX = /^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/;
19
+ const VALUE_REGEX = /^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*\)|[^};])+)/;
20
20
  const SEMICOLON_REGEX = /^[;\s]*/;
21
21
  const TRIM_REGEX = /^\s+|\s+$/g;
22
22
  const NEWLINE = "\n";
@@ -1,6 +1,6 @@
1
- import { build as esbuild } from "esbuild";
2
1
  import * as fs from "node:fs";
3
2
  import { fileURLToPath } from "node:url";
3
+ import { build as esbuild } from "esbuild";
4
4
  import { MarkdocError } from "./utils.js";
5
5
  const SUPPORTED_MARKDOC_CONFIG_FILES = [
6
6
  "markdoc.config.js",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astrojs/markdoc",
3
3
  "description": "Add support for Markdoc in your Astro site",
4
- "version": "0.8.3",
4
+ "version": "0.9.1",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
7
7
  "author": "withastro",
@@ -70,17 +70,13 @@
70
70
  "astro": "^3.0.0 || ^4.0.0"
71
71
  },
72
72
  "devDependencies": {
73
- "@types/chai": "^4.3.10",
74
73
  "@types/html-escaper": "^3.0.2",
75
74
  "@types/markdown-it": "^13.0.6",
76
- "@types/mocha": "^10.0.4",
77
- "chai": "^4.3.7",
78
75
  "devalue": "^4.3.2",
79
76
  "linkedom": "^0.16.4",
80
- "mocha": "^10.2.0",
81
- "vite": "^5.0.10",
82
- "@astrojs/markdown-remark": "4.1.0",
83
- "astro": "4.2.0",
77
+ "vite": "^5.1.4",
78
+ "@astrojs/markdown-remark": "4.2.1",
79
+ "astro": "4.4.9",
84
80
  "astro-scripts": "0.0.14"
85
81
  },
86
82
  "engines": {
@@ -93,7 +89,6 @@
93
89
  "build": "astro-scripts build \"src/**/*.ts\" && tsc",
94
90
  "build:ci": "astro-scripts build \"src/**/*.ts\"",
95
91
  "dev": "astro-scripts dev \"src/**/*.ts\"",
96
- "test": "mocha --exit --timeout 20000",
97
- "test:match": "mocha --timeout 20000 -g"
92
+ "test": "astro-scripts test --timeout 40000 \"test/**/*.test.js\""
98
93
  }
99
94
  }