@cobapen/markdown 0.5.2 → 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 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,6 +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;
21
+ waitMathInit(): Promise<void>;
16
22
  mathcss(): string;
17
23
  }
package/lib/index.js CHANGED
@@ -47,17 +47,22 @@ 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 = { ...this._config };
59
+ render(text, opt) {
60
+ const env = { ...opt };
59
61
  return this._md.render(text, env);
60
62
  }
63
+ async waitMathInit() {
64
+ return this._mj.waitInit();
65
+ }
61
66
  mathcss() {
62
67
  return this._mj.stylesheet();
63
68
  }
@@ -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;
@@ -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
  ;
@@ -59,12 +59,15 @@ type FontName = typeof fontNames[number];
59
59
  interface AnyObject {
60
60
  [x: string]: any;
61
61
  }
62
- export interface Options {
62
+ export interface Options extends DeepPartial<EngineOptions> {
63
63
  loader?: DeepPartial<LoaderOptions>;
64
64
  tex?: DeepPartial<TexInputOptions>;
65
65
  output?: DeepPartial<OutputOptions>;
66
66
  chtml?: DeepPartial<CHTMLOptions>;
67
67
  }
68
+ interface EngineOptions {
69
+ noAsyncLoad: boolean;
70
+ }
68
71
  interface LoaderOptions {
69
72
  load: string[];
70
73
  paths: {
@@ -133,13 +136,16 @@ export interface ConvertOptions {
133
136
  width: number;
134
137
  }
135
138
  export declare class MathjaxEngine {
139
+ private _initStatus;
136
140
  option: Options;
137
141
  adaptor: LiteAdaptor;
138
142
  tex: TeX<N, T, D>;
139
143
  chtml: CHTML<N, T, D>;
140
144
  html: MathDocument<N, T, D>;
141
145
  constructor(option?: Partial<Options>);
142
- convert(tex: string, override?: Partial<ConvertOptions>): string;
146
+ private asyncInit;
147
+ waitInit(): Promise<void>;
148
+ convert(tex: string, option?: Partial<ConvertOptions>): string;
143
149
  stylesheet(): string;
144
150
  }
145
151
  export {};
@@ -57,6 +57,7 @@ const fontNames = [
57
57
  "mathjax-newcm",
58
58
  "mathjax-stix2"
59
59
  ];
60
+ const defaultFontName = fontNames[0];
60
61
  function isFontName(name) {
61
62
  return fontNames.includes(name);
62
63
  }
@@ -76,13 +77,14 @@ const packageList = [
76
77
  ].concat(texExtensions);
77
78
  const MATHJAX_DEFAULT_FONT_URL = (name) => `https://cdn.jsdelivr.net/npm/@mathjax/${name}-font@4/chtml/woff2`;
78
79
  const defaultMathOption = {
80
+ noAsyncLoad: true,
79
81
  tex: {
80
82
  packages: packageList,
81
83
  },
82
84
  output: {
83
85
  scale: 1.21,
84
86
  exFactor: 5,
85
- font: "mathjax-newcm",
87
+ font: defaultFontName,
86
88
  },
87
89
  chtml: {
88
90
  adaptiveCSS: true,
@@ -95,6 +97,7 @@ const defaultConvertOption = {
95
97
  width: 80 * 16,
96
98
  };
97
99
  export class MathjaxEngine {
100
+ _initStatus = null;
98
101
  option;
99
102
  adaptor;
100
103
  tex;
@@ -109,6 +112,9 @@ export class MathjaxEngine {
109
112
  ...this.option.output,
110
113
  ...this.option.chtml,
111
114
  });
115
+ if (this.option.noAsyncLoad !== true) {
116
+ this._initStatus = this.asyncInit(chtml);
117
+ }
112
118
  const html = mathjax.document("", {
113
119
  InputJax: tex,
114
120
  OutputJax: chtml,
@@ -130,14 +136,19 @@ export class MathjaxEngine {
130
136
  adaptor.append(math.typesetRoot, text);
131
137
  }
132
138
  }
133
- convert(tex, override) {
134
- const node = this.html.convert(tex, {
135
- display: !(override?.inline ?? defaultConvertOption.inline),
136
- em: override?.em ?? defaultConvertOption.em,
137
- ex: override?.ex ?? defaultConvertOption.ex,
138
- containerWidth: override?.width ?? defaultConvertOption.width,
139
- scale: 1.0,
140
- });
139
+ async asyncInit(chtml) {
140
+ await import("@mathjax/src/js/util/asyncLoad/esm.js");
141
+ await chtml.font.loadDynamicFiles();
142
+ }
143
+ ;
144
+ async waitInit() {
145
+ if (this._initStatus) {
146
+ await this._initStatus;
147
+ this._initStatus = null;
148
+ }
149
+ }
150
+ convert(tex, option) {
151
+ const node = this.html.convert(tex, convOption(option));
141
152
  if (node instanceof LiteElement) {
142
153
  return this.adaptor.outerHTML(node);
143
154
  }
@@ -159,7 +170,7 @@ function initOption(opt) {
159
170
  if (option.output?.font !== undefined && typeof option.output.font === "string") {
160
171
  let name = option.output.font.trim();
161
172
  if (name === "default") {
162
- name = "mathjax-newcm";
173
+ name = defaultFontName;
163
174
  }
164
175
  if (isFontName(name)) {
165
176
  if (option.output.fontData === undefined) {
@@ -171,5 +182,20 @@ function initOption(opt) {
171
182
  }
172
183
  }
173
184
  }
185
+ if (option.chtml?.adaptiveCSS !== true) {
186
+ if (option.noAsyncLoad) {
187
+ console.warn("[MathjaxEngine] adaptiveCSS is disabled, so noAsyncLoad is forced to false.");
188
+ }
189
+ option.noAsyncLoad = false;
190
+ }
174
191
  return option;
175
192
  }
193
+ function convOption(option) {
194
+ return {
195
+ display: !(option?.inline ?? defaultConvertOption.inline),
196
+ em: option?.em ?? defaultConvertOption.em,
197
+ ex: option?.ex ?? defaultConvertOption.ex,
198
+ containerWidth: option?.width ?? defaultConvertOption.width,
199
+ scale: 1.0,
200
+ };
201
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobapen/markdown",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "A markdown converter for cobapen website",
5
5
  "keywords": [
6
6
  "markdown"