pantsdown 2.2.4 → 2.2.5

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.
package/README.md CHANGED
@@ -105,32 +105,50 @@ import { Pantsdown, type PartialPantsdownConfig } from "pantsdown";
105
105
  // This is the default config object. If you provide
106
106
  // a config object, it will be deeply merged into this.
107
107
  const config: PartialPantsdownConfig = {
108
- renderer: {
109
- /**
110
- * Prefix to be added to relative image sources.
111
- * Must start and end with "/"
112
- *
113
- * @example
114
- * relativeImageUrlPrefix: "/__localimage__/"
115
- *
116
- * ![image](./wallpants-512.png)
117
- * relative src is updated and results in:
118
- * <img src="/__localimage__/wallpants-512.png" />
119
- *
120
- * ![image](https://avatars.githubusercontent.com/wallpants)
121
- * absolute src remains unchanged:
122
- * <img src="https://avatars.githubusercontent.com/wallpants" />
123
- */
124
- relativeImageUrlPrefix: "",
125
-
126
- /**
127
- * Whether to render <details> html tags with attribute open=""
128
- *
129
- * @default
130
- * false
131
- */
132
- detailsTagDefaultOpen: false,
133
- },
108
+ renderer: {
109
+ /**
110
+ * Prefix to be added to relative image sources.
111
+ * Must start and end with "/"
112
+ *
113
+ * @example
114
+ * relativeImageUrlPrefix: "/__localimage__/nested/directory/"
115
+ *
116
+ * ![image](./wallpants-512.png)
117
+ * relative src is updated and results in:
118
+ * <img src="/__localimage__/nested/directory/wallpants-512.png" />
119
+ *
120
+ * ![image](https://avatars.githubusercontent.com/wallpants)
121
+ * absolute src remains unchanged:
122
+ * <img src="https://avatars.githubusercontent.com/wallpants" />
123
+ */
124
+ relativeImageUrlPrefix: string;
125
+
126
+ /**
127
+ * Prefix to be added to absolute image sources.
128
+ * Must start and end with "/"
129
+ * Falls back to `relativeImageUrlPrefix` if not provided
130
+ *
131
+ * @example
132
+ * absoluteImageUrlPrefix: "/__localimage__/"
133
+ *
134
+ * ![image](/wallpants-512.png)
135
+ * relative src is updated and results in:
136
+ * <img src="/__localimage__/wallpants-512.png" />
137
+ *
138
+ * ![image](https://avatars.githubusercontent.com/wallpants)
139
+ * external src remains unchanged:
140
+ * <img src="https://avatars.githubusercontent.com/wallpants" />
141
+ */
142
+ absoluteImageUrlPrefix?: string | undefined;
143
+
144
+ /**
145
+ * Whether to render <details> html tags with attribute `open=""`
146
+ *
147
+ * @default
148
+ * false
149
+ */
150
+ detailsTagDefaultOpen: boolean;
151
+ };
134
152
  };
135
153
 
136
154
  const pantsdown = new Pantsdown(config);
package/bun.lock CHANGED
@@ -1403,6 +1403,8 @@
1403
1403
 
1404
1404
  "@typescript-eslint/typescript-estree/minimatch/brace-expansion": ["brace-expansion@5.0.6", "", { "dependencies": { "balanced-match": "^4.0.2" } }, "sha512-kLpxurY4Z4r9sgMsyG0Z9uzsBlgiU/EFKhj/h91/8yHu0edo7XuixOIH3VcJ8kkxs6/jPzoI6U9Vj3WqbMQ94g=="],
1405
1405
 
1406
+ "bl/readable-stream/string_decoder": ["string_decoder@1.3.0", "", { "dependencies": { "safe-buffer": "~5.2.0" } }, "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA=="],
1407
+
1406
1408
  "cli-highlight/yargs/cliui": ["cliui@7.0.4", "", { "dependencies": { "string-width": "^4.2.0", "strip-ansi": "^6.0.0", "wrap-ansi": "^7.0.0" } }, "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ=="],
