@cobapen/markdown 0.1.0 → 0.3.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,14 +1,3 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  import markdownIt from "markdown-it";
13
2
  import { highlighterForMarkdownIt } from "./code/highlight.js";
14
3
  import anchor from "markdown-it-anchor";
@@ -23,45 +12,68 @@ import footnote from "markdown-it-footnote";
23
12
  import { MathjaxEngine } from "./math/mathjax.js";
24
13
  import { mdmath } from "./math/mdmath.js";
25
14
  import { fence_custom } from "./code/fence-custom.js";
26
- var defaultOptions = {
27
- showCodeTitleByDefault: false,
15
+ import { replacelink } from "./link/replacelink.js";
16
+ 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
+ },
28
+ },
29
+ }
28
30
  };
29
- var MarkdownConverter = /** @class */ (function () {
30
- function MarkdownConverter(option) {
31
- var config = __assign(__assign({}, defaultOptions), option);
32
- var mj = new MathjaxEngine({
33
- tex: {
34
- macros: {
35
- bm: ["\\boldsymbol{#1}", 1],
36
- },
37
- },
38
- });
39
- var md = markdownIt({
40
- html: true,
41
- linkify: true,
42
- highlight: highlighterForMarkdownIt,
43
- });
44
- md.renderer.rules.fence = fence_custom;
45
- md.use(anchor)
46
- .use(cjkbreaks)
47
- .use(footnote)
48
- .use(deflist)
49
- .use(mdmath(mj))
50
- .use(toc, {
51
- includeLevel: [2, 3],
52
- });
53
- this._config = config;
54
- this._mj = mj;
55
- this._md = md;
56
- }
57
- MarkdownConverter.prototype.render = function (text) {
58
- var env = __assign({}, this._config); // env object must s
59
- return this._md.render(text, env);
60
- };
61
- MarkdownConverter.prototype.mathcss = function () {
62
- return this._mj.stylesheet();
63
- };
64
- return MarkdownConverter;
65
- }());
66
- export { MarkdownConverter };
31
+ 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
+ /**
45
+ * Install plugins and renderers to the markdown-it instance.
46
+ *
47
+ * @param md The instance
48
+ */
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
+ /**
62
+ * Render html from markdown.
63
+ *
64
+ * @param text markdown text
65
+ * @returns html text
66
+ */
67
+ render(text) {
68
+ const env = { ...this._config }; // env object must s
69
+ return this._md.render(text, env);
70
+ }
71
+ /**
72
+ * Returns the MathJax CSS.
73
+ * @returns
74
+ */
75
+ mathcss() {
76
+ return this._mj.stylesheet();
77
+ }
78
+ }
67
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,41 @@
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
+ }
9
+ }
10
+ 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
+ });
16
+ }
17
+ 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
+ }
29
+ });
30
+ }
31
+ });
32
+ }
33
+ ;
34
+ return handler;
35
+ }
36
+ export const replacelink = (md, option) => {
37
+ const handler = getHandler(option);
38
+ md.core.ruler.after("linkify", "replace_link", handler);
39
+ };
40
+ export default replacelink;
41
+ //# 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
- interface Options {
59
- inline: boolean;
60
- em: number;
61
- ex: number;
62
- width: number;
63
- tex?: Partial<TexConfig>;
64
- chtml?: Partial<CHTMLConfig>;
58
+ export interface Options {
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 @@ 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 {};
@@ -15,21 +15,21 @@ import { mathjax } from "mathjax-full/js/mathjax.js";
15
15
  import { CHTML } from "mathjax-full/js/output/chtml.js";
16
16
  import { AllPackages } from "mathjax-full/js/input/tex/AllPackages.js";
17
17
  import { merge } from "lodash-es";
18
- var MATHJAX_DEFAULT_FONT_URL = "https://cdn.jsdelivr.net/npm/mathjax-full@3/es5/output/chtml/fonts/woff-v2";
19
- var 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
- },
18
+ const MATHJAX_DEFAULT_FONT_URL = "https://cdn.jsdelivr.net/npm/mathjax-full@3/es5/output/chtml/fonts/woff-v2";
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
+ },
33
33
  };
