質問するログイン新規登録

質問編集履歴

3

変更

2020/12/21 08:40

投稿

aiai8976
aiai8976

スコア112

title CHANGED
@@ -1,1 +1,1 @@
1
- [react-codemirror2]自作modeを作りたい
1
+ [react-codemirror2]TypeError: mfactory is not a function
body CHANGED
@@ -1,8 +1,7 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
3
  codemirrorのreactラッパーを使っています。
4
- 自作modeを指定して自分が指定した正規表現と一致した時にハイライトを付けたいと思っているのですが、状態だとハイイト付かなくなってしま
4
+ 自作modeを指定して自分が指定した正規表現と一致した時にハイライトを付けたいと思っているのですが、以下ようなエ出てうま行きません。nodemodulesでエラーになっているというのは、自力で直せないということでょうか
5
- 公式documentをみながらやっていますが、うまくいきません。どのようにすれば自作modeを作成できるのでしょうか。
6
5
  わかる方、経験者がいればコメントお願いします。
7
6
 
8
7
  参考
@@ -11,8 +10,18 @@
11
10
 
12
11
  ### 発生している問題・エラーメッセージ
13
12
 
14
- ハイライトが付かない
13
+ TypeError: mfactory is not a function
15
14
 
15
+ getMode
16
+ node_modules/codemirror/lib/codemirror.js:751
17
+ 748 | spec = resolveMode(spec);
18
+ 749 | var mfactory = modes[spec.name];
19
+ 750 | if (!mfactory) { return getMode(options, "text/plain") }
20
+ > 751 | var modeObj = mfactory(options, spec);
21
+ | ^ 752 | if (modeExtensions.hasOwnProperty(spec.name)) {
22
+ 753 | var exts = modeExtensions[spec.name];
23
+ 754 | for (var prop in exts) {
24
+
16
25
  ### 該当のソースコード
17
26
 
18
27
  ```ここに言語名を入力
@@ -27,7 +36,10 @@
27
36
  <CodeMirror
28
37
  value="<h1>I ♥ react-codemirror2 @ </h1>"
29
38
  options={{
39
+ theme: "material",
40
+ lineNumbers: true,
41
+ }}
30
- defineMode: {
42
+ defineMode: {
31
43
  name: "simplemode",
32
44
  fn: {
33
45
  start: [
@@ -37,9 +49,6 @@
37
49
  ],
38
50
  },
39
51
  },
40
- theme: "material",
41
- lineNumbers: true,
42
- }}
43
52
  onChange={(editor, data, value) => {
44
53
  }}
45
54
  />
@@ -48,53 +57,4 @@
48
57
 
49
58
  export default CodemirrorArea;
50
59
 
51
- ```
52
-
53
-
54
- ##試したこと
55
- modeの中身を以下のように公式documentと同じにしてもハイライトができなかったので、何か根本が違いそうです。。。
56
-
57
- ```
58
- // The start state contains the rules that are intially used
59
- start: [
60
- // The regex matches the token, the token property contains the type
61
- {regex: /"(?:[^\]|\.)*?(?:"|$)/, token: "string"},
62
- // You can match multiple tokens at once. Note that the captured
63
- // groups must span the whole string in this case
64
- {regex: /(function)(\s+)([a-z$][\w$]*)/,
65
- token: ["keyword", null, "variable-2"]},
66
- // Rules are matched in the order in which they appear, so there is
67
- // no ambiguity between this one and the one above
68
- {regex: /(?:function|var|return|if|for|while|else|do|this)\b/,
69
- token: "keyword"},
70
- {regex: /true|false|null|undefined/, token: "atom"},
71
- {regex: /0x[a-f\d]+|[-+]?(?:.\d+|\d+.?\d*)(?:e[-+]?\d+)?/i,
72
- token: "number"},
73
- {regex: ///.*/, token: "comment"},
74
- {regex: //(?:[^\]|\.)*?//, token: "variable-3"},
75
- // A next property will cause the mode to move to a different state
76
- {regex: //*/, token: "comment", next: "comment"},
77
- {regex: /[-+/*=<>!]+/, token: "operator"},
78
- // indent and dedent properties guide autoindentation
79
- {regex: /[\{[(]/, indent: true},
80
- {regex: /[\}])]/, dedent: true},
81
- {regex: /[a-z$][\w$]*/, token: "variable"},
82
- // You can embed other modes with the mode property. This rule
83
- // causes all code between << and >> to be highlighted with the XML
84
- // mode.
85
- {regex: /<</, token: "meta", mode: {spec: "xml", end: />>/}}
86
- ],
87
- // The multi-line comment state.
88
- comment: [
89
- {regex: /.*?*//, token: "comment", next: "start"},
90
- {regex: /.*/, token: "comment"}
91
- ],
92
- // The meta property contains global information about the mode. It
93
- // can contain properties like lineComment, which are supported by
94
- // all modes, and also directives like dontIndentStates, which are
95
- // specific to simple modes.
96
- meta: {
97
- dontIndentStates: ["comment"],
98
- lineComment: "//"
99
- }
100
60
  ```

2

変更

2020/12/21 08:40

投稿

aiai8976
aiai8976

スコア112

title CHANGED
File without changes
body CHANGED
@@ -50,4 +50,51 @@
50
50
 
51
51
  ```
52
52
 
53
+
54
+ ##試したこと
55
+ modeの中身を以下のように公式documentと同じにしてもハイライトができなかったので、何か根本が違いそうです。。。
56
+
57
+ ```
58
+ // The start state contains the rules that are intially used
59
+ start: [
60
+ // The regex matches the token, the token property contains the type
61
+ {regex: /"(?:[^\]|\.)*?(?:"|$)/, token: "string"},
62
+ // You can match multiple tokens at once. Note that the captured
63
+ // groups must span the whole string in this case
64
+ {regex: /(function)(\s+)([a-z$][\w$]*)/,
65
+ token: ["keyword", null, "variable-2"]},
66
+ // Rules are matched in the order in which they appear, so there is
67
+ // no ambiguity between this one and the one above
68
+ {regex: /(?:function|var|return|if|for|while|else|do|this)\b/,
69
+ token: "keyword"},
70
+ {regex: /true|false|null|undefined/, token: "atom"},
71
+ {regex: /0x[a-f\d]+|[-+]?(?:.\d+|\d+.?\d*)(?:e[-+]?\d+)?/i,
72
+ token: "number"},
73
+ {regex: ///.*/, token: "comment"},
74
+ {regex: //(?:[^\]|\.)*?//, token: "variable-3"},
75
+ // A next property will cause the mode to move to a different state
76
+ {regex: //*/, token: "comment", next: "comment"},
77
+ {regex: /[-+/*=<>!]+/, token: "operator"},
78
+ // indent and dedent properties guide autoindentation
79
+ {regex: /[\{[(]/, indent: true},
80
+ {regex: /[\}])]/, dedent: true},
81
+ {regex: /[a-z$][\w$]*/, token: "variable"},
82
+ // You can embed other modes with the mode property. This rule
83
+ // causes all code between << and >> to be highlighted with the XML
84
+ // mode.
85
+ {regex: /<</, token: "meta", mode: {spec: "xml", end: />>/}}
86
+ ],
87
+ // The multi-line comment state.
88
+ comment: [
89
+ {regex: /.*?*//, token: "comment", next: "start"},
90
+ {regex: /.*/, token: "comment"}
91
+ ],
92
+ // The meta property contains global information about the mode. It
93
+ // can contain properties like lineComment, which are supported by
94
+ // all modes, and also directives like dontIndentStates, which are
95
+ // specific to simple modes.
96
+ meta: {
97
+ dontIndentStates: ["comment"],
98
+ lineComment: "//"
53
-
99
+ }
100
+ ```

1

ミス

2020/12/17 09:06

投稿

aiai8976
aiai8976

スコア112

title CHANGED
File without changes
body CHANGED
@@ -11,9 +11,7 @@
11
11
 
12
12
  ### 発生している問題・エラーメッセージ
13
13
 
14
- ```
15
- ーメッセージ
14
+ ハイイトが付かない
16
- ```
17
15
 
18
16
  ### 該当のソースコード
19
17
 
@@ -34,7 +32,7 @@
34
32
  fn: {
35
33
  start: [
36
34
  {
37
- regex: /@/,
35
+ regex: /@/, //例えば@をハイライトさせたい場合
38
36
  },
39
37
  ],
40
38
  },