marked-katex-extension 5.0.2 → 5.1.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/README.md CHANGED
@@ -60,6 +60,31 @@ $$
60
60
 
61
61
  [`displayMode`](https://katex.org/docs/options.html) will be on by default when using two dollar signs (`$$`) in inline or block katex. And it will be off by default for a single dollar sign (`$`) in inline or block katex.
62
62
 
63
+ ## Non Standard
64
+
65
+ If you want to be able to parse mathematical formulas in non-standard markdown format, that is, without spaces before and after $ or $$, you can turn on the nonStandard option.
66
+
67
+ ```js
68
+ import {marked} from "marked";
69
+ import markedKatex from "marked-katex-extension";
70
+
71
+ const options = {
72
+ nonStandard: true
73
+ };
74
+
75
+ marked.use(markedKatex(options));
76
+
77
+ marked.parse(`
78
+ afdaf$x=x^2$4$x=x^2$
79
+
80
+ $$
81
+ x = x^2
82
+ $$
83
+ `);
84
+ ```
85
+
86
+ ![image](https://private-user-images.githubusercontent.com/14917591/353790887-22238ca5-4a83-487d-912e-95eb10643b34.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI0Nzg0NjEsIm5iZiI6MTcyMjQ3ODE2MSwicGF0aCI6Ii8xNDkxNzU5MS8zNTM3OTA4ODctMjIyMzhjYTUtNGE4My00ODdkLTkxMmUtOTVlYjEwNjQzYjM0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAxVDAyMDkyMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWJkYjhkM2EwNzFiNGRmMTQ0M2QwN2FhN2NlNjhjZDI2ZTY4YmY0OWFjN2JlOGE3MzY3ZmQ5ZTdlYmMwNzliNjYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.EEIyMrF2xQXTRAnM3HrTIIPjdwtCGrQ2M9LoFyKHAww)
87
+
63
88
  ## `options`
64
89
 
65
90
  Options are sent directly to [`katex`](https://katex.org/docs/options.html)
package/lib/index.cjs CHANGED
@@ -3,14 +3,16 @@
3
3
  var katex = require('katex');
4
4
 
5
5
  const inlineRule = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n\$]))\1(?=[\s?!\.,:?!。,:]|$)/;
6
+ const inlineRuleNonStandard = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n\$]))\1/; // Non-standard, even if there are no spaces before and after $ or $$, try to parse
7
+
6
8
  const blockRule = /^(\${1,2})\n((?:\\[^]|[^\\])+?)\n\1(?:\n|$)/;
7
9
 
8
10
  function index(options = {}) {
9
11
  return {
10
12
  extensions: [
11
13
  inlineKatex(options, createRenderer(options, false)),
12
- blockKatex(options, createRenderer(options, true))
13
- ]
14
+ blockKatex(options, createRenderer(options, true)),
15
+ ],
14
16
  };
15
17
  }
16
18
 
@@ -19,6 +21,8 @@ function createRenderer(options, newlineAfter) {
19
21
  }
20
22
 
21
23
  function inlineKatex(options, renderer) {
24
+ const nonStandard = options && options.nonStandard;
25
+ const ruleReg = nonStandard ? inlineRuleNonStandard : inlineRule;
22
26
  return {
23
27
  name: 'inlineKatex',
24
28
  level: 'inline',
@@ -31,11 +35,11 @@ function inlineKatex(options, renderer) {
31
35
  if (index === -1) {
32
36
  return;
33
37
  }
34
-
35
- if (index === 0 || indexSrc.charAt(index - 1) === ' ') {
38
+ const f = nonStandard ? index > -1 : index === 0 || indexSrc.charAt(index - 1) === ' ';
39
+ if (f) {
36
40
  const possibleKatex = indexSrc.substring(index);
37
41
 
38
- if (possibleKatex.match(inlineRule)) {
42
+ if (possibleKatex.match(ruleReg)) {
39
43
  return index;
40
44
  }
41
45
  }
@@ -44,17 +48,17 @@ function inlineKatex(options, renderer) {
44
48
  }
45
49
  },
46
50
  tokenizer(src, tokens) {
47
- const match = src.match(inlineRule);
51
+ const match = src.match(ruleReg);
48
52
  if (match) {
49
53
  return {
50
54
  type: 'inlineKatex',
51
55
  raw: match[0],
52
56
  text: match[2].trim(),
53
- displayMode: match[1].length === 2
57
+ displayMode: match[1].length === 2,
54
58
  };
55
59
  }
56
60
  },
57
- renderer
61
+ renderer,
58
62
  };
59
63
  }
60
64
 
@@ -69,11 +73,11 @@ function blockKatex(options, renderer) {
69
73
  type: 'blockKatex',
70
74
  raw: match[0],
71
75
  text: match[2].trim(),
72
- displayMode: match[1].length === 2
76
+ displayMode: match[1].length === 2,
73
77
  };
74
78
  }
75
79
  },