1407
1409
 
1408
1410
  "cli-highlight/yargs/yargs-parser": ["yargs-parser@20.2.9", "", {}, "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w=="],
@@ -1465,6 +1467,8 @@
1465
1467
 
1466
1468
  "@typescript-eslint/typescript-estree/minimatch/brace-expansion/balanced-match": ["balanced-match@4.0.4", "", {}, "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA=="],
1467
1469
 
1470
+ "bl/readable-stream/string_decoder/safe-buffer": ["safe-buffer@5.2.1", "", {}, "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="],
1471
+
1468
1472
  "commitizen/inquirer/figures/escape-string-regexp": ["escape-string-regexp@1.0.5", "", {}, "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg=="],
1469
1473
 
1470
1474
  "cz-conventional-changelog/chalk/ansi-styles/color-convert": ["color-convert@1.9.3", "", { "dependencies": { "color-name": "1.1.3" } }, "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg=="],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pantsdown",
3
- "version": "2.2.4",
3
+ "version": "2.2.5",
4
4
  "description": "Markdown to 'GitHub HTML' parser",
5
5
  "license": "MIT",
6
6
  "author": "wallpants",
package/src/pantsdown.ts CHANGED
@@ -7,6 +7,7 @@ import { type PantsdownConfig, type PartialPantsdownConfig } from "./types.ts";
7
7
  const defaultConfig: PantsdownConfig = {
8
8
  renderer: {
9
9
  relativeImageUrlPrefix: "",
10
+ absoluteImageUrlPrefix: undefined,
10
11
  detailsTagDefaultOpen: false,
11
12
  },
12
13
  };
package/src/renderer.ts CHANGED
@@ -61,10 +61,7 @@ export class Renderer {
61
61
  }
62
62
 
