@cobapen/markdown 0.2.0 → 0.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/dist/index.d.ts CHANGED
@@ -1,42 +1,42 @@
1
1
  import markdownIt, { Options as MarkdownOptions } from "markdown-it";
2
2
  import { Options as MathOptions } from "./math/mathjax.js";
3
3
  export interface Config {
4
- /**
4
+ /**
5
5
  * Set "true" to display the title (if specified) of the fenced code block.
6
6
  * The title is hidden by default, and user must explicitly override the style.
7
7
  */
8
- showCodeTitleByDefault: boolean;
9
- /**
8
+ showCodeTitleByDefault: boolean;
9
+ /**
10
10
  * MarkdownIt options
11
11
  */
12
- markdown: Partial<MarkdownOptions>;
13
- /**
12
+ markdown: Partial<MarkdownOptions>;
13
+ /**
14
14
  * MathJax options
15
15
  */
16
- math: Partial<MathOptions>;
16
+ math: Partial<MathOptions>;
17
17
  }
18
18
  export type Options = Partial<Config>;
19
19
  export declare class CMarkdown {
20
- private readonly _config;
21
- private readonly _mj;
22
- private readonly _md;
23
- constructor(option?: Options);
24
- /**
20
+ private readonly _config;
21
+ private readonly _mj;
22
+ private readonly _md;
23
+ constructor(option?: Options);
24
+ /**
25
25
  * Install plugins and renderers to the markdown-it instance.
26
26
  *
27
27
  * @param md The instance
28
28
  */
29
- setup(md: markdownIt): void;
30
- /**
29
+ setup(md: markdownIt): void;
30
+ /**
31
31
  * Render html from markdown.
32
32
  *
33
33
  * @param text markdown text
34
34
  * @returns html text
35
35
  */
36
- render(text: string): string;
37
- /**
36
+ render(text: string): string;
37
+ /**
38
38
  * Returns the MathJax CSS.
39
39
  * @returns
40
40
  */
41
- mathcss(): string;
41
+ mathcss(): string;
42
42
  }
package/dist/index.js CHANGED
@@ -12,66 +12,68 @@ import footnote from "markdown-it-footnote";
12
12
  import { MathjaxEngine } from "./math/mathjax.js";
13
13
  import { mdmath } from "./math/mdmath.js";
14
14
  import { fence_custom } from "./code/fence-custom.js";
15
+ import { replacelink } from "./link/replacelink.js";
15
16
  const defaultOptions = {
16
- showCodeTitleByDefault: false,
17
- markdown: {
18
- html: true,
19
- linkify: true,
20
- highlight: highlighterForMarkdownIt,
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
+ },
21
28
  },
22
- math: {
23
- tex: {
24
- macros: {
25
- bm: ["\\boldsymbol{#1}", 1],
26
- },
27
- },
28
- }
29
+ }
29
30
  };
30
31
  export class CMarkdown {
31
- _config;
32
- _mj;
33
- _md;
34
- constructor(option) {
35
- const config = { ...defaultOptions, ...option };
36
- const mj = new MathjaxEngine(config.math);
37
- const md = markdownIt(config.markdown);
38
- this._config = config;
39
- this._mj = mj;
40
- this._md = md;
41
- this.setup(md);
42
- }
43
- /**
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
+ /**
44
45
  * Install plugins and renderers to the markdown-it instance.
45
46
  *
46
47
  * @param md The instance
47
48
  */
48
- setup(md) {
49
- md.renderer.rules.fence = fence_custom;
50
- md.use(anchor)
51
- .use(cjkbreaks)
52
- .use(footnote)
53
- .use(deflist)
54
- .use(mdmath(this._mj))
55
- .use(toc, {
56
- includeLevel: [2, 3],
57
- });
58
- }
59
- /**
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
+ /**
60
62
  * Render html from markdown.
61
63
  *
62
64
  * @param text markdown text
63
65
  * @returns html text
64
66
  */
65
- render(text) {
66
- const env = { ...this._config }; // env object must s
67
- return this._md.render(text, env);
68
- }
69
- /**
67
+ render(text) {
68
+ const env = { ...this._config }; // env object must s
69
+ return this._md.render(text, env);
70
+ }
71
+ /**
70
72
  * Returns the MathJax CSS.
71
73
  * @returns
72
74
  */
73
- mathcss() {
74
- return this._mj.stylesheet();
75
- }
75
+ mathcss() {
76
+ return this._mj.stylesheet();
77
+ }
76
78
  }
