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 +1 -1
- package/src/pantsdown.ts +3 -3
- package/src/parser.ts +3 -3
- package/src/renderer.ts +11 -6
- package/src/types.ts +8 -6
package/package.json
CHANGED
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
|
|
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(
|
|
9
|
+
constructor(config?: PantsdownConfig) {
|
|
10
10
|
this.lexer = new Lexer();
|
|
11
|
-
this.parser = new Parser(
|
|
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
|
|
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(
|
|
11
|
-
this.renderer = new 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
|
|
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
|
|
20
|
+
private rendererConfig: NonNullable<PantsdownConfig["renderer"]>;
|
|
17
21
|
|
|
18
|
-
constructor(
|
|
19
|
-
|
|
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.
|
|
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.
|
|
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
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
};
|