@cobapen/markdown 0.5.2 → 0.5.3

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
@@ -13,5 +13,6 @@ export declare class CMarkdown {
13
13
  constructor(option?: Options);
14
14
  setup(md: markdownIt): void;
15
15
  render(text: string): string;
16
+ waitMathInit(): Promise<void>;
16
17
  mathcss(): string;
17
18
  }
package/lib/index.js CHANGED
@@ -58,6 +58,9 @@ export class CMarkdown {
58
58
  const env = { ...this._config };
59
59
  return this._md.render(text, env);
60
60
  }
61
+ async waitMathInit() {
62
+ return this._mj.waitInit();
63
+ }
61
64
  mathcss() {
62
65
  return this._mj.stylesheet();
63
66
  }
@@ -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.5.3",
4
4
  "description": "A markdown converter for cobapen website",
5
5
  "keywords": [
6
6
  "markdown"