@cobapen/markdown 0.5.3 → 0.6.0
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/lib/index.d.ts +6 -1
- package/lib/index.js +5 -3
- package/lib/link/replacelink.d.ts +3 -2
- package/lib/link/replacelink.js +19 -1
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import markdownIt, { Options as MarkdownOptions } from "markdown-it";
|
|
2
|
+
import { ReplaceHandler } from "./link/replacelink.js";
|
|
2
3
|
import { Options as MathOptions } from "./math/mathjax.js";
|
|
3
4
|
export interface Config {
|
|
4
5
|
showCodeTitleByDefault: boolean;
|
|
6
|
+
linkRewrite?: ReplaceHandler;
|
|
5
7
|
markdown: Partial<MarkdownOptions>;
|
|
6
8
|
math: Partial<MathOptions>;
|
|
7
9
|
}
|
|
10
|
+
export interface RenderOptions {
|
|
11
|
+
file: string;
|
|
12
|
+
}
|
|
8
13
|
export type Options = Partial<Config>;
|
|
9
14
|
export declare class CMarkdown {
|
|
10
15
|
private readonly _config;
|
|
@@ -12,7 +17,7 @@ export declare class CMarkdown {
|
|
|
12
17
|
private readonly _md;
|
|
13
18
|
constructor(option?: Options);
|
|
14
19
|
setup(md: markdownIt): void;
|
|
15
|
-
render(text: string): string;
|
|
20
|
+
render(text: string, opt?: Partial<RenderOptions>): string;
|
|
16
21
|
waitMathInit(): Promise<void>;
|
|
17
22
|
mathcss(): string;
|
|
18
23
|
}
|
package/lib/index.js
CHANGED
|
@@ -47,15 +47,17 @@ export class CMarkdown {
|
|
|
47
47
|
.use(cjk_break)
|
|
48
48
|
.use(footnote)
|
|
49
49
|
.use(deflist)
|
|
50
|
-
.use(replacelink
|
|
50
|
+
.use(replacelink, {
|
|
51
|
+
replace: this._config.linkRewrite,
|
|
52
|
+
})
|
|
51
53
|
.use(advTable)
|
|
52
54
|
.use(mdmath(this._mj))
|
|
53
55
|
.use(toc, {
|
|
54
56
|
includeLevel: [2, 3],
|
|
55
57
|
});
|
|
56
58
|
}
|
|
57
|
-
render(text) {
|
|
58
|
-
const env = { ...
|
|
59
|
+
render(text, opt) {
|
|
60
|
+
const env = { ...opt };
|
|
59
61
|
return this._md.render(text, env);
|
|
60
62
|
}
|
|
61
63
|
async waitMathInit() {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { PluginWithOptions } from "markdown-it";
|
|
2
2
|
import type Token from "markdown-it/lib/token.mjs";
|
|
3
|
-
type ReplaceHandler = (link: string, env: any, token: Token) => string;
|
|
4
|
-
interface Options {
|
|
3
|
+
export type ReplaceHandler = (link: string, env: any, token: Token) => string;
|
|
4
|
+
export interface Options {
|
|
5
5
|
replace: ReplaceHandler;
|
|
6
6
|
}
|
|
7
|
+
export declare function defaultHandler(link: string, _env: any, _token: Token): string;
|
|
7
8
|
export declare const replacelink: PluginWithOptions<Options>;
|
|
8
9
|
export default replacelink;
|
package/lib/link/replacelink.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function defaultHandler(link, _env, _token) {
|
|
1
|
+
export function defaultHandler(link, _env, _token) {
|
|
2
2
|
if (!link.startsWith("http") && link.endsWith(".md")) {
|
|
3
3
|
return link.replace(/\.md$/, ".html");
|
|
4
4
|
}
|
|
@@ -13,6 +13,18 @@ function replaceAttr(token, attrName, handler, env) {
|
|
|
13
13
|
}
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
+
function replaceHtmlAttr(token, handler, env) {
|
|
17
|
+
const regex = /(href|src)\s*=\s*["']([^"']+)["']/g;
|
|
18
|
+
let content = token.content;
|
|
19
|
+
let match = regex.exec(content);
|
|
20
|
+
while (match !== null) {
|
|
21
|
+
const before = match[2];
|
|
22
|
+
const replaced = handler(before, env, token);
|
|
23
|
+
content = content.replace(before, replaced);
|
|
24
|
+
match = regex.exec(content);
|
|
25
|
+
}
|
|
26
|
+
token.content = content;
|
|
27
|
+
}
|
|
16
28
|
function getHandler(option) {
|
|
17
29
|
const replaceFn = option?.replace || defaultHandler;
|
|
18
30
|
function handler(state) {
|
|
@@ -25,8 +37,14 @@ function getHandler(option) {
|
|
|
25
37
|
else if (childToken.type == "image") {
|
|
26
38
|
replaceAttr(childToken, "src", replaceFn, state.env);
|
|
27
39
|
}
|
|
40
|
+
else if (childToken.type == "html_inline") {
|
|
41
|
+
replaceHtmlAttr(childToken, replaceFn, state.env);
|
|
42
|
+
}
|
|
28
43
|
});
|
|
29
44
|
}
|
|
45
|
+
else if (token.type === "html_block") {
|
|
46
|
+
replaceHtmlAttr(token, replaceFn, state.env);
|
|
47
|
+
}
|
|
30
48
|
});
|
|
31
49
|
}
|
|
32
50
|
;
|