From 89746ec589f8ed34037a1f5ffa05ab41406f104e Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Thu, 26 Aug 2021 01:28:22 +0900 Subject: MDN/Structures/Code_examples を更新 (#2105) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 2021/02/20 時点の英語版に同期 --- files/ja/mdn/structures/code_examples/index.html | 203 +++++++---------------- 1 file changed, 57 insertions(+), 146 deletions(-) (limited to 'files/ja/mdn/structures/code_examples') diff --git a/files/ja/mdn/structures/code_examples/index.html b/files/ja/mdn/structures/code_examples/index.html index 4cfd04e729..1cf5b391dd 100644 --- a/files/ja/mdn/structures/code_examples/index.html +++ b/files/ja/mdn/structures/code_examples/index.html @@ -1,8 +1,9 @@ --- -title: コード例 +title: コードサンプル slug: MDN/Structures/Code_examples tags: - Code + - Examples - Landing - Live - MDN Meta @@ -14,205 +15,115 @@ translation_of: MDN/Structures/Code_examples ---
{{MDNSidebar}}
-
{{IncludeSubnav("/ja/docs/MDN")}}
+

MDN では、ウェブプラットフォームの機能の使い方を示すために、ページ中に数多くのコードサンプルが挿入されています。この記事では、ページにコードサンプルを追加するためのさまざまな仕組みと、どのような場合にどのような仕組みを使うべきかについて説明します。

-

MDN には、Web プラットフォーム機能の使用方法を示すために、多数のコード例がページ全体に挿入されています。この記事では、コード例をページに追加する際に使用できるさまざまなメカニズムと、使用する必要があるものとそのタイミングについて説明します。

+

利用できるコードサンプルの種類

-

どのような種類のコード例が利用できますか?

- -

There are four types of code example available on MDN:

+

MDN には 4 種類のコードサンプルがあります。

-

We'll discuss each one in later sections.

+

それぞれの機能については後のセクションで説明します。

-

いつそれらを使うべきですか?

+

使うべき場面

-

Each type of code example has it own use cases. When should you use each one?

+

コードサンプルの種類ごとに、それぞれの用途があります。どのような場合に使用するのでしょうか。

-

If you are not sure which one to use, you should default to traditional or GitHub live samples, depending on which one you are most comfortable with. You are also welcome to ask for advice on our Discourse forum.

+

どちらを使うべきか迷った場合は、まず従来型か GitHub のライブサンプルか、使いやすい方を検討してください。助けを求めることpも歓迎します。

-

一般的なガイドライン

+

一般的なガイドライン

-

Aside from the specific system for presenting the live samples, as summarized above, there are style and content cconsiderations to keep in mind when adding or updating samples on MDN?

+

MDN でサンプルを追加・更新する際には、上記のようなライブサンプルを表示するための具体的なシステムの他に、スタイルやコンテンツについても考慮する必要があります。

-

静的サンプル

+

静的サンプル

-

By static examples, we are talking about static code blocks that show how a feature might be used in code. These are put on a page using the PRE and Syntax Highlighter buttons on the MDN editor UI. An example result might look like this:

+

静的サンプルでは、ある機能がコードでどのように使われるかを示す静的なコードブロックです。コードの構文強調で説明したように、 <pre> 要素を使ってページに配置します。結果の例は次のようになります。

-
// This is a JS example
+
// これは JS の例です
 var test = "Hello";
 console.log(test);
-
-

Note: For more details on adding code blocks to MDN pages, see our Syntax highlighting article.

-
- -

Optionally, you might want to show a static image of the code's resulting output. For example:

- -

- -
-

Note: For more details on adding images to MDN pages, see our Images article.

-
- -

従来のライブサンプル

- -

Traditional live samples are inserted into the page using the EmbedLiveSample macro. An \{{EmbedLiveSample}} call dynamically grabs the code blocks in the same document section as itself and puts them into a document, which it then inserts into the page inside an {{htmlelement("iframe")}}. This is most easily demonstrated with an example, so let's look at one in this section and the next.

- -
    -
  1. The easiest way is to press the Insert Code Sample Template button, which asks us for a title. For the example in the "{{anch("test")}}" section below, we entered "test" for the title, and the button generated the entire section for us.
  2. -
  3. Next, we entered some very simple sample code into the HTML, CSS, and JavaScript code blocks.
  4. -
  5. Finally, we published the page; the \{{EmbedLiveSample('test')}} call you can see in the edit view was replaced with an <iframe> containing the code running live.
  6. -
- -

If you look at the source inside the <iframe>, you'll see this:

- -
<!DOCTYPE html>
-<html>
-  <head>
-    <link href="https://developer.mozilla.org/static/build/styles/samples.css"
-          rel="stylesheet" type="text/css">
-
-    <style type="text/css">
-      h1 {
-        color: blue;
-      }
-    </style>
-  </head>
-  <body>
-    <h1>Your example?</h1>
-
-
-    <script type="text/javascript">
-      document.querySelector('h1').onclick = function() {
-        document.querySelector('h1').textContent = 'Your example?';
-      };
-    </script>
-
-  </body>
-</html>
- -

その他のマクロパラメータ

+

任意で、次のように出力結果を静的な画像で示すことができます。

-

The call we've used in the below example only uses one parameter — \{{EmbedLiveSample('test')}}. This simply defines which section of the document the code blocks should be grabbed from — test is the ID defined on the heading "test" below, so the macro will will grab all the code blocks inside that heading. Put another way, it will grab all the blocks below that heading, up until another {{htmlelement("h2")}} is encountered.

+

-

There are some other optional parameters available too. You can include a second and third parameter, which will be a set width and height for the <iframe>. For example \{{EmbedLiveSample('test', '100%', '100px')}}. You can use pixel values or percentage values. Reasonable defaults are used if you omit these.

+

従来型ライブサンプル

-

There are also optional fourth and fifth parameters available, a screenshot URL that points to a screenshot showing the example should look like, and a page slug that points to another page on MDN —this is only used if you want to embed an example from another page. You won't use these two very often.

+

従来型ライブサンプルは、 EmbedLiveSample マクロを使ってページに挿入します。\{{EmbedLiveSample}} を呼び出すと、自分と同じ文書の節にあるコードブロックを動的に取得して文書に入れ、それを {{htmlelement("iframe")}} の中のページに挿入します。詳しくはライブサンプルガイドをご覧ください。

-

See EmbedLiveSample macro for more details on all these parameters.

+

GitHub ライブサンプル

-

伝統的なライブサンプルを使用するためのヒント

+

GitHub ライブサンプルは、 EmbedGHLiveSample マクロを使ってページに挿入します。\{{EmbedGHLiveSample}} を呼び出すと、指定した URL (GitHub の mdn 組織内のものでなければなりません) の文書を動的に取得し、ページ内の {{htmlelement("iframe")}} 内に挿入します。

- - -
-

Note: You can find a lot more information about traditional Live samples in our Live samples article.

-
- -

テスト

- -

HTML

- -
<h1>My example?</h1>
- -

CSS

- -
h1 {
-  color: blue;
-}
+

これらは従来のライブサンプルとよく似た動作をしますが、よりシンプルなものです。

-

JavaScript

+

ページ上のコードブロックの配置を気にする必要はありません。 GitHub リポジトリーの HTML 文書を取得して、<iframe> の中に挿入します。

-
document.querySelector('h1').onclick = function() {
-  document.querySelector('h1').textContent = 'Your example?';
-};
- -

結果

- -

{{EmbedLiveSample('test')}}

- -

GitHub ライブサンプル

- -

GitHub live samples are inserted into the page using the EmbedGHLiveSample macro. An \{{EmbedGHLiveSample}} call dynamically grabs the document at a specified URL (which has to be inside the mdn GitHub organization), and inserts into the page inside an {{htmlelement("iframe")}}.

- -

These work in a very similar way to the {{anch("Traditional live samples")}}, but they are a lot simpler:

- -

You don't have to worry about placement of code blocks on the page — it simply grabs an HTML document in a GitHub repo, and puts it in the <iframe>.

- -

The macro only has three parameters:

+

マクロの引数は 3 つだけです。

    -
  1. The URL of the document to embed — this is relative to the mdn organization, the top level directory of which is at https://mdn.github.io/. So this parameter needs to contain the part of the URL after that, e.g. my-subdirectory/example.html. You can omit the filename if it is called index.html.
  2. -
  3. The width of the <iframe>, which can be expressed as a percentage or in pixels.
  4. -
  5. The height of the <iframe>, which can be expressed as a percentage or in pixels.
  6. +
  7. 埋め込む文書の URL — これは、 https://mdn.github.io/ にある最上位のディレクトリである mdn 組織からの相対 URL です。ですから、この引数には、my-subdirectory/example.html のように、URL の後の部分を含める必要があります。 index.html の場合は、ファイル名を省略できます。
  8. +
  9. <iframe> の幅、パーセント値またはピクセル単位で表すことができます。
  10. +
  11. <iframe> の高さ、パーセント値またはピクセル単位で表すことができます。
-

Let's look at an example. Say we wanted to embed the code at https://mdn.github.io/learning-area/css/styling-boxes/backgrounds/. We could use the following call:

+

例を見てみましょう。 https://mdn.github.io/learning-area/css/styling-boxes/backgrounds/ のコードを埋め込みたいとします。次のように呼び出すことができます。

\{{EmbedGHLiveSample("learning-area/css/styling-boxes/backgrounds/", '100%', 100)}}

-

This looks like so when rendered:

+

表示されるときには次のようになります。

{{EmbedGHLiveSample("learning-area/css/styling-boxes/backgrounds/", '100%', 100)}}

-

GitHub ライブサンプルを使用するためのヒント

+

GitHub ライブサンプルを使用するためのヒント

-

インタラクティブな例

+

インタラクティブサンプル

-

The newest form of live example available on MDN is interactive live examples. These provide a step up from live examples, because the reader can edit the code and the live example updates on the fly. This is great for learning and experimentation.

+

MDN で利用できる最新のライブサンプルは、インタラクティブなライブサンプルです。これは、読者がコードを編集すると、ライブサンプルがその場で更新されるため、ライブサンプルからステップアップすることできます。これは、学習や実験に最適です。

-

The interactive examples are intended to be used at the top of MDN reference pages — we are aiming to provide these to improve their value to beginners and other readers who want to just grab and play with an example quickly before seeing all the details of whatever they are looking up. There are a few important limitations to note about the interactive examples:

+

インタラクティブなサンプルは、 MDN のリファレンスページの上部で使用されることを想定しています。初心者や、調べていることの詳細を見る前に、すぐに例題を手に取って遊びたいという読者にとっての価値を高めるために、これらを提供することを目指しています。インタラクティブサンプルについては、いくつかの重要な制限事項があります。

-

If you want to submit an example, you can find out how at the interactive examples repo Contribution guide.

+

サンプルを投稿したい場合は、 interactive examples repo Contribution guide でその方法を知ることができます。

-

If you find a page that doesn't have an associated interactive example, you are welcome to contribute one! The MDN Discourse forum is a good place to ask for help or advice.

+

関連するインタラクティブサンプルがないページを見つけた場合は、ぜひ投稿してください。

-

インタラクティブなサンプルデモ

+

インタラクティブサンプルのデモ

-

The \{{EmbedInteractiveExample}} macro is used to embed finished examples into MDN pages. For example, the macro call \{{EmbedInteractiveExample("pages/js/array-push.html")}} displays the following code example:

+

EmbedInteractiveExample マクロを使用して、完成したサンプルを MDN ページに埋め込みます。たとえば、 \{{EmbedInteractiveExample("pages/js/array-push.html")}} というマクロ呼び出しで、次のコードサンプルが表示されます。

{{EmbedInteractiveExample("pages/js/array-push.html")}}
-
 
- -
Try adjusting the code to see what happens, and playing with the controls.
+
コードを調整して様子を見たり、操作をしたりしてみてください。
-- cgit v1.2.3-54-g00ecf