pantsdown 1.3.0 → 1.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pantsdown",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "author": "wallpants",
5
5
  "description": "The markdown parser",
6
6
  "license": "MIT",
package/src/pantsdown.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { Lexer } from "./lexer.ts";
2
2
  import { Parser } from "./parser.ts";
3
- import { type RendererProps } from "./types.ts";
3
+ import { type PantsdownConfig } from "./types.ts";
4
4
 
5
5
  export class Pantsdown {
6
6
  private lexer: Lexer;
7
7
  private parser: Parser;
8
8
 
9
- constructor({ renderer }: { renderer: RendererProps }) {
9
+ constructor(config?: PantsdownConfig) {
10
10
  this.lexer = new Lexer();
11
- this.parser = new Parser({ renderer });
11
+ this.parser = new Parser(config);
12
12
  }
13
13
 
14
14
  parse(src: string): string {
package/src/parser.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Renderer } from "./renderer.ts";
2
- import { type RendererProps, type Token, type Tokens } from "./types.ts";
2
+ import { type PantsdownConfig, type Token, type Tokens } from "./types.ts";
3
3
 
4
4
  /**
5
5
  * Parsing & Compiling
@@ -7,8 +7,8 @@ import { type RendererProps, type Token, type Tokens } from "./types.ts";
7
7
  export class Parser {
8
8
  private renderer: Renderer;
9
9
 
10
- constructor({ renderer }: { renderer: RendererProps }) {
11
- this.renderer = new Renderer(renderer);
10
+ constructor(config: PantsdownConfig | undefined) {
11
+ this.renderer = new Renderer(config);
12
12
  }
13
13
 
14
14
  /**
package/src/renderer.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import hljs from "highlight.js";
2
- import { type RendererProps, type SourceMap } from "./types.ts";
2
+ import { type PantsdownConfig, type SourceMap } from "./types.ts";
3
3
  import {
4
4
  cleanUrl,
5
5
  escape,
@@ -9,14 +9,19 @@ import {
9
9
  renderSourceMap,
10
10
  } from "./utils.ts";
11
11
 
12
+ const defaultConfig: NonNullable<PantsdownConfig["renderer"]> = {
13
+ localImageUrlPrefix: "",
14
+ };
15
+
12
16
  /**
13
17
  * An object containing functions to render tokens to HTML.
14
18
  */
15
19
  export class Renderer {
16
- private props: RendererProps;
20
+ private rendererConfig: NonNullable<PantsdownConfig["renderer"]>;
17
21
 
18
- constructor(props: RendererProps) {
19
- this.props = props;
22
+ constructor(config: PantsdownConfig | undefined) {
23
+ const rendererConfig = Object.assign(defaultConfig, config?.renderer ?? {});
24
+ this.rendererConfig = rendererConfig;
20
25
  }
21
26
 
22
27
  code(code: string, infostring: string | undefined, sourceMap: SourceMap): string {
@@ -41,7 +46,7 @@ export class Renderer {
41
46
  }
42
47
 
43
48
  html(html: string, _block: boolean, sourceMap?: SourceMap | undefined): string {
44
- let result = fixHtmlLocalImageHref(html, this.props.localImageUrlPrefix);
49
+ let result = fixHtmlLocalImageHref(html, this.rendererConfig.localImageUrlPrefix);
45
50
  if (sourceMap) {
46
51
  result = injectHtmlAttributes(result, [
47
52
  ["line-start", sourceMap[0]],
@@ -145,7 +150,7 @@ export class Renderer {
145
150
  if (cleanHref === null) {
146
151
  return text;
147
152
  }
148
- href = fixLocalImageHref(cleanHref, this.props.localImageUrlPrefix);
153
+ href = fixLocalImageHref(cleanHref, this.rendererConfig.localImageUrlPrefix);
149
154
 
150
155
  let out = `<img src="${href}" alt="${text}"`;
151
156
 
package/src/types.ts CHANGED
@@ -187,10 +187,12 @@ export type Token =
187
187
 
188
188
  export type Links = Record<string, { href: string; title: string }>;
189
189
 
190
- export type RendererProps = {
191
- /**
192
- * url prefix. Must start and end with "/"
193
- * example: "/__localimage__/"
194
- */
195
- localImageUrlPrefix: string;
190
+ export type PantsdownConfig = {
191
+ renderer?: {
192
+ /**
193
+ * url prefix. Must start and end with "/"
194
+ * example: "/__localimage__/"
195
+ */
196
+ localImageUrlPrefix: string;
197
+ };
196
198
  };