|
| 1 | +--- |
| 2 | +layout: docs |
| 3 | +order: 4 |
| 4 | +tocHeading: 2 |
| 5 | +--- |
| 6 | + |
| 7 | +# Frontmatter |
| 8 | + |
| 9 | +[Frontmatter](https://www.npmjs.com/package/front-matter) is a [YAML](https://yaml.org/) block at the top of a file that gives you the ability to define variables that are made available to Greenwood's [build process and then your HTML](/docs/content-as-data/). You can also use it to import additional static assets like JS and CSS files. |
| 10 | + |
| 11 | +Greenwood defines the following properties that you can use in HTML or [markdown](/docs/plugins/markdown/) out of the box: |
| 12 | + |
| 13 | +- Label |
| 14 | +- Title |
| 15 | +- Layout |
| 16 | +- Imports |
| 17 | +- Custom Data |
| 18 | + |
| 19 | +## Label |
| 20 | + |
| 21 | +By default Greenwood will aim to create a label for your page based on filename path, but you can override it if desired. This can be useful if you want to create a custom value to display for a link with custom formatting or text. |
| 22 | + |
| 23 | +<!-- prettier-ignore-start --> |
| 24 | + |
| 25 | +<app-ctc-block variant="snippet"> |
| 26 | + |
| 27 | + ```md |
| 28 | + --- |
| 29 | + label: "My Blog Post from 3/5/2020" |
| 30 | + --- |
| 31 | + |
| 32 | + # My Blog Post |
| 33 | + ``` |
| 34 | + |
| 35 | +</app-ctc-block> |
| 36 | + |
| 37 | +<!-- prettier-ignore-end --> |
| 38 | + |
| 39 | +## Title |
| 40 | + |
| 41 | +To set the `<title>` for a given page, you can customize the **title** variable. Otherwise, the `<title>` will be inferred from the file name. |
| 42 | + |
| 43 | +<!-- prettier-ignore-start --> |
| 44 | + |
| 45 | +<app-ctc-block variant="snippet"> |
| 46 | + |
| 47 | + ```md |
| 48 | + --- |
| 49 | + title: My Blog Post |
| 50 | + --- |
| 51 | + |
| 52 | + # This is a post |
| 53 | + |
| 54 | + The is a markdown file with the title defined in frontmatter. |
| 55 | + ``` |
| 56 | + |
| 57 | +</app-ctc-block> |
| 58 | + |
| 59 | +<!-- prettier-ignore-end --> |
| 60 | + |
| 61 | +In this example, the `<title>` tag will be the value of **title**. |
| 62 | + |
| 63 | +```html |
| 64 | +<title>My Blog Post</title> |
| 65 | +``` |
| 66 | + |
| 67 | +## Layouts |
| 68 | + |
| 69 | +When creating multiple [page layouts](/docs/pages/layouts/), you can use the **layout** frontmatter key to configure Greenwood to use that layout to wrap a given page. |
| 70 | + |
| 71 | +<!-- prettier-ignore-start --> |
| 72 | + |
| 73 | +<app-ctc-block variant="snippet"> |
| 74 | + |
| 75 | + ```md |
| 76 | + --- |
| 77 | + layout: blog |
| 78 | + --- |
| 79 | + |
| 80 | + # My First Blog Post |
| 81 | + |
| 82 | + This is my first blog post, I hope you like it! |
| 83 | + ``` |
| 84 | + |
| 85 | +</app-ctc-block> |
| 86 | + |
| 87 | +<!-- prettier-ignore-end --> |
| 88 | + |
| 89 | +In this example, _src/layouts/blog.html_ will be used to wrap the content of this markdown page. |
| 90 | + |
| 91 | +> **Note:** By default, Greenwood will look for and use _src/layouts/page.html_ for all pages by default. |
| 92 | +
|
| 93 | +## Imports |
| 94 | + |
| 95 | +If you want to include scripts or styles on a _per **page** basis_ (typically when using markdown), you can provide filepaths and attributes using the `imports` key. This is great for one off use cases where you don't want to ship a third party lib in all your layouts, or as a demo for a particular blog post. You can also add attributes by space delimiting them after the path. |
| 96 | + |
| 97 | +<!-- prettier-ignore-start --> |
| 98 | + |
| 99 | +<app-ctc-block variant="snippet"> |
| 100 | + |
| 101 | + ```md |
| 102 | + --- |
| 103 | + imports: |
| 104 | + - /components/my-component/component.js type="module" foo="bar" |
| 105 | + - /components/my-component/component.css |
| 106 | + --- |
| 107 | + |
| 108 | + # My Demo Page |
| 109 | + ``` |
| 110 | + |
| 111 | +</app-ctc-block> |
| 112 | + |
| 113 | +<!-- prettier-ignore-end --> |
| 114 | + |
| 115 | +You will then see the following emitted for file |
| 116 | + |
| 117 | +```html |
| 118 | +<script type="module" src="/components/my-component/component.js" type="module" foo="bar"></script> |
| 119 | +<link rel="stylesheet" href="/components/my-component/component.css" /> |
| 120 | +``` |
| 121 | + |
| 122 | +## Custom Data |
| 123 | + |
| 124 | +You can also define any custom frontmatter property you want and that will be made available on the `data` property of [the page object](/docs/content-as-data/pages-data/). |
| 125 | + |
| 126 | +<!-- prettier-ignore-start --> |
| 127 | + |
| 128 | +<app-ctc-block variant="snippet"> |
| 129 | + |
| 130 | + ```md |
| 131 | + --- |
| 132 | + author: Jon Doe |
| 133 | + date: 04/07/2020 |
| 134 | + --- |
| 135 | + |
| 136 | + # First Post |
| 137 | + |
| 138 | + My first post |
| 139 | + ``` |
| 140 | + |
| 141 | +</app-ctc-block> |
| 142 | + |
| 143 | +<!-- prettier-ignore-end --> |
0 commit comments