|
| 1 | +--- |
| 2 | +title: "Docs::Site generation" |
| 3 | +layout: docs.liquid |
| 4 | +data: |
| 5 | + route: site-gen |
| 6 | +--- |
| 7 | +## Site generation |
| 8 | + |
| 9 | +[`cobalt serve`](/docs/usage) and `cobalt build` process your entire source directory the same |
| 10 | +way. Both write asset files, transformed files, and generated pages to an output |
| 11 | +directory; `cobalt serve` writes to a temporary directory while `cobalt build` |
| 12 | +writes to a [`destination`](/docs/config) directory (default `./_site`). `cobalt serve` also |
| 13 | +runs a web server to serve this static content, and then continues to watch the |
| 14 | +source directory for changes. |
| 15 | + |
| 16 | +### Generation process |
| 17 | + |
| 18 | +The generation process is, approximately, |
| 19 | +1. Cobalt considers every file in the [`source`](/docs/config)directory. |
| 20 | +2. It skips directories and file names beginning with dot `.` or underscore |
| 21 | + `_`, and other files and directories. You can specify additional files and |
| 22 | + directories to skip using the [`ignore`](/docs/config) configuration |
| 23 | + variable. |
| 24 | +3. It treats all remaining files that aren't pages and posts (see later) as |
| 25 | + [assets](/docs/assets) and copies them to the output directory. It may |
| 26 | + preprocess Sass files to turn them into CSS. |
| 27 | +4. If a file has an extension in [`template_extensions`](/docs/config) (the |
| 28 | + default extensions are `.html` and `.md`), then Cobalt treats it as a |
| 29 | + [page](/docs/pages): |
| 30 | + - Cobalt looks for a [frontmatter](/docs/front) section (lines between a pair |
| 31 | + of `---`) in it, if any, and evaluates metadata settings in it. |
| 32 | + - If the site [configuration](/docs/configuration) or the page's |
| 33 | + frontmatter specifies a [layout](/docs/layout), Cobalt wraps the file in |
| 34 | + that layout's Liquid template. |
| 35 | + - Cobalt evaluates any [liquid template](https://shopify.github.io/liquid/) |
| 36 | + expressions in the file (to simplify, directives inside curly braces `{...}`). |
| 37 | + - Cobalt writes the transformed file to the output directory with, by |
| 38 | + default, a `.html` extension. A file with no frontmatter or liquid |
| 39 | + template expressions will be unchanged. |
| 40 | +5. If a file with a template extension is in the [`posts`](/docs/posts) |
| 41 | + directory, Cobalt processes it further, such as draft handling. A page's |
| 42 | + frontmatter can direct Cobalt to generate a list of pages, for example a |
| 43 | + paginated list of blog posts. |
| 44 | +6. Cobalt may generates additional files, such as an `rss.xml` feed. |
| 45 | + |
| 46 | +### Generating from an existing static site |
| 47 | + |
| 48 | +You can run `cobalt serve` or `cobalt build` in your existing static site's |
| 49 | +directory. |
| 50 | +- They will create a lot of files in the output directory, but will not create |
| 51 | + new files or modify existing files in your source directory. _Caution_, |
| 52 | + `cobalt build` will overwrite files in an existing destination directory |
| 53 | + (which defaults to `./_site`). |
| 54 | +- The commands will complain if you don't have a `_config.yml` file or specify |
| 55 | + the path to one, nevertheless Cobalt will do default processing. |
| 56 | +- Even if your existing files have extensions that Cobalt treats as pages, as |
| 57 | + explained in "Generation process" they will not be transformed if they don't |
| 58 | + contain liquid templates and a frontmatter section, and you don't specify a |
| 59 | + default [`layout`](/docs/config). |
| 60 | +- Some assets that you want to appear in the output directory, such as |
| 61 | + `.htaccess` files and the `.well-known` directory, may not appear; |
| 62 | + conversely, unintended files may appear. |
| 63 | + |
| 64 | +To start adjusting Cobalt's site generation you need to provide a |
| 65 | +[configuration](/docs/config) file. Running [`cobalt init`](docs/usage/) will |
| 66 | +create a `_config.yml` file and a few sample files and layout. If files |
| 67 | +already exist with the same names, `cobalt init` will not overwrite them but |
| 68 | +fail, so you could run it in your existing site directory, or you can run it |
| 69 | +in an empty directory and copy over files as needed. You can specify the path |
| 70 | +to your configuration file with the `--config` _\<FILE>_ command-line option. |
0 commit comments