pantsdown 1.4.0 → 1.4.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.
package/README.md CHANGED
@@ -70,6 +70,38 @@ function App() {
70
70
  export default App;
71
71
  ```
72
72
 
73
+ ## Configuration
74
+
75
+ The Pantsdown constructor accepts an optional configuration object:
76
+
77
+ ```typescript
78
+ import { PantsdownConfig } from "pantsdown";
79
+
80
+ const config: PantsdownConfig = {
81
+ renderer: {
82
+ /**
83
+ * Prefix to be added to relative image sources.
84
+ * Must start and end with "/"
85
+ *
86
+ * @example
87
+ * relativeImageUrlPrefix: "/__localimage__/"
88
+ *
89
+ * ![image](./wallpants-512.png)
90
+ * relative src is updated and result in:
91
+ * <img src="/__localimage__/wallpants-512.png" />
92
+ *
93
+ * ![image](https://avatars.githubusercontent.com/wallpants)
94
+ * absolute path remains unchanged:
95
+ * <img src="https://avatars.githubusercontent.com/wallpants" />
96
+ */
97
+ relativeImageUrlPrefix: "/__localimage__/",
98
+ },
99
+ };
100
+
101
+ const pantsdown = new Pantsdown(config);
102
+ pantsdown.parse(markdown);
103
+ ```
104
+
73
105
  ## Roadmap
74
106
 
75
107
  - [x] Add id & anchors to headings
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pantsdown",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "author": "wallpants",
5
5
  "description": "The markdown parser",
6
6
  "license": "MIT",
package/src/renderer.ts CHANGED
@@ -13,7 +13,7 @@ import {
13
13
  } from "./utils.ts";
14
14
 
15
15
  const defaultConfig: NonNullable<PantsdownConfig["renderer"]> = {
16
- localImageUrlPrefix: "",
16
+ relativeImageUrlPrefix: "",
17
17
  };
18
18
 
19
19
  /**
@@ -50,7 +50,7 @@ export class Renderer {
50
50
  }
51
51
 
52
52
  html(html: string, _block: boolean, sourceMap?: SourceMap | undefined): string {
53
- let result = fixHtmlLocalImageHref(html, this.rendererConfig.localImageUrlPrefix);
53
+ let result = fixHtmlLocalImageHref(html, this.rendererConfig.relativeImageUrlPrefix);
54
54
  if (sourceMap) {
55
55
  result = injectHtmlAttributes(result, [
56
56
  ["line-start", sourceMap[0]],
@@ -179,7 +179,7 @@ export class Renderer {
179
179
  if (cleanHref === null) {
180
180
  return text;
181
181
  }
182
- href = fixLocalImageHref(cleanHref, this.rendererConfig.localImageUrlPrefix);
182
+ href = fixLocalImageHref(cleanHref, this.rendererConfig.relativeImageUrlPrefix);
183
183
 
184
184
  let out = `<img src="${href}" alt="${text}"`;
185
185
 
package/src/types.ts CHANGED
@@ -190,9 +190,20 @@ export type Links = Record<string, { href: string; title: string }>;
190
190
  export type PantsdownConfig = {
191
191
  renderer?: {
192
192
  /**
193
- * url prefix. Must start and end with "/"
194
- * example: "/__localimage__/"
193
+ * Prefix to be added to relative image sources.
194
+ * Must start and end with "/"
195
+ *
196
+ * @example
197
+ * relativeImageUrlPrefix: "/__localimage__/"
198
+ *
199
+ * ![image](./wallpants-512.png)
200
+ * relative src is updated and result in:
201
+ * <img src="/__localimage__/wallpants-512.png" />
202
+ *
203
+ * ![image](https://avatars.githubusercontent.com/wallpants)
204
+ * absolute path remains unchanged:
205
+ * <img src="https://avatars.githubusercontent.com/wallpants" />
195
206
  */
196
- localImageUrlPrefix: string;
207
+ relativeImageUrlPrefix: string;
197
208
  };
198
209
  };
package/src/utils.ts CHANGED
@@ -76,21 +76,21 @@ export function injectHtmlAttributes(
76
76
  }
77
77
  }
78
78
 
79
- export function fixHtmlLocalImageHref(html: string, localImageUrlPrefix: string): string {
79
+ export function fixHtmlLocalImageHref(html: string, relativeImageUrlPrefix: string): string {
80
80
  return html.replace(
81
81
  /<img\s+([^>]*?)src\s*=\s*(["'])([^\2>]+?)\2([^>]*)>/gm,
82
82
  (_m, g1, _g2, g3: string, g4) => {
83
- const href = fixLocalImageHref(g3, localImageUrlPrefix);
83
+ const href = fixLocalImageHref(g3, relativeImageUrlPrefix);
84
84
  return `<img ${g1}src="${href}"${g4}>`;
85
85
  },
86
86
  );
87
87
  }
88
88
 
89
- export function fixLocalImageHref(href: string, localImageUrlPrefix: string): string {
89
+ export function fixLocalImageHref(href: string, relativeImageUrlPrefix: string): string {
90
90
  const reIsAbsolute = /^[\w+]+:\/\//;
91
91
  const dummyUrl = "http://__dummy__";
92
- const dummyBaseUrl = new URL(localImageUrlPrefix, dummyUrl);
93
- const dummyUrlLength = dummyUrl.length + (localImageUrlPrefix.startsWith("/") ? 0 : 1);
92
+ const dummyBaseUrl = new URL(relativeImageUrlPrefix, dummyUrl);
93
+ const dummyUrlLength = dummyUrl.length + (relativeImageUrlPrefix.startsWith("/") ? 0 : 1);
94
94
 
95
95
  if (reIsAbsolute.test(href)) {
96
96
  // the URL is absolute, do not touch it