Skip to content

Commit 90c6dd6

Browse files
authored
Merge pull request #561 from ut-code/auto-type-term-tags
Termタグのtypeを省略できるように
2 parents 9cab4c2 + 6925790 commit 90c6dd6

File tree

4 files changed

+183
-4
lines changed

4 files changed

+183
-4
lines changed

package-lock.json

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"react-dom": "^18.2.0",
2727
"react-icons": "^4.11.0",
2828
"rehype-katex": "^7.0.0",
29-
"remark-math": "^6.0.0"
29+
"remark-math": "^6.0.0",
30+
"react-children-utilities": "^2.9.0"
3031
},
3132
"browserslist": {
3233
"production": [

src/components/Term/index.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@ import Tippy from "@tippyjs/react";
44
import { MdArrowForward } from "react-icons/md";
55
import "tippy.js/dist/tippy.css";
66
import "tippy.js/themes/material.css";
7+
import { onlyText } from "react-children-utilities";
78
import styles from "./styles.module.css";
89
import definitions from "./definitions";
10+
import typeMap from "./type-map.js";
911

1012
/**
1113
* @param {Object} props
12-
* @param {keyof typeof definitions} props.type
14+
* @param {keyof typeof definitions | null} props.type
1315
* @param {React.ReactNode} props.children
1416
*/
15-
export default function Term({ type, children }) {
17+
export default function Term({ type = null, children }) {
18+
if (type === null) type = typeMap.get(onlyText(children));
19+
if (!type)
20+
throw new Error(
21+
`Problem: Term ${onlyText(children)} is not defined in type-map.js .
22+
Solution: explicitly specify term type, or add type definition to type-map.js`,
23+
);
24+
1625
const term = definitions.terms[type];
1726
if (!term) throw new Error(`Type ${type} is not defined.`);
1827
const referencePageTitle =

src/components/Term/type-map.js

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* # term の追加
3+
* > 文字列そのままの Term しか match しないので、
4+
* 説明的な Term は対応していません。
5+
* 例: "演算子の優先順位"、"Truthy と Falsy"
6+
*
7+
* > 名前の同じ Term は、使用頻度の高そうな方を優先してください。
8+
* 例: "プロパティ" -> "javascriptProperty"
9+
*
10+
* > 英数字と日本語の間は半角スペースを入れることになっているので、
11+
* 半角が入っているもののみを記述してください。
12+
* 例: 〇 "HTML 要素" × "HTML要素"
13+
*
14+
*
15+
* # 使い方
16+
* > 説明的な Term (Truthy と Falsy など) や、
17+
* 名前の同じ Term (CSS のプロパティ -> JS のプロパティとして解釈する)
18+
* などは対応していないので、term を明示してください。
19+
* 例: CSS の<Term type="cssProperty">プロパティ</Term> // /docs/
20+
*/
21+
22+
const typeMap = new Map([
23+
["拡張子", "fileExtension"],
24+
["フォーク", "fork"],
25+
["Git", "git"],
26+
["GitHub", "github"],
27+
["Gitpod", "gitpod"],
28+
["リポジトリ", "repository"],
29+
["HTML", "html"],
30+
["JavaScript", "javascript"],
31+
["開始タグ", "startTag"],
32+
["タグ", "tag"],
33+
["属性", "attribute"],
34+
["HTML 要素", "element"],
35+
["要素", "element"],
36+
["終了タグ", "endTag"],
37+
["CSS", "css"],
38+
["style 属性", "styleAttribute"],
39+
["文", "javascriptStatement"],
40+
["値", "javascriptValue"],
41+
["文字列", "javascriptString"],
42+
["演算子", "javascriptOperator"],
43+
["式", "javascriptExpression"],
44+
["評価", "javascriptEvaluation"],
45+
["変数", "javascriptVariable"],
46+
["宣言", "javascriptDeclaration"],
47+
["代入", "javascriptAssignment"],
48+
["キャメルケース", "camelCase"],
49+
["スネークケース", "snakeCase"],
50+
["パスカルケース", "pascalCase"],
51+
["真偽値", "javascriptBoolean"],
52+
["if 文", "javascriptIfStatement"],
53+
["制御構造", "javascriptControlFlow"],
54+
["関数", "javascriptFunction"],
55+
["引数", "javascriptParameter"],
56+
["渡す", "javascriptPass"],
57+
["戻り値", "javascriptReturnValue"],
58+
["返す", "javascriptReturn"],
59+
["スコープ", "javascriptScope"],
60+
["モジュール化", "javascriptModularization"],
61+
["オブジェクト", "javascriptObject"],
62+
["プリミティブ", "javascriptPrimitive"],
63+
["プロパティ", "javascriptProperty"],
64+
["DOM", "dom"],
65+
["イベント", "events"],
66+
["イベントハンドラ", "eventHandler"],
67+
["参照", "javascriptReference"],
68+
["副作用", "javascriptSideEffects"],
69+
["純粋関数", "javascriptPureFunction"],
70+
["参照透過性", "javascriptReferenceTransparency"],
71+
["クラス", "javascriptClass"],
72+
["インスタンス", "javascriptInstance"],
73+
["コンストラクタ", "javascriptConstructor"],
74+
["メソッド", "javascriptMethod"],
75+
["アロー関数", "arrowFunction"],
76+
["コールバック関数", "callBackFunction"],
77+
["モジュール", "javascriptModule"],
78+
["ライブラリ", "library"],
79+
["JSON", "json"],
80+
["クエリ文字列", "queryString"],
81+
["npx コマンド", "npxCommand"],
82+
["非同期処理", "asynchronousProcess"],
83+
["API", "api"],
84+
["スレッド", "thread"],
85+
["トランスパイル", "transpile"],
86+
["モジュールバンドラ", "moduleBundler"],
87+
["JSX", "jsx"],
88+
]);
89+
90+
export default typeMap;
91+
92+
/* for later use // no-more-meaningless-javascript
93+
const typeMap = new Map([
94+
["拡張子", "fileExtension"],
95+
["フォーク", "fork"],
96+
["Git", "git"],
97+
["GitHub", "github"],
98+
["Gitpod", "gitpod"],
99+
["リポジトリ", "repository"],
100+
["HTML", "html"],
101+
["JavaScript", "javascript"],
102+
["開始タグ", "startTag"],
103+
["タグ", "tag"],
104+
["属性", "attribute"],
105+
["HTML 要素", "element"],
106+
["要素", "element"],
107+
["終了タグ", "endTag"],
108+
["CSS", "css"],
109+
["style 属性", "styleAttribute"],
110+
["文", "statement"],
111+
["値", "value"],
112+
["文字列", "string"],
113+
["演算子", "operator"],
114+
["式", "expression"],
115+
["評価", "evaluation"],
116+
["変数", "variable"],
117+
["宣言", "declaration"],
118+
["代入", "assignment"],
119+
["キャメルケース", "camelCase"],
120+
["スネークケース", "snakeCase"],
121+
["パスカルケース", "pascalCase"],
122+
["真偽値", "bool"],
123+
["if 文", "ifStatement"],
124+
["制御構造", "controlFlow"],
125+
["関数", "function"],
126+
["引数", "parameter"],
127+
["渡す", "pass"],
128+
["戻り値", "returnValue"],
129+
["返す", "return"],
130+
["スコープ", "scope"],
131+
["モジュール化", "modularization"],
132+
["オブジェクト", "object"],
133+
["プリミティブ", "primitive"],
134+
["プロパティ", "property"],
135+
["DOM", "dom"],
136+
["イベント", "events"],
137+
["イベントハンドラ", "eventHandler"],
138+
["参照", "reference"],
139+
["副作用", "sideEffects"],
140+
["純粋関数", "pureFunction"],
141+
["参照透過性", "referenceTransparency"],
142+
["クラス", "class"],
143+
["インスタンス", "instance"],
144+
["コンストラクタ", "constructor"],
145+
["メソッド", "method"],
146+
["アロー関数", "arrowFunction"],
147+
["コールバック関数", "callBackFunction"],
148+
["モジュール", "module"],
149+
["ライブラリ", "library"],
150+
["JSON", "json"],
151+
["クエリ文字列", "queryString"],
152+
["npx コマンド", "npxCommand"],
153+
["非同期処理", "asyncProcess"],
154+
["API", "api"],
155+
["スレッド", "thread"],
156+
["トランスパイル", "transpile"],
157+
["モジュールバンドラ", "moduleBundler"],
158+
["JSX", "jsx"],
159+
]);
160+
*/

0 commit comments

Comments
 (0)