angular-cn/aio/content/guide/docs-style-guide.md

1362 lines
46 KiB
Markdown
Raw Normal View History

# Angular documentation style guide
<!-- formerly Authors Style Guide -->
2017-06-09 15:07:08 -04:00
This style guide is for anyone who contributes to the Angular documentation (this site).
These guidelines should be followed by all authors.
Deviations must be approved by a documentation editor.
2017-06-09 15:07:08 -04:00
The guidelines described here serve two purposes:
* To ensure a high-quality, consistent experience for Angular documentation users.
* To simplify the writing process for contributing authors.
This guide helps you make decisions about tone, voice, and style.
It also helps you find the right markup quickly.
<div class="alert is-helpful">
This guide is a *living document*; it changes over time.
We strive for consistency to the extent feasible, but you may find parts of our documentation that don't match this style guide.
When in doubt, **follow this guide rather than imitating existing documents.**
</div>
## Scope of these guidelines
We ask all contributing authors to adhere to three aspects of style:
* **Writing style:** Word usage, grammar, capitalization, and punctuation.
Adherence to Angular's writing guidelines ensures a consistent "voice", helps ensure accuracy of the information, and facilitates use world-wide, by audiences with different backgrounds.
* **Markup style:** How to include images, tables, alert boxes, and code snippets.
Angular docs are written in Markdown, with custom extensions for this site. Correct markup ensures a consistent look-and-feel, and is essential for the doc to build and function correctly.
* **Angular coding style:** Coding style for example apps and code snippets.
Code examples are encouraged for demonstrating how to apply the concepts and features discussed.
Angular has a custom framework that enables authors to include code snippets directly from example apps that are automatically tested as part of doc builds.
To contribute example code, you must understand Angular itself and the custom framework for Angular doc examples.
For each aspect of style, the following table explains where to find the primary guidelines and what this Angular Documentation Style Guide offers.
Style | Guidelines
------------------------ | -------------------------------
**Writing style** | Primary: [Google Developer Documentation Style Guide](https://developers.google.com/style/)<br />This guide: Specifies any special considerations for Angular docs.
**Markup style** | Primary: This guide<br />This guide: Specifies guidelines for markup of guides and tutorials, which are written primarily in Markdown.
**Angular coding style** | Primary: [Angular Style Guide](guide/styleguide "Angular Application Code Style Guide").<br />This guide: How to create, store, and include code examples in guides and tutorials.
<div class="alert is-helpful">
Note: Angular API and CLI reference docs are generated from source code and/or related source files, which may have other markup styles and other ways of including code examples.
</div>
2017-06-09 15:07:08 -04:00
## Doc generation and tooling
To make changes to the documentation pages and sample code, clone the [Angular github repository](https://github.com/angular/angular "Angular repo") and go to the `aio/` folder.
The [aio/README.md](https://github.com/angular/angular/blob/master/aio/README.md "AIO ReadMe") explains how to install and use the tools to edit and test your changes.
Here are a few essential commands for guide page authors.
1. `yarn setup` &mdash; installs packages; builds docs, stackblitz, and zips.
1. `yarn docs-watch --watch-only` &mdash; watches for saved content changes and refreshes the browser. The (optional) `--watch-only` flag skips the initial docs rebuild.
1. `yarn start` &mdash; starts the doc viewer application so you can see your local changes in the browser.
1. http://localhost:4200/ &mdash; browse to the app running locally.
You can combine `yarn docs-watch` and `yarn start` into one command with `yarn serve-and-sync`.
2017-06-20 02:32:30 -04:00
## Guide pages
2017-06-09 15:07:08 -04:00
All but a few guide pages are [markdown](https://daringfireball.net/projects/markdown/syntax "markdown") files with an `.md` extension.
2017-06-09 15:07:08 -04:00
Every guide page file is stored in the `content/guide` directory. Although the [side navigation](#navigation) panel displays as a hierarchy, the directory is flat with no sub-folders.
2017-06-20 02:32:30 -04:00
The flat folder approach allows us to shuffle the apparent navigation structure without moving page files or redirecting old page URLs.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
The doc generation process consumes the markdown files in the `content/guide` directory and produces JSON files in the `src/generated/docs/guide` directory, which is also flat. Those JSON files contain a combination of document metadata and HTML content.
2017-06-09 15:07:08 -04:00
The reader requests a page by its Page URL. The doc viewer fetches the corresponding JSON file, interprets it, and renders it as fully-formed HTML page.
2017-06-09 15:07:08 -04:00
Page URLs mirror the `content` file structure. The URL for the page of a guide is in the form `guide/{page-name}`. The page for _this_ "Authors Style Guide" is located at `content/guide/docs-style-guide.md` and its URL is `guide/docs-style-guide`.
2017-06-09 15:07:08 -04:00
<div class="alert is-helpful">
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
_Tutorial_ pages are exactly like guide pages. The only difference is that they reside in `content/tutorial` instead of `content/guide` and have URLs like `tutorial/{page-name}`.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
_API_ pages are generated from Angular source code into the `src/generated/docs/api` directory.
The doc viewer translates URLs that begin `api/` into requests for document JSON files in that directory. This style guide does not discuss creation or maintenance of API pages.
2017-06-09 15:07:08 -04:00
_Marketing_ pages are similar to guide pages. They're located in the `content/marketing` directory. While they can be markdown files, they may be static HTML pages or dynamic HTML pages that render with JSON data.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Only a few people are authorized to write marketing pages. This style guide does not discuss creation or maintenance of marketing pages.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
</div>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
## Markdown and HTML
While documentation guide pages ultimately render as HTML, almost all of them are written in [markdown](https://daringfireball.net/projects/markdown/syntax "markdown").
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Markdown is easier to read and to edit than HTML. Many editors (including Visual Studio Code) can render markdown as you type it.
2017-06-09 15:07:08 -04:00
From time to time you'll have to step away from markdown and write a portion of the document in HTML. Markdown allows you to mix HTML and markdown in the same document.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Standard markdown processors don't allow you to put markdown _within_ HTML tags. But the Angular documentation markdown processor **supports markdown within HTML**, as long as you follow one rule:
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
<div class="alert is-critical">
**Always** follow every opening and closing HTML tag with _a blank line_.
2017-06-09 15:07:08 -04:00
</div>
```html
2017-06-20 02:32:30 -04:00
<div class="alert is-critical">
2017-06-09 15:07:08 -04:00
**Always** follow every opening and closing HTML tag with _a blank line_.
2017-06-20 02:32:30 -04:00
</div>
```
2017-06-09 15:07:08 -04:00
<div class="alert is-helpful">
2017-06-09 15:07:08 -04:00
It is customary but not required to _precede_ the _closing HTML_ tag with a blank line as well.
2017-06-09 15:07:08 -04:00
</div>
2017-06-20 02:32:30 -04:00
## Title
Every guide document must have a title.
The title should appear at the top of the physical page.
Begin the title with the markdown `#` character. Alternatively, you can write the equivalent `<h1>`.
```html
# Angular documentation style guide
2017-06-09 15:07:08 -04:00
```
2017-06-20 02:32:30 -04:00
**Only one title (`<h1>`) per document!**
Title text should be in "Sentence case", which means the first word is capitalized and all other words are lower case (unless they are technical terms that are always capitalized, like "Angular").
2017-06-20 02:32:30 -04:00
```html
# Deprecation policy in Angular
2017-06-20 02:32:30 -04:00
```
**Always follow the title with at least one blank line.**
Note that the corresponding left-nav TOC text should be in "title case", which means that you use capital letters to start the first words and all principal words. Use lower case letters for secondary words such as "in", "of", and "the". The TOC title can also be shortened to fit in the column.
2017-06-20 02:32:30 -04:00
## Sections
A typical document is divided into sections.
2017-06-09 15:07:08 -04:00
All heading text should be in "Sentence case", which means the first word is capitalized and all other words are lower case.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
**Always follow the section heading with at least one blank line.**
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
<h2 class="no-toc">
Main section heading
</h2>
There are usually one or more main sections that may be further divided into secondary sections.
Begin a main section heading with the markdown `##` characters. Alternatively, you can write the equivalent `<h2>` HTML tag.
2017-06-20 02:32:30 -04:00
The main section heading should be followed by a blank line and then the content for that heading.
2017-06-09 15:07:08 -04:00
```html
2017-06-20 02:32:30 -04:00
## Sections
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
A typical document is divided into sections.
```
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
<h3 class="no-toc">
Secondary section heading
2017-06-09 15:07:08 -04:00
</h3>
2017-06-20 02:32:30 -04:00
A secondary section heading is related to a main heading and _falls textually within_ the bounds of that main heading.
Begin a secondary heading with the markdown `###` characters. Alternatively, you can write the equivalent `<h3>` HTML tag.
The secondary heading should be followed by a blank line and then the content for that heading.
```html
### Secondary section heading
A secondary section ...
2017-06-09 15:07:08 -04:00
```
2017-06-20 02:32:30 -04:00
#### Additional section headings
Try to minimize the heading depth, preferably only two. But more headings, such as this one, are permitted if they make sense.
**N.B.**: The [Table-of-contents](#table-of-contents) generator only considers main (`<h2>`) and secondary (`<h3>`) headings.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
```html
#### Additional section headings
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Try to minimize ...
```
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
## Table of contents
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
Most pages display a table of contents (TOC). The TOC appears in the right panel when the viewport is wide. When narrow, the TOC appears in an expandable/collapsible region near the top of the page.
2017-06-09 15:07:08 -04:00
You should not create your own TOC by hand. The TOC is generated automatically from the page's main and secondary section headers.
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
To exclude a heading from the TOC, create the heading as an `<h2>` or `<h3>` element with a class called 'no-toc'. You can't do this with markdown.
2017-06-14 17:39:15 -04:00
2017-06-09 15:07:08 -04:00
```html
2017-06-20 02:32:30 -04:00
<h3 class="no-toc">
This heading is not displayed in the TOC
</h3>
```
You can turn off TOC generation for the _entire_ page by writing the title with an `<h1>` tag and the `no-toc` class.
```html
<h1 class="no-toc">
A guide without a TOC
</h1>
```
## Navigation
The navigation links at the top, left, and bottom of the screen are generated from the JSON configuration file, `content/navigation.json`.
The authority to change the `navigation.json` file is limited to a few core team members.
2017-06-20 02:32:30 -04:00
But for a new guide page, you should suggest a navigation title and position in the left-side navigation panel called the "side nav".
Look for the `SideNav` node in `navigation.json`. The `SideNav` node is an array of navigation nodes. Each node is either an _item_ node for a single document or a _header_ node with child nodes.
Find the header for your page. For example, a guide page that describes an Angular feature is probably a child of the `Fundamentals` header.
```html
{
"title": "Fundamentals",
"tooltip": "The fundamentals of Angular",
"children": [ ... ]
}
2017-06-09 15:07:08 -04:00
```
2017-06-20 02:32:30 -04:00
A _header_ node child can be an _item_ node or another _header_ node. If your guide page belongs under a sub-header, find that sub-header in the JSON.
Add an _item_ node for your guide page as a child of the appropriate _header_ node. It probably looks something like this one.
```html
{
"url": "guide/architecture",
"title": "Architecture",
"tooltip": "The basic building blocks of Angular applications."
}
```
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
A navigation node has the following properties:
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
* `url`- the URL of the guide page (_item node only_).
* `title`- the text displayed in the side nav.
* `tooltip` - text that appears when the reader hovers over the navigation link.
* `children` - an array of child nodes (_header node only_).
* `hidden` - defined and set true if this is a guide page that should _not_ be displayed in the navigation panel. Rarely needed, it is a way to hide the page from navigation while making it available to readers who should know about it. _This_ "Authors Style Guide" is a hidden page.
<div class="alert is-critical">
Do not create a node that is both a _header_ and an _item_ node. That is, do not specify the `url` property of a _header_ node.
</div>
<div class="alert is-critical">
The current guidelines allow for a three-level navigation structure with two header levels. Don't add a third header level.
</div>
## Code snippets
Guides are rich in examples of working Angular code. Example code can be commands entered in a terminal window, a fragment of TypeScript or HTML, or an entire code file.
Whatever the source, the doc viewer renders them as "code snippets", either individually with the [_code-example_](#code-example "code-example") component or as a tabbed collection with the [_code-tabs_](#code-tabs "code-tabs") component.
{@a code-example}
### Code example
2017-06-20 02:32:30 -04:00
You can display a simple, inline code snippet with the markdown backtick syntax.
Use a single backtick on either side of a term when referring to code or the
name of a file in a sentence.
The following are some examples:
* In the `app.component.ts`, add a `logger()` method.
* The `name` property is `Sally`.
* Add the component class name to the `declarations` array.
The markdown is as follows:
```markdown
* In the `app.component.ts`, add a `logger()` method.
* The <code class="no-auto-link">item</code> property is `true`.
* Add the component class name to the `declarations` array.
```
In certain cases, when you apply backticks around a term, it may auto-link to
the API documentation. If you do not intend the term to be a link, use the following
syntax:
```html
The <code class="no-auto-link">item</code> property is `true`.
```
For block code snippets, we generally prefer to display code with
the Angular documentation _code-example_ component represented by the `<code-example>` tag.
The `<code-example>` tag has a `header` attribute that you use to identify the file that the example comes from. The header should be used whenever possible to establish the context of the example.
See [Code snippets and code examples](guide/docs-style-guide#code-snippets-and-code-samples) for more details.
2017-06-20 02:32:30 -04:00
<h3 class="no-toc">Inline code-snippets</h3>
You should source code snippets [from working sample code](#from-code-samples) when possible.
But there are times when an inline snippet is the better choice.
For terminal input and output, put the content between `<code-example>` tags, set the CSS class to `code-shell`, and set the language attribute to `sh` as in this example.
2017-06-14 17:39:15 -04:00
2017-06-09 15:07:08 -04:00
<code-example language="sh" class="code-shell">
npm start
</code-example>
2017-06-14 17:39:15 -04:00
2017-06-09 15:07:08 -04:00
```html
<code-example language="sh" class="code-shell">
npm start
</code-example>
```
2017-06-20 02:32:30 -04:00
Inline, hand-coded snippets like this one are _not_ testable and, therefore, are intrinsically unreliable.
This example belongs to the small set of pre-approved, inline snippets that includes
user input in a command shell or the _output_ of some process.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
**Do not write inline code snippets** unless you have a good reason and the editor's permission to do so.
In all other cases, code snippets should be generated automatically from tested code samples.
2017-06-14 17:39:15 -04:00
For hypothetical examples such as illustrations of configuration options in a JSON file, you should still use The `<code-example>` tag with the `header` attribute to identify the context.
2017-06-20 02:32:30 -04:00
{@a from-code-samples}
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
<h3 class="no-toc">Code snippets and code samples</h3>
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
One of the documentation design goals is that guide page code snippets should be examples of real, working code.
2017-06-14 17:39:15 -04:00
We meet this goal by displaying code snippets that are derived directly from standalone code samples, written specifically for these guide pages.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
The author of a guide page is responsible for the code sample that supports that page.
The author must also write end-to-end tests for the sample.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Code samples are located in sub-folders of the `content/examples` directory of the `angular/angular` repository. An example folder name should be the same as the guide page it supports.
2017-06-09 15:07:08 -04:00
<div class="alert is-helpful">
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
A guide page might not have its own sample code. It might refer instead to a sample belonging to another page.
</div>
The Angular CI process runs all end-to-end tests for every Angular PR. Angular re-tests the samples after every new version of a sample and every new version of Angular itself.
When possible, every snippet of code on a guide page should be derived from a code sample file. You tell the Angular documentation engine which code file - or fragment of a code file - to display by configuring `<code-example>` attributes.
#### Code snippet from a file
_This_ "Authors Doc Style Guide" has its own sample application, located in the `content/examples/docs-style-guide` folder.
The following _code-example_ displays the sample's `app.module.ts`.
<code-example path="docs-style-guide/src/app/app.module.ts" header="src/app/app.module.ts"></code-example>
2017-06-20 02:32:30 -04:00
Here's the brief markup that produced that lengthy snippet:
2017-06-14 17:39:15 -04:00
2017-06-09 15:07:08 -04:00
```html
<code-example
path="docs-style-guide/src/app/app.module.ts"
header="src/app/app.module.ts">
</code-example>
2017-06-09 15:07:08 -04:00
```
You identified the snippet's source file by setting the `path` attribute to sample folder's location _within_ `content/examples`.
In this example, that path is `docs-style-guide/src/app/app.module.ts`.
2017-06-09 15:07:08 -04:00
You added a header to tell the reader where to find the file by setting the `header` attribute.
Following convention, you set the `header` attribute to the file's location within the sample's root folder.
2017-06-09 15:07:08 -04:00
<div class="alert is-helpful">
2017-06-09 15:07:08 -04:00
Unless otherwise noted, all code snippets in this page are derived from sample source code
2017-06-20 02:32:30 -04:00
located in the `content/examples/docs-style-guide` directory.
2017-06-09 15:07:08 -04:00
</div>
<div class="alert is-important">
The doc tooling reports an error if the file identified in the path does not exist **or is _git_-ignored**.
Most `.js` files are _git_-ignored.
If you want to include an ignored code file in your project and display it in a guide you must _un-ignore_ it.
The preferred way to un-ignore a file is to update the `content/examples/.gitignore` like this:
<code-example header="content/examples/.gitignore">
# my-guide
!my-guide/src/something.js
!my-guide/more-javascript*.js
</code-example>
</div>
#### Code-example attributes
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
You control the _code-example_ output by setting one or more of its attributes:
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
* `path`- the path to the file in the `content/examples` folder.
2017-06-09 15:07:08 -04:00
* `header`- the header of the code listing.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
* `region`- displays the source file fragment with that region name; regions are identified by _docregion_ markup in the source file, as explained [below](#region "Displaying a code fragment").
2017-06-14 17:39:15 -04:00
* `linenums`- value may be `true`, `false`, or a `number`. When not specified, line numbers default to `false` (i.e. no line numbers are displayed). The rarely used `number` option starts line numbering at the given value. `linenums=4` sets the starting line number to 4.
2017-06-20 02:32:30 -04:00
* `class`- code snippets can be styled with the CSS classes `no-box`, `code-shell`, and `avoid`.
* `hideCopy`- hides the copy button
* `language`- the source code language such as `javascript`, `html`, `css`, `typescript`, `json`, or `sh`. This attribute only works for inline examples.
{@a region}
#### Displaying a code fragment
Often you want to focus on a fragment of code within a sample code file. In this example, you focus on the `AppModule` class and its `NgModule` metadata.
<code-example
path="docs-style-guide/src/app/app.module.ts"
region="class">
</code-example>
2017-06-20 02:32:30 -04:00
First you surround that fragment in the source file with a named _docregion_ as described [below](#source-code-markup).
Then you reference that _docregion_ in the `region` attribute of the `<code-example>` like this
2017-06-09 15:07:08 -04:00
```html
<code-example
path="docs-style-guide/src/app/app.module.ts"
region="class">
</code-example>
2017-06-09 15:07:08 -04:00
```
2017-06-20 02:32:30 -04:00
A couple of observations:
1. The `region` value, `"class"`, is the name of the `#docregion` in the source file. Confirm that by looking at `content/examples/docs-style-guide/src/app/app.module.ts`
2017-06-20 02:32:30 -04:00
1. Omitting the `header` is fine when the source of the fragment is obvious. We just said that this is a fragment of the `app.module.ts` file which was displayed immediately above, in full, with a header.
2017-06-20 02:32:30 -04:00
There's no need to repeat the header.
#### Example of bad code
Sometimes you want to display an example of bad code or bad design.
You should be careful. Readers don't always read carefully and are likely to copy and paste your example of bad code in their own applications. So don't display bad code often.
2017-06-20 02:32:30 -04:00
When you do, set the `class` to `avoid`. The code snippet will be framed in bright red to grab the reader's attention.
Here's the markup for an "avoid" example in the
2017-06-20 02:32:30 -04:00
[_Angular Style Guide_](guide/styleguide#style-05-03 "Style 05-03: components as elements").
2017-06-09 15:07:08 -04:00
```html
<code-example
path="styleguide/src/05-03/app/heroes/shared/hero-button/hero-button.component.avoid.ts"
region="example"
header="app/heroes/hero-button/hero-button.component.ts">
2017-06-20 02:32:30 -04:00
</code-example>
```
2017-06-09 15:07:08 -04:00
<code-example path="styleguide/src/05-03/app/heroes/shared/hero-button/hero-button.component.avoid.ts" region="example" header="app/heroes/hero-button/hero-button.component.ts">
2017-06-20 02:32:30 -04:00
</code-example>
{@a code-tabs}
### Code Tabs
2017-06-09 15:07:08 -04:00
2017-10-30 14:13:04 -04:00
Code tabs display code much like _code examples_ do. The added advantage is that they can display multiple code samples within a tabbed interface. Each tab is displayed using _code pane_.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
#### Code-tabs attributes
2017-06-14 17:39:15 -04:00
* `linenums`: The value can be `true`, `false` or a number indicating the starting line number. If not specified, it defaults to `false`.
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
#### Code-pane attributes
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
* `path` - a file in the content/examples folder
* `header` - seen in the header of a tab
* `linenums` - overrides the `linenums` property at the `code-tabs` level for this particular pane. The value can be `true`, `false` or a number indicating the starting line number. If not specified, it defaults to `false`.
2017-06-14 17:39:15 -04:00
The next example displays multiple code tabs, each with its own header.
It demonstrates control over display of line numbers at both the `<code-tabs>` and `<code-pane>` levels.
2017-06-14 17:39:15 -04:00
<code-tabs linenums="true">
<code-pane
header="app.component.html"
path="docs-style-guide/src/app/app.component.html">
2017-06-14 17:39:15 -04:00
</code-pane>
<code-pane
header="app.component.ts"
path="docs-style-guide/src/app/app.component.ts"
linenums="false">
2017-06-14 17:39:15 -04:00
</code-pane>
<code-pane
header="app.component.css (heroes)"
path="docs-style-guide/src/app/app.component.css"
region="heroes">
2017-06-14 17:39:15 -04:00
</code-pane>
<code-pane
header="package.json (scripts)"
path="docs-style-guide/package.1.json">
2017-06-14 17:39:15 -04:00
</code-pane>
</code-tabs>
Here's the markup for that example.
2017-06-09 15:07:08 -04:00
Note how the `linenums` attribute in the `<code-tabs>` explicitly enables numbering for all panes.
The `linenums` attribute in the second pane disables line numbering for _itself only_.
2017-06-09 15:07:08 -04:00
```html
<code-tabs linenums="true">
<code-pane
header="app.component.html"
path="docs-style-guide/src/app/app.component.html">
2017-06-14 17:39:15 -04:00
</code-pane>
<code-pane
header="app.component.ts"
path="docs-style-guide/src/app/app.component.ts"
linenums="false">
2017-06-14 17:39:15 -04:00
</code-pane>
<code-pane
header="app.component.css (heroes)"
path="docs-style-guide/src/app/app.component.css"
region="heroes">
2017-06-14 17:39:15 -04:00
</code-pane>
<code-pane
header="package.json (scripts)"
path="docs-style-guide/package.1.json">
2017-06-14 17:39:15 -04:00
</code-pane>
</code-tabs>
2017-06-09 15:07:08 -04:00
```
2017-06-20 02:32:30 -04:00
{@a source-code-markup}
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
## Source code markup
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
You must add special code snippet markup to sample source code files before they can be displayed by `<code-example>` and `<code-tabs>` components.
2017-06-09 15:07:08 -04:00
<div class="alert is-helpful">
2017-06-20 02:32:30 -04:00
The sample source code for this page, located in `context/examples/docs-style-guide`, contains examples of every code snippet markup described in this section.
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
</div>
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
Code snippet markup is always in the form of a comment. Here's the default _docregion_ markup for a TypeScript or JavaScript file:
```
// #docregion
... some code ...
// #enddocregion
```
Different file types have different comment syntax so adjust accordingly.
2017-06-14 17:39:15 -04:00
2017-06-09 15:07:08 -04:00
```html
2017-06-20 02:32:30 -04:00
<!-- #docregion -->
... some HTML ...
<!-- #enddocregion -->
```
```
/* #docregion */
... some CSS ...
/* #enddocregion */
```
The doc generation process erases these comments before displaying them in the doc viewer.
It also strips them from stackblitz and sample code downloads.
2017-06-20 02:32:30 -04:00
<div class="alert is-important">
Code snippet markup is not supported in JSON files because comments are forbidden in JSON files.
See [below](#json-files) for details and workarounds.
</div>
#### _#docregion_
The _#docregion_ is the most important kind of code snippet markup.
The `<code-example>` and `<code-tabs>` components won't display a source code file unless it has a _#docregion_.
The _#docregion_ comment begins a code snippet region.
Every line of code _after_ that comment belongs in the region _until_ the code fragment processor encounters the end of the file or a closing _#enddocregion_.
<div class="alert is-helpful">
2017-06-20 02:32:30 -04:00
The `src/main.ts` is a simple example of a file with a single _#docregion_ at the top of the file.
<code-example
path="docs-style-guide/src/main.ts"
header="src/main.ts"></code-example>
2017-06-20 02:32:30 -04:00
</div>
#### Named _#docregions_
You'll often display multiple snippets from different fragments within the same file.
You distinguish among them by giving each fragment its own _#docregion name_ as follows.
```
// #docregion region-name
... some code ...
// #enddocregion region-name
```
Remember to refer to this region by name in the `region` attribute of the `<code-example>` or `<code-pane>` as you did in an example above like this:
```html
<code-example
path="docs-style-guide/src/app/app.module.ts"
2017-06-20 02:32:30 -04:00
region="class"></code-example>
```
The _#docregion_ with no name is the _default region_. Do _not_ set the `region` attribute when referring to the default _#docregion_.
#### Nested _#docregions_
You can nest _#docregions_ within _#docregions_
```
// #docregion
2017-06-20 02:32:30 -04:00
... some code ...
// #docregion inner-region
... more code ...
// #enddocregion inner-region
... yet more code ...
/// #enddocregion
```
<div class="alert is-helpful">
2017-06-20 02:32:30 -04:00
The `src/app/app.module.ts` file has a good example of a nested region.
</div>
#### Combining fragments
You can combine several fragments from the same file into a single code snippet by defining
multiple _#docregions_ with the _same region name_.
Examine the `src/app/app.component.ts` file which defines two nested _#docregions_.
The inner, `class-skeleton` region appears twice, once to capture the code that opens the class definition and once to capture the code that closes the class definition.
<code-example>
2017-06-20 02:32:30 -04:00
// #docplaster
...
// #docregion class, class-skeleton
export class AppComponent {
// #enddocregion class-skeleton
title = 'Authors Style Guide Sample';
heroes = HEROES;
selectedHero: Hero;
onSelect(hero: Hero): void {
this.selectedHero = hero;
}
// #docregion class-skeleton
}
// #enddocregion class, class-skeleton
</code-example>
Here's are the two corresponding code snippets displayed side-by-side.
<code-tabs>
<code-pane
header="app.component.ts (class)"
2017-06-20 02:32:30 -04:00
path="docs-style-guide/src/app/app.component.ts"
region="class">
</code-pane>
<code-pane
header="app.component.ts (class-skeleton)"
2017-06-20 02:32:30 -04:00
path="docs-style-guide/src/app/app.component.ts"
region="class-skeleton">
</code-pane>
</code-tabs>
Some observations:
* The `#docplaster` at the top is another bit of code snippet markup. It tells the processor how to join the fragments into a single snippet.
2017-06-20 02:32:30 -04:00
In this example, we tell the processor to put the fragments together without anything in between - without any "plaster". Most sample files define this _empty plaster_.
If we neglected to add, `#docplaster`, the processor would insert the _default_ plaster - an ellipsis comment - between the fragments. Try removing the `#docplaster` comment yourself to see the effect.
* One `#docregion` comment mentions **_two_** region names as does an `#enddocregion` comment. This is a convenient way to start (or stop) multiple regions on the same code line. You could have put these comments on separate lines and many authors prefer to do so.
#### JSON files
Code snippet markup is not supported for JSON files because comments are forbidden in JSON files.
You can display an entire JSON file by referring to it in the `src` attribute.
2017-06-20 02:32:30 -04:00
But you can't display JSON fragments because you can't add `#docregion` tags to the file.
If the JSON file is too big, you could copy the nodes-of-interest into markdown backticks.
Unfortunately, it's easy to mistakenly create invalid JSON that way. The preferred way is to create a JSON partial file with the fragment you want to display.
You can't test this partial file and you'll never use it in the application. But at least your IDE can confirm that it is syntactically correct.
Here's an example that excerpts certain scripts from `package.json` into a partial file named `package.1.json`.
<code-example
2017-06-20 02:32:30 -04:00
path="docs-style-guide/package.1.json"
header="package.json (selected scripts)"></code-example>
2017-06-20 02:32:30 -04:00
```html
<code-example
2017-06-20 02:32:30 -04:00
path="docs-style-guide/package.1.json"
header="package.json (selected scripts)"></code-example>
2017-06-09 15:07:08 -04:00
```
2017-06-20 02:32:30 -04:00
#### Partial file naming
Many guides tell a story. In that story, the app evolves incrementally, often with simplistic or incomplete code along the way.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
To tell that story in code, you'll often need to create partial files or intermediate versions of the final source code file with fragments of code that don't appear in the final app.
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
Such partial and intermediate files need their own names.
Follow the doc sample naming convention. Add a number before the file extension as illustrated here:
2017-06-14 17:39:15 -04:00
2017-06-09 15:07:08 -04:00
```html
2017-06-20 02:32:30 -04:00
package.1.json
app.component.1.ts
app.component.2.ts
2017-06-09 15:07:08 -04:00
```
2017-06-20 02:32:30 -04:00
You'll find many such files among the samples in the Angular documentation.
Remember to exclude these files from stackblitz by listing them in the `stackblitz.json` as illustrated here.
2017-06-20 02:32:30 -04:00
<code-example
path="docs-style-guide/stackblitz.json"
header="stackblitz.json"></code-example>
2017-06-20 02:32:30 -04:00
{@a live-examples}
## Live examples
By adding `<live-example>` to the page you generate links that run sample code in the Stackblitz live coding environment and download that code to the reader's file system.
2017-06-20 02:32:30 -04:00
Live examples (AKA "stackblitz") are defined by one or more `stackblitz.json` files in the root of a code sample folder. Each sample folder usually has a single unnamed definition file, the default `stackblitz.json`.
2017-06-20 02:32:30 -04:00
<div class="alert is-helpful">
2017-06-20 02:32:30 -04:00
You can create additional, named definition files in the form `name.stackblitz.json`. See `content/examples/testing` for examples.
2017-06-20 02:32:30 -04:00
The schema for a `stackblitz.json` hasn't been documented yet but looking at the `stackblitz.json` files in the example folders should tell you most of what you need to know.
2017-06-20 02:32:30 -04:00
</div>
Adding `<live-example></live-example>` to the page generates the two default links.
<live-example></live-example>
1. a link to the Stackblitz defined by the default `stackblitz.json` file located in the code sample folder with the same name as the guide page.
2017-06-20 02:32:30 -04:00
2. a link that downloads that sample.
Clicking the first link opens the code sample on StackBlitz in a new browser tab.
2017-06-20 02:32:30 -04:00
You can change the appearance and behavior of the live example with attributes and classes.
2017-06-14 17:39:15 -04:00
<h3 class="no-toc">Custom label and tooltip</h3>
2017-06-20 02:32:30 -04:00
Give the live example anchor a custom label and tooltip by setting the `title` attribute.
<live-example title="Live Example with title"></live-example>
2017-06-14 17:39:15 -04:00
2017-06-09 15:07:08 -04:00
```html
2017-06-20 02:32:30 -04:00
<live-example title="Live Example with title"></live-example>
2017-06-09 15:07:08 -04:00
```
2017-06-20 02:32:30 -04:00
You can achieve the same effect by putting the label between the `<live-example>` tags:
2017-06-14 17:39:15 -04:00
2017-06-20 02:32:30 -04:00
<live-example>Live example with content label</live-example>
```html
<live-example>Live example with content label</live-example>
```
<h3 class="no-toc">Live example from another guide</h3>
2017-06-20 02:32:30 -04:00
To link to a Stackblitz in a folder whose name is not the same as the current guide page, set the `name` attribute to the name of that folder.
2017-06-20 02:32:30 -04:00
<live-example name="router">Live Example from the Router guide</live-example>
2017-06-14 17:39:15 -04:00
2017-06-09 15:07:08 -04:00
```html
<live-example name="router">Live Example from the Router guide</live-example>
```
<h3 class="no-toc">Live Example for named Stackblitz</h3>
2017-06-20 02:32:30 -04:00
To link to a Stackblitz defined by a named `stackblitz.json` file, set the `stackblitz` attribute. The following example links to the Stackblitz defined by `second.stackblitz.json` in the current guide's directory.
2017-06-20 02:32:30 -04:00
<live-example stackblitz="second"></live-example>
2017-06-20 02:32:30 -04:00
2017-06-09 15:07:08 -04:00
```html
<live-example stackblitz="second"></live-example>
2017-06-09 15:07:08 -04:00
```
<h3 class="no-toc">Live Example without download</h3>
2017-06-20 02:32:30 -04:00
To skip the download link, add the `noDownload` attribute.
2017-06-20 02:32:30 -04:00
<live-example noDownload>Just the Stackblitz</live-example>
2017-06-20 02:32:30 -04:00
2017-06-09 15:07:08 -04:00
```html
<live-example noDownload>Just the Stackblitz</live-example>
2017-06-09 15:07:08 -04:00
```
<h3 class="no-toc">Live Example with download-only</h3>
2017-06-20 02:32:30 -04:00
To skip the live Stackblitz link and only link to the download, add the `downloadOnly` attribute.
2017-06-20 02:32:30 -04:00
<live-example downloadOnly>Download only</live-example>
2017-06-09 15:07:08 -04:00
```html
2017-06-20 02:32:30 -04:00
<live-example downloadOnly>Download only</live-example>
2017-06-09 15:07:08 -04:00
```
<h3 class="no-toc">Embedded live example</h3>
2017-06-20 02:32:30 -04:00
By default, a live example link opens a Stackblitz in a separate browser tab.
You can embed the Stackblitz within the guide page itself by adding the `embedded` attribute.
2017-06-20 02:32:30 -04:00
For performance reasons, the Stackblitz does not start right away. The reader sees an image instead. Clicking the image starts the sometimes-slow process of launching the embedded Stackblitz within an iframe on the page.
2017-06-20 02:32:30 -04:00
Here's an embedded live example for this guide.
2017-06-20 02:32:30 -04:00
2017-06-09 15:07:08 -04:00
```html
<live-example embedded></live-example>
2017-06-09 15:07:08 -04:00
```
<live-example embedded></live-example>
2017-06-20 02:32:30 -04:00
{@a anchors}
2017-06-20 02:32:30 -04:00
2017-06-09 15:07:08 -04:00
## Anchors
2017-06-20 02:32:30 -04:00
Every section header tag is also an anchor point. Another guide page could add a link to this section by writing:
<div class="alert is-helpful">
2017-06-20 02:32:30 -04:00
See the ["Anchors"](guide/docs-style-guide#anchors "Style Guide - Anchors") section for details.
</div>
2017-06-09 15:07:08 -04:00
```html
<div class="alert is-helpful">
2017-06-20 02:32:30 -04:00
See the ["Anchors"](guide/docs-style-guide#anchors "Style Guide - Anchors") section for details.
</div>
2017-06-09 15:07:08 -04:00
```
2017-06-20 02:32:30 -04:00
When navigating within the page, you can omit the page URL when specifying the link that [scrolls up](#anchors "Anchors") to the beginning of this section.
```html
... the link that [scrolls up](#anchors "Anchors") to ...
```
{@a ugly-anchors}
#### Ugly, long section header anchors
It is often a good idea to *lock-in* a good anchor name.
Sometimes the section header text makes for an unattractive anchor. [This one](#ugly-long-section-header-anchors) is pretty bad.
2017-06-20 02:32:30 -04:00
```html
[This one](#ugly-long-section-header-anchors) is pretty bad.
2017-06-20 02:32:30 -04:00
```
The greater danger is that **a future rewording of the header text would break** a link to this section.
For these reasons, it is often wise to add a custom anchor explicitly, just above the heading or
text to which it applies, using the special `{@a name}` syntax like this.
2017-06-20 02:32:30 -04:00
<code-example language="html">
&#123;@a ugly-anchors&#125;
#### Ugly, long section header anchors
2017-06-20 02:32:30 -04:00
</code-example>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Now [link to that custom anchor name](#ugly-anchors) as you did before.
```html
Now [link to that custom anchor name](#ugly-anchors) as you did before.
```
2017-06-09 15:07:08 -04:00
<div class="alert is-helpful">
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Alternatively, you can use the HTML `<a>` tag.
If you do, be sure to set the `id` attribute - not the `name` attribute! The docs generator will not convert the `name` to the proper link URL.
```html
<a id="anchors"></a>
## Anchors
```
</div>
## Alerts and Calllouts
Alerts and callouts present warnings, extra detail or references to other pages. They can also be used to provide commentary that _enriches_ the reader's understanding of the content being presented.
An alert or callout _must not_ contain anything _essential_ to that understanding. Don't put a critical instruction or a tutorial step in a subsection.
### Alerts
Alerts draw attention to short important points. Alerts should not be used for multi-line content (use [callouts](#callouts "callouts") instead).
<div class="alert is-helpful">
You'll learn about styles for live examples in the [section below](guide/docs-style-guide#live-examples "Live examples").
</div>
Note that at least one blank line must follow both the opening and closing `<div>` tags. A blank line before the closing `</div>` is customary but not required.
```html
<div class="alert is-helpful">
2017-06-20 02:32:30 -04:00
You'll learn about styles for live examples in the [section below](guide/docs-style-guide#live-examples "Live examples").
</div>
```
There are three different _urgency levels_ used to style the alerts based on the severity or importance of the content.
2017-06-20 02:32:30 -04:00
2017-06-09 15:07:08 -04:00
<div class="alert is-critical">
2017-06-20 02:32:30 -04:00
A critical alert.
2017-06-09 15:07:08 -04:00
</div>
<div class="alert is-important">
2017-06-20 02:32:30 -04:00
An important alert.
2017-06-09 15:07:08 -04:00
</div>
<div class="alert is-helpful">
2017-06-20 02:32:30 -04:00
A helpful, informational alert.
2017-06-09 15:07:08 -04:00
</div>
2017-06-20 02:32:30 -04:00
Here is the markup for these alerts.
2017-06-09 15:07:08 -04:00
```html
<div class="alert is-critical">
2017-06-20 02:32:30 -04:00
A critical alert.
2017-06-09 15:07:08 -04:00
</div>
<div class="alert is-important">
2017-06-20 02:32:30 -04:00
An important alert.
2017-06-09 15:07:08 -04:00
</div>
<div class="alert is-helpful">
2017-06-20 02:32:30 -04:00
A helpful, informational alert.
2017-06-09 15:07:08 -04:00
</div>
```
### Callouts
Callouts, like alerts, are meant to draw attention to important points. Use a callout when you want a riveting header and multi-line content.
2017-06-09 15:07:08 -04:00
If you have more than two paragraphs, consider creating a new page or making it part of the main content.
2017-06-09 15:07:08 -04:00
Callouts use the same _urgency levels_ that alerts do.
2017-06-09 15:07:08 -04:00
<div class="callout is-critical">
2017-06-20 02:32:30 -04:00
<header>A critical point</header>
2017-06-09 15:07:08 -04:00
**Pitchfork hoodie semiotics**, roof party pop-up _paleo_ messenger messenger bag cred Carles tousled Truffaut yr. Semiotics viral freegan VHS, Shoreditch disrupt McSweeney's. Intelligentsia kale chips Vice four dollar toast
2017-06-09 15:07:08 -04:00
</div>
<div class="callout is-important">
2017-06-20 02:32:30 -04:00
<header>An important point</header>
2017-06-09 15:07:08 -04:00
**Pitchfork hoodie semiotics**, roof party pop-up _paleo_ messenger bag cred Carles tousled Truffaut yr. Semiotics viral freegan VHS, Shoreditch disrupt McSweeney's. Intelligentsia kale chips Vice four dollar toast
2017-06-09 15:07:08 -04:00
</div>
<div class="callout is-helpful">
<header>A helpful or informational point</header>
2017-06-09 15:07:08 -04:00
**Pitchfork hoodie semiotics**, roof party pop-up _paleo_ messenger bag cred Carles tousled Truffaut yr. Semiotics viral freegan VHS, Shoreditch disrupt McSweeney's. Intelligentsia kale chips Vice four dollar toast
2017-06-09 15:07:08 -04:00
</div>
2017-06-20 02:32:30 -04:00
Here is the markup for the first of these callouts.
2017-06-09 15:07:08 -04:00
```html
2017-06-20 02:32:30 -04:00
<div class="callout is-critical">
<header>A critical point</header>
2017-06-09 15:07:08 -04:00
**Pitchfork hoodie semiotics**, roof party pop-up _paleo_ messenger bag cred Carles tousled Truffaut yr. Semiotics viral freegan VHS, Shoreditch disrupt McSweeney's. Intelligentsia kale chips Vice four dollar toast
2017-06-09 15:07:08 -04:00
</div>
```
Notice that:
* the callout header text is forced to all upper case
* the callout body can be written in markdown
* a blank line separates the `</header>` tag from the markdown content
2017-06-20 02:32:30 -04:00
Callouts are meant to grab the user's attention. They are not for casual asides. Please use them sparingly.
2017-06-09 15:07:08 -04:00
## Trees
2017-06-20 02:32:30 -04:00
Trees can represent hierarchical data.
2017-06-09 15:07:08 -04:00
<div class='filetree'>
<div class='file'>
sample-dir
</div>
<div class='children'>
<div class='file'>
src
</div>
<div class='children'>
<div class='file'>
app
</div>
<div class='children'>
<div class='file'>
app.component.ts
</div>
<div class='file'>
app.module.ts
</div>
</div>
<div class='file'>
styles.css
</div>
<div class='file'>
tsconfig.json
</div>
</div>
<div class='file'>
node_modules ...
</div>
<div class='file'>
package.json
</div>
</div>
</div>
2017-06-20 02:32:30 -04:00
Here is the markup for this file tree.
2017-06-09 15:07:08 -04:00
```html
<div class='filetree'>
<div class='file'>
sample-dir
</div>
<div class='children'>
<div class='file'>
src
</div>
<div class='children'>
<div class='file'>
app
</div>
<div class='children'>
<div class='file'>
app.component.ts
</div>
<div class='file'>
app.module.ts
</div>
</div>
<div class='file'>
styles.css
</div>
<div class='file'>
tsconfig.json
</div>
</div>
<div class='file'>
node_modules ...
</div>
<div class='file'>
package.json
</div>
</div>
</div>
```
## Tables
2017-06-20 02:32:30 -04:00
Use HTML tables to present tabular data.
2017-06-09 15:07:08 -04:00
<style>
td, th {vertical-align: top}
</style>
<table>
<tr>
2017-06-20 02:32:30 -04:00
<th>Framework</th>
<th>Task</th>
<th>Speed</th>
</tr>
<tr>
<td><code>AngularJS</code></td>
<td>Routing</td>
<td>Fast</td>
</tr>
<tr>
<td><code>Angular v2</code></td>
<td>Routing</td>
<!-- can use markdown too; remember blank lines -->
<td>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
*Faster*
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
</td>
</tr>
2017-06-09 15:07:08 -04:00
<tr>
2017-06-20 02:32:30 -04:00
<td><code>Angular v4</code></td>
<td>Routing</td>
2017-06-09 15:07:08 -04:00
<td>
2017-06-20 02:32:30 -04:00
**Fastest :)**
2017-06-09 15:07:08 -04:00
</td>
2017-06-20 02:32:30 -04:00
</tr>
</table>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Here is the markup for this table.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
```html
<style>
td, th {vertical-align: top}
</style>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
<table>
<tr>
<th>Framework</th>
<th>Task</th>
<th>Speed</th>
</tr>
<tr>
<td><code>AngularJS</code></td>
<td>Routing</td>
<td>Fast</td>
</tr>
<tr>
<td><code>Angular v2</code></td>
<td>Routing</td>
<!-- can use markdown too; remember blank lines -->
2017-06-09 15:07:08 -04:00
<td>
2017-06-20 02:32:30 -04:00
*Faster*
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
</td>
</tr>
<tr>
<td><code>Angular v4</code></td>
<td>Routing</td>
2017-06-09 15:07:08 -04:00
<td>
2017-06-20 02:32:30 -04:00
**Fastest :)**
2017-06-09 15:07:08 -04:00
</td>
2017-06-20 02:32:30 -04:00
</tr>
</table>
```
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
## Images
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
<h3 class="no-toc">Image location</h3>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Store images in the `content/images` directory in a folder with the same URL as the guide page.
Images for this "Authors Style Guide" page belong in the `content/images/guide/docs-style-guide` folder.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Angular doc generation copies these image folders to the _runtime_ location, `generated/images`.
Set the image `src` attribute to begin in _that_ directory.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Here's the `src` attribute for the "flying hero" image belonging to this page.
```
src="generated/images/guide/docs-style-guide/flying-hero.png"
```
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
<h3 class="no-toc">Use the HTML <i>&lt;img&gt;</i> tag</h3>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
**Do not use the markdown image syntax, \!\[\.\.\.\]\(\.\.\.\).**
2017-06-09 15:07:08 -04:00
Images should be specified in an `<img>` tag.
2017-06-20 02:32:30 -04:00
For accessibility, always set the `alt` attribute with a meaningful description of the image.
2017-06-09 15:07:08 -04:00
You should nest the `<img>` tag within a `<div class="lightbox">` tag, which styles the image within a drop-shadow frame. You'll need the editor's permission to skip the `lightbox` class on its `div` encapsulation.
2017-06-20 02:32:30 -04:00
Here's a conforming example
<div class="lightbox">
<img src="generated/images/guide/docs-style-guide/flying-hero.png"
alt="flying hero">
</div>
2017-06-09 15:07:08 -04:00
```html
<div class="lightbox">
<img src="generated/images/guide/docs-style-guide/flying-hero.png"
alt="flying hero">
</div>
2017-06-09 15:07:08 -04:00
```
2017-06-20 02:32:30 -04:00
_Note that the HTML image element does not have a closing tag._
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
<h3 class="no-toc">Image dimensions</h3>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
The doc generator reads the image dimensions from the file and adds width and height attributes to the `img` tag automatically. If you want to control the size of the image, supply your own width and height attributes.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Here's the "flying hero" at a more reasonable scale.
2017-06-09 15:07:08 -04:00
<div class="lightbox">
<img src="generated/images/guide/docs-style-guide/flying-hero.png"
alt="flying Angular hero"
width="200">
</div>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
```html
2017-06-09 15:07:08 -04:00
<div class="lightbox">
<img src="generated/images/guide/docs-style-guide/flying-hero.png"
alt="flying Angular hero"
width="200">
</div>
2017-06-20 02:32:30 -04:00
```
2017-06-09 15:07:08 -04:00
Wide images can be a problem. Most browsers try to rescale the image but wide images may overflow the document in certain viewports.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
**Do not set a width greater than 700px**. If you wish to display a larger image, provide a link to the actual image that the user can click on to see the full size image separately as in this example of `source-map-explorer` output from the "Ahead-of-time Compilation" guide:
2017-06-09 15:07:08 -04:00
<a href="generated/images/guide/docs-style-guide/toh-pt6-bundle.png" title="Click to view larger image">
<div class="lightbox">
<img src="generated/images/guide/docs-style-guide/toh-pt6-bundle-700w.png" alt="toh-pt6-bundle" width="300px">
</div>
2017-06-20 02:32:30 -04:00
</a>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
<h3 class="no-toc">Image compression</h3>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Large image files can be slow to load, harming the user experience. Always compress the image.
Consider using an image compression web site such as [tinypng](https://tinypng.com/ "tinypng").
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
<h3 class="no-toc">Floating images</h3>
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
You can float the image to the left or right of text by applying the class="left" or class="right" attributes respectively.
2017-06-09 15:07:08 -04:00
<img src="generated/images/guide/docs-style-guide/flying-hero.png"
alt="flying Angular hero"
2017-06-20 02:32:30 -04:00
width="200"
class="left">
2017-06-20 02:32:30 -04:00
This text wraps around to the right of the floating "flying hero" image.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
Headings and code-examples automatically clear a floating image. If you need to force a piece of text to clear a floating image, add `<br class="clear">` where the text should break.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
<br class="clear">
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
The markup for the above example is:
2017-06-09 15:07:08 -04:00
```html
<img src="generated/images/guide/docs-style-guide/flying-hero.png"
alt="flying Angular hero"
2017-06-20 02:32:30 -04:00
width="200"
class="left">
2017-06-20 02:32:30 -04:00
This text wraps around to the right of the floating "flying hero" image.
Headings and code-examples automatically clear a floating image. If you need to force a piece of text to clear a floating image, add `<br class="clear">` where the text should break.
2017-06-09 15:07:08 -04:00
<br class="clear">
```
2017-06-20 02:32:30 -04:00
Note that you generally don't wrap a floating image in a `<figure>` element.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
#### Floating within a subsection
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
If you have a floating image inside an alert, callout, or a subsection, it is a good idea to apply the `clear-fix` class to the `div` to ensure that the image doesn't overflow its container. For example:
2017-06-09 15:07:08 -04:00
<div class="alert is-helpful clear-fix">
2017-06-09 15:07:08 -04:00
<img src="generated/images/guide/docs-style-guide/flying-hero.png"
alt="flying Angular hero"
2017-06-20 02:32:30 -04:00
width="100"
class="right">
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
A subsection with **markdown** formatted text.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
</div>
2017-06-09 15:07:08 -04:00
```html
<div class="alert is-helpful clear-fix">
2017-06-09 15:07:08 -04:00
<img src="generated/images/guide/docs-style-guide/flying-hero.png"
alt="flying Angular hero"
2017-06-20 02:32:30 -04:00
width="100"
class="right">
2017-06-20 02:32:30 -04:00
A subsection with **markdown** formatted text.
2017-06-09 15:07:08 -04:00
2017-06-20 02:32:30 -04:00
</div>
```