77
79
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,8 @@
1
+ import { PluginWithOptions } from "markdown-it";
2
+ import Token from "markdown-it/lib/token.mjs";
3
+ type ReplaceHandler = (link: string, env: any, token: Token) => string;
4
+ interface Options {
5
+ replace: ReplaceHandler;
6
+ }
7
+ export declare const replacelink: PluginWithOptions<Options>;
8
+ export default replacelink;
@@ -0,0 +1,40 @@
1
+ function defaultHandler(link, _env, _token) {
2
+ if (!link.startsWith("http") && link.endsWith(".md")) {
3
+ return link.replace(/\.md$/, ".html");
4
+ }
5
+ else {
6
+ return link;
7
+ }
8
+ }
9
+ function replaceAttr(token, attrName, handler, env) {
10
+ token.attrs?.forEach(attr => {
11
+ if (attr[0] === attrName) {
12
+ attr[1] = handler(attr[1], env, token);
13
+ }
14
+ });
15
+ }
16
+ function getHandler(option) {
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
+ }
30
+ });
31
+ }
32
+ ;
33
+ return handler;
34
+ }
35
+ export const replacelink = (md, option) => {
36
+ const handler = getHandler(option);
37
+ md.core.ruler.after("linkify", "replace_link", handler);
38
+ };
39
+ export default replacelink;
40
+ //# sourceMappingURL=replacelink.js.map
@@ -18,50 +18,50 @@ type N = LiteElement;
18
18
  type T = LiteText;
19
19
  type D = LiteDocument;
20
20
  interface AnyObject {
21
- [x: string]: any;
21
+ [x: string]: any;
22
22
  }
23
23
  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;
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;
41
41
  }
42
42
  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;
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;
57
57
  }
58
58
  export interface Options {
59
- inline: boolean;
60
- em: number;
61
- ex: number;
62
- width: number;
63
- tex?: Partial<TexConfig>;
64
- chtml?: Partial<CHTMLConfig>;
59
+ inline: boolean;
60
+ em: number;
61
+ ex: number;
62
+ width: number;
63
+ tex?: Partial<TexConfig>;
64
+ chtml?: Partial<CHTMLConfig>;
65
65
  }
66
66
  /**
67
67
  * Initialize and encapsulates mathjax instances to generate
@@ -72,26 +72,26 @@ export interface Options {
72
72
  * in your HTML document to render the equation properly.
73
73
  */
74
74
  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
- /**
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
+ /**
82
82
  * convert TeX input to CHTML.
83
83
  *
84
84
  * @param tex input string
85
85
  * @param override parameter to override the defaults, if you wish to
86
86
  * @returns
87
87
  */
88
- convert(tex: string, override?: Partial<Options>): string;
89
- /**
88
+ convert(tex: string, override?: Partial<Options>): string;
89
+ /**
90
90
  * returns adaptive css (stylesheet for the processed equations only),
91
91
  * or the full mathjax css (if configured)
92
92
  *
93
93
  * @returns css content
94
94
  */
95
- stylesheet(): string;
95
+ stylesheet(): string;
96
96
  }
97
97
  export {};
@@ -17,19 +17,19 @@ import { AllPackages } from "mathjax-full/js/input/tex/AllPackages.js";
17
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,71 @@ 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);
76
- }
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*/);
77
52
  }
78
- /**
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
+ }
77
+ }
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);
95
- }
96
- else {
97
- return "ERROR";
98
- }
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";
99
98
  }
100
- /**
99
+ }
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
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";
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;
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";
37
25
  }
26
+ },
27
+ });
28
+ }
29
+ function renderBlockMath(tex) {
30
+ try {
31
+ const math = mathjax.convert(tex);
32
+ return "<p>" + math + "</p>";
38
33
  }
39
- function inlineRenderer(tokens, index) {
40
- return renderInlineMath(tokens[index].content);
41
- }
42
- function blockRenderer(tokens, index) {
43
- return renderBlockMath(tokens[index].content + "\n");
34
+ catch (err) {
35
+ console.error(err);
36
+ return tex;
44
37
  }
45
- return {
46
- inlineRenderer,
47
- blockRenderer,
48
- };
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
+ };
49
49
  }
50
50
  /**
51
51
  * returns a Markdown-It plugin
@@ -54,14 +54,14 @@ 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
67
  //# sourceMappingURL=mdmath.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobapen/markdown",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "A markdown converter for cobapen website",
5
5
  "keywords": ["markdown"],
6
6
  "license": "MIT",