76
- renderer
80
+ renderer,
77
81
  };
78
82
  }
79
83
 
package/lib/index.umd.js CHANGED
@@ -5,14 +5,16 @@
5
5
  })(this, (function (katex) { 'use strict';
6
6
 
7
7
  const inlineRule = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n\$]))\1(?=[\s?!\.,:?!。,:]|$)/;
8
+ const inlineRuleNonStandard = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n\$]))\1/; // Non-standard, even if there are no spaces before and after $ or $$, try to parse
9
+
8
10
  const blockRule = /^(\${1,2})\n((?:\\[^]|[^\\])+?)\n\1(?:\n|$)/;
9
11
 
10
12
  function index(options = {}) {
11
13
  return {
12
14
  extensions: [
13
15
  inlineKatex(options, createRenderer(options, false)),
14
- blockKatex(options, createRenderer(options, true))
15
- ]
16
+ blockKatex(options, createRenderer(options, true)),
17
+ ],
16
18
  };
17
19
  }
18
20
 
@@ -21,6 +23,8 @@
21
23
  }
22
24
 
23
25
  function inlineKatex(options, renderer) {
26
+ const nonStandard = options && options.nonStandard;
27
+ const ruleReg = nonStandard ? inlineRuleNonStandard : inlineRule;
24
28
  return {
25
29
  name: 'inlineKatex',
26
30
  level: 'inline',
@@ -33,11 +37,11 @@
33
37
  if (index === -1) {
34
38
  return;
35
39
  }
36
-
37
- if (index === 0 || indexSrc.charAt(index - 1) === ' ') {
40
+ const f = nonStandard ? index > -1 : index === 0 || indexSrc.charAt(index - 1) === ' ';
41
+ if (f) {
38
42
  const possibleKatex = indexSrc.substring(index);
39
43
 
40
- if (possibleKatex.match(inlineRule)) {
44
+ if (possibleKatex.match(ruleReg)) {
41
45
  return index;
42
46
  }
43
47
  }
@@ -46,17 +50,17 @@
46
50
  }
47
51
  },
48
52
  tokenizer(src, tokens) {
49
- const match = src.match(inlineRule);
53
+ const match = src.match(ruleReg);
50
54
  if (match) {
51
55
  return {
52
56
  type: 'inlineKatex',
53
57
  raw: match[0],
54
58
  text: match[2].trim(),
55
- displayMode: match[1].length === 2
59
+ displayMode: match[1].length === 2,
56
60
  };
57
61
  }
58
62
  },
59
- renderer
63
+ renderer,
60
64
  };
61
65
  }
62
66
 
@@ -71,11 +75,11 @@
71
75
  type: 'blockKatex',
72
76
  raw: match[0],
73
77
  text: match[2].trim(),
74
- displayMode: match[1].length === 2
78
+ displayMode: match[1].length === 2,
75
79
  };
76
80
  }
77
81
  },
78
- renderer
82
+ renderer,
79
83
  };
80
84
  }
81
85
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "marked-katex-extension",
3
- "version": "5.0.2",
3
+ "version": "5.1.1",
4
4
  "description": "MarkedJS extesion to render katex",
5
5
  "main": "./lib/index.cjs",
6
6
  "module": "./src/index.js",
@@ -28,7 +28,7 @@
28
28
  "test:spec": "node --test spec/marked-tests.js",
29
29
  "test:cover": "jest --coverage",
30
30
  "test:types": "tsd -f spec/index.test-d.ts -t src/index.d.ts",
31
- "lint": "eslint .",
31
+ "lint": "eslint",
32
32
  "build": "rollup -c rollup.config.js",
33
33
  "update-specs": "node ./update-specs.js"
34
34
  },
@@ -43,34 +43,31 @@
43
43
  },
44
44
  "homepage": "https://github.com/UziTech/marked-katex-extension#readme",
45
45
  "peerDependencies": {
46
- "marked": ">=4 <14",
47
- "katex": ">=0.16 <0.17"
46
+ "katex": ">=0.16 <0.17",
47
+ "marked": ">=4 <15"
48
48
  },