34
34
  /**
35
35
  * Initialize and encapsulates mathjax instances to generate
@@ -39,70 +39,72 @@ var defaultOption = {
39
39
  * The other returns a stylesheet document. The stylesheet must be included
40
40
  * in your HTML document to render the equation properly.
41
41
  */
42
- var MathjaxEngine = /** @class */ (function () {
43
- function MathjaxEngine(option) {
44
- var _a;
45
- this.option = merge({}, defaultOption, option);
46
- if (typeof ((_a = this.option.tex) === null || _a === void 0 ? void 0 : _a.packages) === "string") {
47
- this.option.tex.packages = this.option.tex.packages.split(/\s*,\s*/);
48
- }
49
- this.adaptor = liteAdaptor();
50
- RegisterHTMLHandler(this.adaptor);
51
- var tex = new TeX(this.option.tex);
52
- var chtml = new CHTML(this.option.chtml);
53
- var html = mathjax.document("", {
54
- InputJax: tex,
55
- OutputJax: chtml,
56
- });
57
- html.addRenderAction("typeset", 155, renderDoc, renderMath);
58
- this.tex = tex;
59
- this.chtml = chtml;
60
- this.html = html;
61
- function renderDoc(_doc) { }
62
- function renderMath(math, doc) {
63
- var adaptor = doc.adaptor;
64
- var text = adaptor.node("mjx-copytext", { "aria-hidden": true }, [
65
- adaptor.text(math.math),
66
- ]);
67
- adaptor.setStyle(text, "position", "absolute");
68
- adaptor.setStyle(text, "visibility", "hidden");
69
- adaptor.setStyle(math.typesetRoot, "position", "relative");
70
- adaptor.append(math.typesetRoot, text);
71
- }
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*/);
72
52
  }
73
- /**
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
+ /**
74
79
  * convert TeX input to CHTML.
75
80
  *
76
81
  * @param tex input string
77
82
  * @param override parameter to override the defaults, if you wish to
78
83
  * @returns
79
84
  */
80
- MathjaxEngine.prototype.convert = function (tex, override) {
81
- var _a, _b, _c, _d;
82
- var node = this.html.convert(tex, {
83
- display: !((_a = override === null || override === void 0 ? void 0 : override.inline) !== null && _a !== void 0 ? _a : this.option.inline),
84
- em: (_b = override === null || override === void 0 ? void 0 : override.em) !== null && _b !== void 0 ? _b : this.option.em,
85
- ex: (_c = override === null || override === void 0 ? void 0 : override.ex) !== null && _c !== void 0 ? _c : this.option.ex,
86
- containerWidth: (_d = override === null || override === void 0 ? void 0 : override.width) !== null && _d !== void 0 ? _d : this.option.width,
87
- scale: 1.0,
88
- });
89
- if (node instanceof LiteElement) {
90
- return this.adaptor.outerHTML(node);
91
- }
92
- else {
93
- return "ERROR";
94
- }
95
- };
96
- /**
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
+ }
99
+ }
100
+ /**
97
101
  * returns adaptive css (stylesheet for the processed equations only),
98
102
  * or the full mathjax css (if configured)
99
103
  *
100
104
  * @returns css content
101
105
  */
102
- MathjaxEngine.prototype.stylesheet = function () {
103
- return this.adaptor.textContent(this.chtml.styleSheet(this.html));
104
- };
105
- return MathjaxEngine;
106
- }());
107
- export { MathjaxEngine };
106
+ stylesheet() {
107
+ return this.adaptor.textContent(this.chtml.styleSheet(this.html));
108
+ }
109
+ }
108
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: function (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
- var 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: inlineRenderer,
47
- blockRenderer: 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
- var renderer = getRenderers(math);
58
- return function (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