@cobapen/markdown 0.3.0 → 0.4.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/dist/index.js CHANGED
@@ -1,79 +1,80 @@
1
1
  import markdownIt from "markdown-it";
2
- import { highlighterForMarkdownIt } from "./code/highlight.js";
2
+ import advTable from "markdown-it-adv-table";
3
3
  import anchor from "markdown-it-anchor";
4
4
  // @ts-ignore
5
5
  import cjkbreaks from "markdown-it-cjk-breaks";
6
6
  // @ts-ignore
7
7
  import deflist from "markdown-it-deflist";
8
8
  // @ts-ignore
9
- import toc from "markdown-it-table-of-contents";
10
- // @ts-ignore
11
9
  import footnote from "markdown-it-footnote";
12
- import { MathjaxEngine } from "./math/mathjax.js";
13
- import { mdmath } from "./math/mdmath.js";
10
+ // @ts-ignore
11
+ import toc from "markdown-it-table-of-contents";
14
12
  import { fence_custom } from "./code/fence-custom.js";
13
+ import { highlighterForMarkdownIt } from "./code/highlight.js";
15
14
  import { replacelink } from "./link/replacelink.js";
15
+ import { MathjaxEngine } from "./math/mathjax.js";
16
+ import { mdmath } from "./math/mdmath.js";
16
17
  const defaultOptions = {
17
- showCodeTitleByDefault: false,
18
- markdown: {
19
- html: true,
20
- linkify: true,
21
- highlight: highlighterForMarkdownIt,
22
- },
23
- math: {
24
- tex: {
25
- macros: {
26
- bm: ["\\boldsymbol{#1}", 1],
27
- },
18
+ showCodeTitleByDefault: false,
19
+ markdown: {
20
+ html: true,
21
+ linkify: true,
22
+ highlight: highlighterForMarkdownIt,
28
23
  },
29
- }
24
+ math: {
25
+ tex: {
26
+ macros: {
27
+ bm: ["\\boldsymbol{#1}", 1],
28
+ },
29
+ },
30
+ }
30
31
  };
31
32
  export class CMarkdown {
32
- _config;
33
- _mj;
34
- _md;
35
- constructor(option) {
36
- const config = { ...defaultOptions, ...option };
37
- const mj = new MathjaxEngine(config.math);
38
- const md = markdownIt(config.markdown);
39
- this._config = config;
40
- this._mj = mj;
41
- this._md = md;
42
- this.setup(md);
43
- }
44
- /**
33
+ _config;
34
+ _mj;
35
+ _md;
36
+ constructor(option) {
37
+ const config = { ...defaultOptions, ...option };
38
+ const mj = new MathjaxEngine(config.math);
39
+ const md = markdownIt(config.markdown);
40
+ this._config = config;
41
+ this._mj = mj;
42
+ this._md = md;
43
+ this.setup(md);
44
+ }
45
+ /**
45
46
  * Install plugins and renderers to the markdown-it instance.
46
47
  *
47
48
  * @param md The instance
48
49
  */
49
- setup(md) {
50
- md.renderer.rules.fence = fence_custom;
51
- md.use(anchor)
52
- .use(cjkbreaks)
53
- .use(footnote)
54
- .use(deflist)
55
- .use(replacelink)
56
- .use(mdmath(this._mj))
57
- .use(toc, {
58
- includeLevel: [2, 3],
59
- });
60
- }
61
- /**
50
+ setup(md) {
51
+ md.renderer.rules.fence = fence_custom;
52
+ md.use(anchor)
53
+ .use(cjkbreaks)
54
+ .use(footnote)
55
+ .use(deflist)
56
+ .use(replacelink)
57
+ .use(advTable)
58
+ .use(mdmath(this._mj))
59
+ .use(toc, {
60
+ includeLevel: [2, 3],
61
+ });
62
+ }
63
+ /**
62
64
  * Render html from markdown.
63
65
  *
64
66
  * @param text markdown text
65
67
  * @returns html text
66
68
  */
67
- render(text) {
68
- const env = { ...this._config }; // env object must s
69
- return this._md.render(text, env);
70
- }
71
- /**
69
+ render(text) {
70
+ const env = { ...this._config }; // env object must s
71
+ return this._md.render(text, env);
72
+ }
73
+ /**
72
74
  * Returns the MathJax CSS.
73
75
  * @returns
74
76
  */
75
- mathcss() {
76
- return this._mj.stylesheet();
77
- }
77
+ mathcss() {
78
+ return this._mj.stylesheet();
79
+ }
78
80
  }
79
- //# sourceMappingURL=index.js.map
@@ -2,7 +2,7 @@ import { PluginWithOptions } from "markdown-it";
2
2
  import Token from "markdown-it/lib/token.mjs";
3
3
  type ReplaceHandler = (link: string, env: any, token: Token) => string;
4
4
  interface Options {
5
- replace: ReplaceHandler;
5
+ replace: ReplaceHandler;
6
6
  }
7
7
  export declare const replacelink: PluginWithOptions<Options>;
8
8
  export default replacelink;
@@ -1,41 +1,39 @@
1
1
  function defaultHandler(link, _env, _token) {
2
- console.log(link);
3
- if (!link.startsWith("http") && link.endsWith(".md")) {
4
- return link.replace(/\.md$/, ".html");
5
- }
6
- else {
7
- return link;
8
- }
2
+ if (!link.startsWith("http") && link.endsWith(".md")) {
3
+ return link.replace(/\.md$/, ".html");
4
+ }
5
+ else {
6
+ return link;
7
+ }
9
8
  }
10
9
  function replaceAttr(token, attrName, handler, env) {
11
- token.attrs?.forEach(attr => {
12
- if (attr[0] === attrName) {
13
- attr[1] = handler(attr[1], env, token);
14
- }
15
- });
10
+ token.attrs?.forEach(attr => {
11
+ if (attr[0] === attrName) {
12
+ attr[1] = handler(attr[1], env, token);
13
+ }
14
+ });
16
15
  }
17
16
  function getHandler(option) {
18
- const replaceFn = option?.replace || defaultHandler;
19
- function handler(state) {
20
- state.tokens.forEach(token => {
21
- if (token.type === "inline" && token.children !== null) {
22
- token.children.forEach(childToken => {
23
- if (childToken.type == "link_open") {
24
- replaceAttr(childToken, "href", replaceFn, state.env);
25
- }
26
- else if (childToken.type == "image") {
27
- replaceAttr(childToken, "src", replaceFn, state.env);
28
- }
17
+ const replaceFn = option?.replace || defaultHandler;
18
+ function handler(state) {
19
+ state.tokens.forEach(token => {
20
+ if (token.type === "inline" && token.children !== null) {
21
+ token.children.forEach(childToken => {
22
+ if (childToken.type == "link_open") {
23
+ replaceAttr(childToken, "href", replaceFn, state.env);
24
+ }
25
+ else if (childToken.type == "image") {
26
+ replaceAttr(childToken, "src", replaceFn, state.env);
27
+ }
28
+ });
29
+ }
29
30
  });
30
- }
31
- });
32
- }
33
- ;
34
- return handler;
31
+ }
32
+ ;
33
+ return handler;
35
34
  }
36
35
  export const replacelink = (md, option) => {
37
- const handler = getHandler(option);
38
- md.core.ruler.after("linkify", "replace_link", handler);
36
+ const handler = getHandler(option);
37
+ md.core.ruler.after("linkify", "replace_link", handler);
39
38
  };
40
39
  export default replacelink;
41
- //# sourceMappingURL=replacelink.js.map
@@ -1,16 +1,7 @@
1
- /**
2
- * mathjax.ts
3
- *
4
- * Server-Side Mathjax converter from TeX input to CommonHTML.
5
- *
6
- * see official examples for more information
7
- * https://github.com/mathjax/mathjax-v3
8
- * https://github.com/mathjax/MathJax-demos-node/blob/master/direct/tex2chtml
9
- */
10
- import { type LiteAdaptor } from "mathjax-full/js/adaptors/liteAdaptor.js";
1
+ import { type LiteDocument } from "mathjax-full/js/adaptors/lite/Document.js";
11
2
  import { LiteElement } from "mathjax-full/js/adaptors/lite/Element.js";
12
3
  import { type LiteText } from "mathjax-full/js/adaptors/lite/Text.js";
13
- import { type LiteDocument } from "mathjax-full/js/adaptors/lite/Document.js";
4
+ import { type LiteAdaptor } from "mathjax-full/js/adaptors/liteAdaptor.js";
14
5
  import { type MathDocument } from "mathjax-full/js/core/MathDocument.js";
15
6
  import { TeX } from "mathjax-full/js/input/tex.js";
16
7
  import { CHTML } from "mathjax-full/js/output/chtml.js";
@@ -18,50 +9,50 @@ type N = LiteElement;
18
9
  type T = LiteText;
19
10
  type D = LiteDocument;
20
11
  interface AnyObject {
21
- [x: string]: any;
12
+ [x: string]: any;
22
13
  }
23
14
  interface TexConfig {
24
- packages: string | [string] | AnyObject;
25
- inlineMath: [[string, string]];
26
- displayMath: [[string, string]];
27
- processEscapes: boolean;
28
- processEnvironments: boolean;
29
- processRefs: boolean;
30
- digits: RegExp;
31
- tags: string;
32
- tagSide: string;
33
- tagIndent: string;
34
- useLabelIds: boolean;
35
- multlineWidth: string;
36
- maxMacros: number;
37
- maxBuffer: number;
38
- baseURL: string;
39
- formatError: (jax: object, err: Error) => void;
40
- macros: AnyObject;
15
+ packages: string | [string] | AnyObject;
16
+ inlineMath: [[string, string]];
17
+ displayMath: [[string, string]];
18
+ processEscapes: boolean;
19
+ processEnvironments: boolean;
20
+ processRefs: boolean;
21
+ digits: RegExp;
22
+ tags: string;
23
+ tagSide: string;
24
+ tagIndent: string;
25
+ useLabelIds: boolean;
26
+ multlineWidth: string;
27
+ maxMacros: number;
28
+ maxBuffer: number;
29
+ baseURL: string;
30
+ formatError: (jax: object, err: Error) => void;
31
+ macros: AnyObject;
41
32
  }
42
33
  interface CHTMLConfig {
43
- scale: number;
44
- minScale: number;
45
- matchFontHeight: boolean;
46
- mtextInheritFont: boolean;
47
- merrorInheritFont: boolean;
48
- mtextFont: string;
49
- merrorFont: string;
50
- mathmlspacing: boolean;
51
- skipAttributes: AnyObject;
52
- exFactor: number;
53
- displayAlign: string;
54
- displayIndent: number | string;
55
- fontURL: string;
56
- adaptiveCSS: boolean;
34
+ scale: number;
35
+ minScale: number;
36
+ matchFontHeight: boolean;
37
+ mtextInheritFont: boolean;
38
+ merrorInheritFont: boolean;
39
+ mtextFont: string;
40
+ merrorFont: string;
41
+ mathmlspacing: boolean;
42
+ skipAttributes: AnyObject;
43
+ exFactor: number;
44
+ displayAlign: string;
45
+ displayIndent: number | string;
46
+ fontURL: string;
47
+ adaptiveCSS: boolean;
57
48
  }
58
49
  export interface Options {
59
- inline: boolean;
60
- em: number;
61
- ex: number;
62
- width: number;
63
- tex?: Partial<TexConfig>;
64
- chtml?: Partial<CHTMLConfig>;
50
+ inline: boolean;
51
+ em: number;
52
+ ex: number;
53
+ width: number;
54
+ tex?: Partial<TexConfig>;
55
+ chtml?: Partial<CHTMLConfig>;
65
56
  }
66
57
  /**
67
58
  * Initialize and encapsulates mathjax instances to generate
@@ -72,26 +63,26 @@ export interface Options {
72
63
  * in your HTML document to render the equation properly.
73
64
  */
74
65
  export declare class MathjaxEngine {
75
- option: Options;
76
- adaptor: LiteAdaptor;
77
- tex: TeX<N, T, D>;
78
- chtml: CHTML<N, T, D>;
79
- html: MathDocument<N, T, D>;
80
- constructor(option?: Partial<Options>);
81
- /**
66
+ option: Options;
67
+ adaptor: LiteAdaptor;
68
+ tex: TeX<N, T, D>;
69
+ chtml: CHTML<N, T, D>;
70
+ html: MathDocument<N, T, D>;
71
+ constructor(option?: Partial<Options>);
72
+ /**
82
73
  * convert TeX input to CHTML.
83
74
  *
84
75
  * @param tex input string
85
76
  * @param override parameter to override the defaults, if you wish to
86
77
  * @returns
87
78
  */
88
- convert(tex: string, override?: Partial<Options>): string;
89
- /**
79
+ convert(tex: string, override?: Partial<Options>): string;
80
+ /**
90
81
  * returns adaptive css (stylesheet for the processed equations only),
91
82
  * or the full mathjax css (if configured)
92
83
  *
93
84
  * @returns css content
94
85
  */
95
- stylesheet(): string;
86
+ stylesheet(): string;
96
87
  }
97
88
  export {};
@@ -7,29 +7,29 @@
7
7
  * https://github.com/mathjax/mathjax-v3
8
8
  * https://github.com/mathjax/MathJax-demos-node/blob/master/direct/tex2chtml
9
9
  */
10
- import { liteAdaptor } from "mathjax-full/js/adaptors/liteAdaptor.js";
10
+ import { merge } from "lodash-es";
11
11
  import { LiteElement } from "mathjax-full/js/adaptors/lite/Element.js"; /* N */
12
+ import { liteAdaptor } from "mathjax-full/js/adaptors/liteAdaptor.js";
12
13
  import { RegisterHTMLHandler } from "mathjax-full/js/handlers/html.js";
14
+ import { AllPackages } from "mathjax-full/js/input/tex/AllPackages.js";
13
15
  import { TeX } from "mathjax-full/js/input/tex.js";
14
16
  import { mathjax } from "mathjax-full/js/mathjax.js";
15
17
  import { CHTML } from "mathjax-full/js/output/chtml.js";
16
- import { AllPackages } from "mathjax-full/js/input/tex/AllPackages.js";
17
- import { merge } from "lodash-es";
18
18
  const MATHJAX_DEFAULT_FONT_URL = "https://cdn.jsdelivr.net/npm/mathjax-full@3/es5/output/chtml/fonts/woff-v2";
19
19
  const defaultOption = {
20
- inline: false,
21
- em: 16,
22
- ex: 8,
23
- width: 80 * 16,
24
- tex: {
25
- packages: AllPackages,
26
- },
27
- chtml: {
28
- scale: 1.21, // magic # chosen which look nice for me
29
- fontURL: MATHJAX_DEFAULT_FONT_URL,
30
- adaptiveCSS: true,
31
- exFactor: 5,
32
- },
20
+ inline: false,
21
+ em: 16,
22
+ ex: 8,
23
+ width: 80 * 16,
24
+ tex: {
25
+ packages: AllPackages,
26
+ },
27
+ chtml: {
28
+ scale: 1.21, // magic # chosen which look nice for me
29
+ fontURL: MATHJAX_DEFAULT_FONT_URL,
30
+ adaptiveCSS: true,
31
+ exFactor: 5,
32
+ },
33
33
  };
34
34
  /**
35
35
  * Initialize and encapsulates mathjax instances to generate
@@ -40,71 +40,70 @@ const defaultOption = {
40
40
  * in your HTML document to render the equation properly.
41
41
  */
42
42
  export class MathjaxEngine {
43
- option;
44
- adaptor;
45
- tex;
46
- chtml;
47
- html;
48
- constructor(option) {
49
- this.option = merge({}, defaultOption, option);
50
- if (typeof this.option.tex?.packages === "string") {
51
- this.option.tex.packages = this.option.tex.packages.split(/\s*,\s*/);
52
- }
53
- this.adaptor = liteAdaptor();
54
- RegisterHTMLHandler(this.adaptor);
55
- const tex = new TeX(this.option.tex);
56
- const chtml = new CHTML(this.option.chtml);
57
- const html = mathjax.document("", {
58
- InputJax: tex,
59
- OutputJax: chtml,
60
- });
61
- html.addRenderAction("typeset", 155, renderDoc, renderMath);
62
- this.tex = tex;
63
- this.chtml = chtml;
64
- this.html = html;
65
- function renderDoc(_doc) { }
66
- function renderMath(math, doc) {
67
- const adaptor = doc.adaptor;
68
- const text = adaptor.node("mjx-copytext", { "aria-hidden": true }, [
69
- adaptor.text(math.math),
70
- ]);
71
- adaptor.setStyle(text, "position", "absolute");
72
- adaptor.setStyle(text, "display", "none");
73
- adaptor.setStyle(text, "width", "0");
74
- adaptor.setStyle(math.typesetRoot, "position", "relative");
75
- adaptor.append(math.typesetRoot, text);
43
+ option;
44
+ adaptor;
45
+ tex;
46
+ chtml;
47
+ html;
48
+ constructor(option) {
49
+ this.option = merge({}, defaultOption, option);
50
+ if (typeof this.option.tex?.packages === "string") {
51
+ this.option.tex.packages = this.option.tex.packages.split(/\s*,\s*/);
52
+ }
53
+ this.adaptor = liteAdaptor();
54
+ RegisterHTMLHandler(this.adaptor);
55
+ const tex = new TeX(this.option.tex);
56
+ const chtml = new CHTML(this.option.chtml);
57
+ const html = mathjax.document("", {
58
+ InputJax: tex,
59
+ OutputJax: chtml,
60
+ });
61
+ html.addRenderAction("typeset", 155, renderDoc, renderMath);
62
+ this.tex = tex;
63
+ this.chtml = chtml;
64
+ this.html = html;
65
+ function renderDoc(_doc) { }
66
+ function renderMath(math, doc) {
67
+ const adaptor = doc.adaptor;
68
+ const text = adaptor.node("mjx-copytext", { "aria-hidden": true }, [
69
+ adaptor.text(math.math),
70
+ ]);
71
+ adaptor.setStyle(text, "position", "absolute");
72
+ adaptor.setStyle(text, "display", "none");
73
+ adaptor.setStyle(text, "width", "0");
74
+ adaptor.setStyle(math.typesetRoot, "position", "relative");
75
+ adaptor.append(math.typesetRoot, text);
76
+ }
76
77
  }
77
- }
78
- /**
78
+ /**
79
79
  * convert TeX input to CHTML.
80
80
  *
81
81
  * @param tex input string
82
82
  * @param override parameter to override the defaults, if you wish to
83
83
  * @returns
84
84
  */
85
- convert(tex, override) {
86
- const node = this.html.convert(tex, {
87
- display: !(override?.inline ?? this.option.inline),
88
- em: override?.em ?? this.option.em,
89
- ex: override?.ex ?? this.option.ex,
90
- containerWidth: override?.width ?? this.option.width,
91
- scale: 1.0,
92
- });
93
- if (node instanceof LiteElement) {
94
- return this.adaptor.outerHTML(node);
85
+ convert(tex, override) {
86
+ const node = this.html.convert(tex, {
87
+ display: !(override?.inline ?? this.option.inline),
88
+ em: override?.em ?? this.option.em,
89
+ ex: override?.ex ?? this.option.ex,
90
+ containerWidth: override?.width ?? this.option.width,
91
+ scale: 1.0,
92
+ });
93
+ if (node instanceof LiteElement) {
94
+ return this.adaptor.outerHTML(node);
95
+ }
96
+ else {
97
+ return "ERROR";
98
+ }
95
99
  }
96
- else {
97
- return "ERROR";
98
- }
99
- }
100
- /**
100
+ /**
101
101
  * returns adaptive css (stylesheet for the processed equations only),
102
102
  * or the full mathjax css (if configured)
103
103
  *
104
104
  * @returns css content
105
105
  */
106
- stylesheet() {
107
- return this.adaptor.textContent(this.chtml.styleSheet(this.html));
108
- }
106
+ stylesheet() {
107
+ return this.adaptor.textContent(this.chtml.styleSheet(this.html));
108
+ }
109
109
  }
110
- //# sourceMappingURL=mathjax.js.map
@@ -13,39 +13,39 @@ import { math_block, math_inline } from "./mdparser.js";
13
13
  * @returns
14
14
  */
15
15
  function getRenderers(mathjax) {
16
- function renderInlineMath(tex) {
17
- return katex.renderToString(tex, {
18
- throwOnError: false,
19
- strict: (code, _msg, _token) => {
20
- switch (code) {
21
- case "unicodeTextInMathMode":
22
- return "ignore";
23
- default:
24
- return "warn";
16
+ function renderInlineMath(tex) {
17
+ return katex.renderToString(tex, {
18
+ throwOnError: false,
19
+ strict: (code, _msg, _token) => {
20
+ switch (code) {
21
+ case "unicodeTextInMathMode":
22
+ return "ignore";
23
+ default:
24
+ return "warn";
25
+ }
26
+ },
27
+ });
28
+ }
29
+ function renderBlockMath(tex) {
30
+ try {
31
+ const math = mathjax.convert(tex);
32
+ return "<p>" + math + "</p>";
33
+ }
34
+ catch (err) {
35
+ console.error(err);
36
+ return tex;
25
37
  }
26
- },
27
- });
28
- }
29
- function renderBlockMath(tex) {
30
- try {
31
- const math = mathjax.convert(tex);
32
- return "<p>" + math + "</p>";
33
38
  }
34
- catch (err) {
35
- console.error(err);
36
- return tex;
39
+ function inlineRenderer(tokens, index) {
40
+ return renderInlineMath(tokens[index].content);
41
+ }
42
+ function blockRenderer(tokens, index) {
43
+ return renderBlockMath(tokens[index].content + "\n");
37
44
  }
38
- }
39
- function inlineRenderer(tokens, index) {
40
- return renderInlineMath(tokens[index].content);
41
- }
42
- function blockRenderer(tokens, index) {
43
- return renderBlockMath(tokens[index].content + "\n");
44
- }
45
- return {
46
- inlineRenderer,
47
- blockRenderer,
48
- };
45
+ return {
46
+ inlineRenderer,
47
+ blockRenderer,
48
+ };
49
49
  }
50
50
  /**
51
51
  * returns a Markdown-It plugin
@@ -54,14 +54,13 @@ function getRenderers(mathjax) {
54
54
  * @returns
55
55
  */
56
56
  export function mdmath(math) {
57
- const renderer = getRenderers(math);
58
- return (md) => {
59
- md.inline.ruler.after("escape", "math_inline", math_inline);
60
- md.block.ruler.after("blockquote", "math_block", math_block, {
61
- alt: ["paragraph", "reference", "blockquote", "list"],
62
- });
63
- md.renderer.rules.math_inline = renderer.inlineRenderer;
64
- md.renderer.rules.math_block = renderer.blockRenderer;
65
- };
57
+ const renderer = getRenderers(math);
58
+ return (md) => {
59
+ md.inline.ruler.after("escape", "math_inline", math_inline);
60
+ md.block.ruler.after("blockquote", "math_block", math_block, {
61
+ alt: ["paragraph", "reference", "blockquote", "list"],
62
+ });
63
+ md.renderer.rules.math_inline = renderer.inlineRenderer;
64
+ md.renderer.rules.math_block = renderer.blockRenderer;
65
+ };
66
66
  }
67
- //# sourceMappingURL=mdmath.js.map