49
49
  "devDependencies": {
50
- "@babel/core": "^7.24.7",
51
- "@babel/preset-env": "^7.24.7",
50
+ "@babel/core": "^7.25.2",
51
+ "@babel/preset-env": "^7.25.3",
52
+ "@markedjs/eslint-config": "^1.0.1",
52
53
  "@markedjs/testutils": "13.0.0-0",
53
54
  "@rollup/plugin-node-resolve": "^15.2.3",
54
55
  "@semantic-release/changelog": "^6.0.3",
55
56
  "@semantic-release/commit-analyzer": "^13.0.0",
56
57
  "@semantic-release/git": "^10.0.1",
57
- "@semantic-release/github": "^10.0.6",
58
+ "@semantic-release/github": "^10.1.3",
58
59
  "@semantic-release/npm": "^12.0.1",
59
- "@semantic-release/release-notes-generator": "^14.0.0",
60
+ "@semantic-release/release-notes-generator": "^14.0.1",
60
61
  "babel-jest": "^29.7.0",
61
62
  "cheerio": "^1.0.0-rc.12",
62
- "eslint": "^8.57.0",
63
- "eslint-config-standard": "^17.1.0",
64
- "eslint-plugin-import": "^2.29.1",
65
- "eslint-plugin-n": "^16.6.2",
66
- "eslint-plugin-promise": "^6.2.0",
63
+ "globals": "^15.9.0",
67
64
  "jest-cli": "^29.7.0",
68
- "katex": "^0.16.10",
69
- "marked": "^13.0.0",
65
+ "katex": "^0.16.11",
66
+ "marked": "^14.0.0",
70
67
  "node-fetch": "^3.3.2",
71
- "rollup": "^4.18.0",
68
+ "rollup": "^4.20.0",
72
69
  "semantic-release": "^24.0.0",
73
- "tsd": "^0.31.0"
70
+ "tsd": "^0.31.1"
74
71
  },
75
72
  "dependencies": {
76
73
  "@types/katex": "^0.16.7"
package/src/index.d.ts CHANGED
@@ -1 +1 @@
1
- export default function markedKatex(options?: import('katex').KatexOptions): import('marked').MarkedExtension
1
+ export default function markedKatex(options?: import('katex').KatexOptions): import('marked').MarkedExtension;
package/src/index.js CHANGED
@@ -1,14 +1,16 @@
1
1
  import katex from 'katex';
2
2
 
3
3
  const inlineRule = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n\$]))\1(?=[\s?!\.,:?!。,:]|$)/;
4
+ const inlineRuleNonStandard = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n\$]))\1/; // Non-standard, even if there are no spaces before and after $ or $$, try to parse
5
+
4
6
  const blockRule = /^(\${1,2})\n((?:\\[^]|[^\\])+?)\n\1(?:\n|$)/;
5
7
 
6
8
  export default function(options = {}) {
7
9
  return {
8
10
  extensions: [
9
11
  inlineKatex(options, createRenderer(options, false)),
10
- blockKatex(options, createRenderer(options, true))
11
- ]
12
+ blockKatex(options, createRenderer(options, true)),
13
+ ],
12
14
  };
13
15
  }
14
16
 
@@ -17,6 +19,8 @@ function createRenderer(options, newlineAfter) {
17
19
  }
18
20
 
19
21
  function inlineKatex(options, renderer) {
22
+ const nonStandard = options && options.nonStandard;
23
+ const ruleReg = nonStandard ? inlineRuleNonStandard : inlineRule;
20
24
  return {
21
25
  name: 'inlineKatex',
22
26
  level: 'inline',
@@ -29,11 +33,11 @@ function inlineKatex(options, renderer) {
29
33
  if (index === -1) {
30
34
  return;
31
35
  }
32
-
33
- if (index === 0 || indexSrc.charAt(index - 1) === ' ') {
36
+ const f = nonStandard ? index > -1 : index === 0 || indexSrc.charAt(index - 1) === ' ';
37
+ if (f) {
34
38
  const possibleKatex = indexSrc.substring(index);
35
39
 
36
- if (possibleKatex.match(inlineRule)) {
40
+ if (possibleKatex.match(ruleReg)) {
37
41
  return index;
38
42
  }
39
43
  }
@@ -42,17 +46,17 @@ function inlineKatex(options, renderer) {
42
46
  }
43
47
  },
44
48
  tokenizer(src, tokens) {
45
- const match = src.match(inlineRule);
49
+ const match = src.match(ruleReg);
46
50
  if (match) {
47
51
  return {
48
52
  type: 'inlineKatex',
49
53
  raw: match[0],
50
54
  text: match[2].trim(),
51
- displayMode: match[1].length === 2
55
+ displayMode: match[1].length === 2,
52
56
  };
53
57
  }
54
58
  },
55
- renderer
59
+ renderer,
56
60
  };
57
61
  }
58
62
 
@@ -67,10 +71,10 @@ function blockKatex(options, renderer) {
67
71
  type: 'blockKatex',
68
72
  raw: match[0],
69
73
  text: match[2].trim(),
70
- displayMode: match[1].length === 2
74
+ displayMode: match[1].length === 2,
71
75
  };
72
76
  }
73
77
  },
74
- renderer
78
+ renderer,
75
79
  };
76
80
  }