63
63
  html(html: string, _block: boolean, sourceMap?: SourceMap): string {
64
- let result = fixHtmlLocalImageHref(
65
- html,
66
- this.pantsdown.config.renderer.relativeImageUrlPrefix,
67
- );
64
+ let result = fixHtmlLocalImageHref(html, this.pantsdown.config);
68
65
  result = addGithubImageStyles(result);
69
66
 
70
67
  const attrs: HTMLAttrs = [];
@@ -190,10 +187,7 @@ export class Renderer {
190
187
  return text;
191
188
  }
192
189
  const attrs: HTMLAttrs = [
193
- [
194
- "src",
195
- fixLocalImageHref(cleanHref, this.pantsdown.config.renderer.relativeImageUrlPrefix),
196
- ],
190
+ ["src", fixLocalImageHref(cleanHref, this.pantsdown.config)],
197
191
  ["alt", text],
198
192
  ];
199
193
  if (title) attrs.push(["title", title]);
package/src/types.ts CHANGED
@@ -238,11 +238,11 @@ export type PantsdownConfig = {
238
238
  * Must start and end with "/"
239
239
  *
240
240
  * @example
241
- * relativeImageUrlPrefix: "/__localimage__/"
241
+ * relativeImageUrlPrefix: "/__localimage__/nested/directory/"
242
242
  *
243
243
  * ![image](./wallpants-512.png)
244
244
  * relative src is updated and results in:
245
- * <img src="/__localimage__/wallpants-512.png" />
245
+ * <img src="/__localimage__/nested/directory/wallpants-512.png" />
246
246
  *
247
247
  * ![image](https://avatars.githubusercontent.com/wallpants)
248
248
  * absolute src remains unchanged:
@@ -250,6 +250,24 @@ export type PantsdownConfig = {
250
250
  */
251
251
  relativeImageUrlPrefix: string;
252
252
 
253
+ /**
254
+ * Prefix to be added to absolute image sources.
255
+ * Must start and end with "/"
256
+ * Falls back to `relativeImageUrlPrefix` if not provided
257
+ *
258
+ * @example
259
+ * absoluteImageUrlPrefix: "/__localimage__/"
260
+ *
261
+ * ![image](./wallpants-512.png)
262
+ * relative src is updated and results in:
263
+ * <img src="/__localimage__/wallpants-512.png" />
264
+ *
265
+ * ![image](https://avatars.githubusercontent.com/wallpants)
266
+ * external src remains unchanged:
267
+ * <img src="https://avatars.githubusercontent.com/wallpants" />
268
+ */
269
+ absoluteImageUrlPrefix?: string | undefined;
270
+
253
271
  /**
254
272
  * Whether to render <details> html tags with attribute `open=""`
255
273
  *
package/src/utils.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type Lexer } from "./lexer.ts";
2
- import { type HTMLAttrs, type SourceMap, type Tokens } from "./types.ts";
2
+ import { PantsdownConfig, type HTMLAttrs, type SourceMap, type Tokens } from "./types.ts";
3
3
 
4
4
  /**
5
5
  * Helpers
@@ -67,15 +67,12 @@ export function injectHtmlAttributes(html: string, attrs: HTMLAttrs, sourceMap?:
67
67
  return html;
68
68
  }
69
69
 
70
- export function fixHtmlLocalImageHref(
71
- html: string,
72
- relativeImageUrlPrefix: string | undefined,
73
- ): string {
74
- return relativeImageUrlPrefix
70
+ export function fixHtmlLocalImageHref(html: string, config: PantsdownConfig): string {
71
+ return config.renderer.relativeImageUrlPrefix
75
72
  ? html.replace(
76
73
  /<img\s+([^>]*?)src\s*=\s*(["'])([^>'"]+?)\2([^>]*)>/gm,
77
74
  (_m, g1, _g2, g3: string, g4) => {
78
- const href = fixLocalImageHref(g3, relativeImageUrlPrefix);
75
+ const href = fixLocalImageHref(g3, config);
79
76
  return `<img ${g1}src="${href}"${g4}>`;
80
77
  },
81
78
  )
@@ -120,25 +117,27 @@ export function addGithubImageStyles(html: string): string {
120
117
  });
121
118
  }
122
119
 
123
- export function fixLocalImageHref(
124
- href: string,
125
- relativeImageUrlPrefix: string | undefined,
126
- ): string {
127
- if (!relativeImageUrlPrefix) return href;
120
+ export function fixLocalImageHref(href: string, config: PantsdownConfig): string {
121
+ if (!config.renderer.relativeImageUrlPrefix) return href;
128
122
 
129
- const reIsAbsolute = /^[\w+]+:\/\//;
123
+ // http://something
124
+ const refIsExternal = /^[\w+]+:\/\//;
130
125
  const dummyUrl = "http://__dummy__";
131
- const dummyBaseUrl = new URL(relativeImageUrlPrefix, dummyUrl);
132
- const dummyUrlLength = dummyUrl.length + (relativeImageUrlPrefix.startsWith("/") ? 0 : 1);
126
+ const dummyBaseUrl = new URL(config.renderer.relativeImageUrlPrefix, dummyUrl);
127
+ const dummyUrlLength =
128
+ dummyUrl.length + (config.renderer.relativeImageUrlPrefix.startsWith("/") ? 0 : 1);
133
129
 
134
- if (reIsAbsolute.test(href)) {
135
- // the URL is absolute, do not touch it
130
+ if (refIsExternal.test(href)) {
131
+ // the URL is external, do not touch it
136
132
  return href;
137
133
  }
138
134
 
139
135
  if (href.startsWith("/")) {
140
136
  // the URL is from root
141
- return relativeImageUrlPrefix + href.slice(1);
137
+ return (
138
+ (config.renderer.absoluteImageUrlPrefix ?? config.renderer.relativeImageUrlPrefix) +
139
+ href.slice(1)
140
+ );
142
141
  }
143
142
 
144
143
  try {