Markdown
Markdownは標準的な記法に加えて、本プロジェクトで独自拡張した仕様があります。
Frontmatter#
必要に応じてページのメタ情報や設定をfrontmatterに書くことができます。frontmatterはYAML形式です。
yaml---slug: /reference/function---
yaml---slug: /reference/function---
利用可能な設定は次のものがあります。
| キー | 型 | 説明 |
|---|---|---|
sidebar_label | string | サイドバーや前後ページナビゲーション、サイト内リンクブロックに表示されるページタイトルです。指定しない場合、最初の見出しの内容がサイドバーに表示されます。 |
slug | string | ページのURLのパス部分。指定しない場合、ファイル名から拡張子を除いたものがslugになります。 |
tags | string[] | ページのタグ。デフォルト値は[]です。 |
description | string | ページの要約。<meta name="description" content="..."/>やサイト内リンクブロックの説明として使われる。指定しない場合、コンテンツの最初の段落がdescriptionになります。 |
title | string | ページのタイトル。これを指定した場合、Markdownに見出しをつける必要はありません。 |
yaml---sidebar_label: アロー関数slug: /reference/functions/arrow-functiontags:- 関数- アロー関数description: TypeScriptでアロー関数を定義する方法---
yaml---sidebar_label: アロー関数slug: /reference/functions/arrow-functiontags:- 関数- アロー関数description: TypeScriptでアロー関数を定義する方法---
見出し#
見出しレベル1#は、ページタイトルにのみ使います。
ページ内のセクションは見出しレベル2##以上を使います。
例markdown# ページタイトル...序文...## 大見出し...### 中見出し...
例markdown# ページタイトル...序文...## 大見出し...### 中見出し...
リンク#
サイト内リンクはMarkdownファイルパスを相対パスで書きます。
markdown詳細は[関数](../references/functions.md)をご覧ください。
markdown詳細は[関数](../references/functions.md)をご覧ください。
内部リンクブロック#
内部リンクブロックは、サイト内のページへのリンクを表示するためのものです。次の例のように、ページタイトルと説明文が自動的に表示されるコンポーネントです。
内部リンクブロックには次の利点があります。
- 関連ページが目立つ
- リンク先ページのタイトル変更に自動的に追従できる
リンクブロックを作るには、前後に空白行を置き、かつ、サイト内リンクの行の前後には何も文字を書かないようにします。
markdown...テキスト...[letとconst](../references/values-types-variables/let-and-const.md)...テキスト...
markdown...テキスト...[letとconst](../references/values-types-variables/let-and-const.md)...テキスト...
caution
Markdownのリンクテキストは無視され、リンク先のタイトルが採用されます。たとえば、function.mdのタイトルが「関数について」で、Markdownが[ファンクション](./function.md)のとき、「ファンクション」は無視され、ウェブサイト上の表示は「関数について」が採用されます。
インラインコード#
インラインコードの前後には空白を置かないようにします。
markdown❌ 変数宣言は `const` を用います。⭕️ 変数宣言は`const`を用います。
markdown❌ 変数宣言は `const` を用います。⭕️ 変数宣言は`const`を用います。
インラインコードでバッククォートを使うには、ダブルバッククォートを使います。
markdownテンプレートリテラルは`` ` ``を使います。
markdownテンプレートリテラルは`` ` ``を使います。
テンプレートリテラルは
`を使います。
コードブロック#
コードブロックは言語名を指定すると、シンタックスハイライトが効きます。
markdown```ts// code```
markdown```ts// code```
使用可能な言語は次のとおりです。
コードブロックのタイトル#
コードブロックにタイトルをつけるにはtitle属性を指定します。
markdown```ts title="sample.ts"// sample code```
markdown```ts title="sample.ts"// sample code```
sample.tsts// sample code
sample.tsts// sample code
行番号#
4行以上あるコードブロックは行番号が自動で付与されます。
markdown1行目2行目3行目
markdown1行目2行目3行目
markdown1行目2行目3行目4行目
markdown1行目2行目3行目4行目
Twoslash#
TwoslashはサンプルコードにTypeScriptコンパイラーから得られる情報を付加する機能です。付加される情報には変数の型、コンパイルエラーのメッセージなどがあります。
変数の型を表示する#
^?を書くと型推論された変数の型の中身を表示できます。
markdown```ts twoslashconst point = { x: 135, y: 35 };// ^?type ReadonlyPoint = Readonly<typeof point>;// ^?```
markdown```ts twoslashconst point = { x: 135, y: 35 };// ^?type ReadonlyPoint = Readonly<typeof point>;// ^?```
表示例tsconstpoint = {x : 135,y : 35 };typeReadonlyPoint =Readonly <typeofpoint >;
表示例tsconstpoint = {x : 135,y : 35 };typeReadonlyPoint =Readonly <typeofpoint >;
エラーを表示する#
@errorsでコンパイルエラーの内容を表示できます。
markdown```ts twoslash// @errors: 7006function fn(s) {}```
markdown```ts twoslash// @errors: 7006function fn(s) {}```
表示例tsfunctionParameter 's' implicitly has an 'any' type.7006Parameter 's' implicitly has an 'any' type.fn () {} s
表示例tsfunctionParameter 's' implicitly has an 'any' type.7006Parameter 's' implicitly has an 'any' type.fn () {} s
コンパイラーオプションを設定する#
@コンパイラーオプション: 設定値の形式で書くと、そのコードブロックでのみ効くコンパイラーオプションを設定できます。
markdown```ts twoslash// @noImplicitAny: falsefunction fn(s) {}```
markdown```ts twoslash// @noImplicitAny: falsefunction fn(s) {}```
表示例tsfunctionfn (s ) {}
表示例tsfunctionfn (s ) {}
実行結果を表示する#
@log、@warn、@errorを用いると、実行結果のコメントをスタイリングして表示できます。
markdown```js twoslashconsole.log(123);// @log: 123console.warn("メッセージ");// @warn: メッセージconst x = value;// @error: ReferenceError: value is not defined```
markdown```js twoslashconsole.log(123);// @log: 123console.warn("メッセージ");// @warn: メッセージconst x = value;// @error: ReferenceError: value is not defined```
表示例jsconsole .log (123);console .warn ("メッセージ");constx =value ;
表示例jsconsole .log (123);console .warn ("メッセージ");constx =value ;
コード補完の再現#
^|を書いたところにVS Codeでのコード補完の様子を再現できます。
markdown```ts twoslash// @noErrors[1, 2, 3].fin// ^|```
markdown```ts twoslash// @noErrors[1, 2, 3].fin// ^|```
表示例ts[1, 2, 3].fin
表示例ts[1, 2, 3].fin
JavaScriptの出力#
@showEmitでコンパイル結果のJavaScriptコードを表示できます。
markdown```ts twoslash title="表示例"// @showEmitenum Example {FOO,BAR,}```
markdown```ts twoslash title="表示例"// @showEmitenum Example {FOO,BAR,}```
表示例ts"use strict";var Example;(function (Example) {Example[Example["FOO"] = 0] = "FOO";Example[Example["BAR"] = 1] = "BAR";})(Example || (Example = {}));
表示例ts"use strict";var Example;(function (Example) {Example[Example["FOO"] = 0] = "FOO";Example[Example["BAR"] = 1] = "BAR";})(Example || (Example = {}));
型定義ファイルの出力#
TypeScriptソースコードを型定義ファイルに変換した結果を表示できます。
markdown```ts twoslash// @declaration: true// @showEmit// @showEmittedFile: index.d.tsexport function getStringLength(value: string) {return value.length;}```
markdown```ts twoslash// @declaration: true// @showEmit// @showEmittedFile: index.d.tsexport function getStringLength(value: string) {return value.length;}```
表示例tsexport declare function getStringLength(value: string): number;
表示例tsexport declare function getStringLength(value: string): number;
インラインハイライト(下線)#
下線^^を引いた部分がハイライトされます。これは未対応で、下線コメントが消えるだけです。
markdown```ts twoslashfunction greet(person: string, date: Date) {console.log(`Hello ${person}, today is ${date.toDateString()}!`);}greet("Maddison", new Date());// ^^^^^^^^^^```
markdown```ts twoslashfunction greet(person: string, date: Date) {console.log(`Hello ${person}, today is ${date.toDateString()}!`);}greet("Maddison", new Date());// ^^^^^^^^^^```
表示例tsfunctiongreet (person : string,date :Date ) {console .log (`Hello ${person }, today is ${date .toDateString ()}!`);}greet ("Maddison", newDate ());
表示例tsfunctiongreet (person : string,date :Date ) {console .log (`Hello ${person }, today is ${date .toDateString ()}!`);}greet ("Maddison", newDate ());
行ハイライト#
特定の行に注目してもらいたいときは、行番号を書くとその行の背景色を変えられます。
markdown```js twoslash {1,4-6,11} title="行ハイライトの表示例"import React from "react";function MyComponent(props) {if (props.isBar) {return <div>Bar</div>;}return <div>Foo</div>;}export default MyComponent;```
markdown```js twoslash {1,4-6,11} title="行ハイライトの表示例"import React from "react";function MyComponent(props) {if (props.isBar) {return <div>Bar</div>;}return <div>Foo</div>;}export default MyComponent;```
行ハイライトの表示例jsimportReact from "react";functionMyComponent (props ) {if (props .isBar ) {return <div >Bar</div >;}return <div >Foo</div >;}export defaultMyComponent ;
行ハイライトの表示例jsimportReact from "react";functionMyComponent (props ) {if (props .isBar ) {return <div >Bar</div >;}return <div >Foo</div >;}export defaultMyComponent ;
サンプルコードの自動整形#
コードブロックはPrettierで自動整形されます。
自動整形をされたくないコードブロック場合は、<!--prettier-ignore-->を直前に書きます。
markdown```tsf = x => x;```<!--prettier-ignore-->```tsf = x => x;```
markdown```tsf = x => x;```<!--prettier-ignore-->```tsf = x => x;```
整形結果markdown```tsf = (x) => x;```<!--prettier-ignore-->```tsf = x => x;```
整形結果markdown```tsf = (x) => x;```<!--prettier-ignore-->```tsf = x => x;```
警告表示#
トリプルコロン:::で囲んだテキストは警告表示にできます。
markdown:::noteテキスト::::::tipテキスト::::::infoテキスト::::::cautionテキスト::::::dangerテキスト:::
markdown:::noteテキスト::::::tipテキスト::::::infoテキスト::::::cautionテキスト::::::dangerテキスト:::
note
テキスト
tip
テキスト
info
テキスト
caution
テキスト
danger
テキスト
警告表示にはタイトルを指定できます。
markdown:::note 好みのタイトルテキスト:::
markdown:::note 好みのタイトルテキスト:::
好みのタイトル
テキスト