|
| 1 | +--- |
| 2 | +title: Links |
| 3 | +description: Link to pages, sections, and other sites. Write links relative to your file, and every build checks that they still work. |
| 4 | +template: doc |
| 5 | +layout: |
| 6 | + showNav: true |
| 7 | + showToc: true |
| 8 | + showFooter: true |
| 9 | +nav: |
| 10 | + order: 16 |
| 11 | + icon: tabler:link |
| 12 | +--- |
| 13 | + |
| 14 | +# Links |
| 15 | + |
| 16 | +Write a link the way you see your files. PureStack turns it into the right page address and checks it every time the site builds, so a renamed or deleted page never leaves a broken link behind. |
| 17 | + |
| 18 | +## Link to another page |
| 19 | + |
| 20 | +Start from the file you are writing in, just like in your editor's file tree. Take this content folder: |
| 21 | + |
| 22 | +```text |
| 23 | +index.mdx |
| 24 | +guides/ |
| 25 | + index.mdx |
| 26 | + semantic-tones.mdx |
| 27 | + themes.mdx |
| 28 | + components/ |
| 29 | + index.mdx |
| 30 | + buttons.mdx |
| 31 | +``` |
| 32 | + |
| 33 | +From `guides/semantic-tones.mdx`, these links work: |
| 34 | + |
| 35 | +| Write | Opens | |
| 36 | +| --- | --- | |
| 37 | +| `[Themes](./themes)` | `/guides/themes/`, a page in the same folder | |
| 38 | +| `[Buttons](./components/buttons)` | `/guides/components/buttons/`, a page in a subfolder | |
| 39 | +| `[Components](./components/)` | `/guides/components/`, the main page of a subfolder | |
| 40 | +| `[All guides](./)` | `/guides/`, the main page of this folder | |
| 41 | +| `[Home](../)` | `/`, the main page one folder up | |
| 42 | + |
| 43 | +The file extension is optional. `./themes` and `./themes.mdx` open the same page. |
| 44 | + |
| 45 | +A folder's main page is its `index.mdx`, or a file named after the folder, such as `components/components.mdx`. Link to the folder, like `./components/`, and you reach it either way. |
| 46 | + |
| 47 | +## Jump to a section |
| 48 | + |
| 49 | +Add `#` and the section name after the page: |
| 50 | + |
| 51 | +```md |
| 52 | +[Create a skin](./themes#create-a-skin) |
| 53 | +[Back to the top](#links) |
| 54 | +``` |
| 55 | + |
| 56 | +A section name is its heading in lowercase, with hyphens between the words. The heading "Create a skin" becomes `#create-a-skin`. Try it: [create a skin](./themes#create-a-skin). |
| 57 | + |
| 58 | +PureStack checks that the page exists, not the section, so double-check section names after you rename a heading. |
| 59 | + |
| 60 | +## Links in components |
| 61 | + |
| 62 | +The same rule works for `href` on any tag, including components and links they build from a value: |
| 63 | + |
| 64 | +```mdx |
| 65 | +<BtnLink href="./themes" tone="accent">Read about themes</BtnLink> |
| 66 | + |
| 67 | +<BtnLink :href="nextPage">Next</BtnLink> |
| 68 | + |
| 69 | +<a href="../">Back to home</a> |
| 70 | +``` |
| 71 | + |
| 72 | +Links in a shared header or footer start from that header or footer file, so they work on every page that shows them. |
| 73 | + |
| 74 | +## Link outside your content |
| 75 | + |
| 76 | +Anything that starts with `/` or a protocol is used as written: |
| 77 | + |
| 78 | +| Write | Use it for | |
| 79 | +| --- | --- | |
| 80 | +| `/blog/` | Pages served from the same domain but built separately | |
| 81 | +| `https://github.com/PureStackStudio/PureStack` | Other websites | |
| 82 | +| `mailto:hello@example.com` | Email | |
| 83 | + |
| 84 | +PureStack doesn't check these links, because the pages behind them aren't part of your content folder. |
| 85 | + |
| 86 | +You can link your own pages this way too, as in `/guides/themes/`, but a relative link is the better habit: if that page moves, the build tells you. |
| 87 | + |
| 88 | +If the site lives under a base path, such as `/docs`, PureStack adds it to every link for you. Set it once in the [site configuration](./site-config). |
| 89 | + |
| 90 | +## When a link is broken |
| 91 | + |
| 92 | +The build stops and names the file and the link: |
| 93 | + |
| 94 | +```text |
| 95 | +Content link "./themse" in "guides/semantic-tones.mdx" does not match any page. |
| 96 | +``` |
| 97 | + |
| 98 | +While `purestack serve` runs, only that page shows the error, and it updates as soon as you fix the link. Most fixes are one of these: |
| 99 | + |
| 100 | +- **A typo or different capitals.** `./Themes` doesn't find `themes.mdx`. |
| 101 | +- **The page moved.** Write the path again, starting from your file. |
| 102 | +- **The page is built elsewhere.** Start the path at the site root, such as `/blog/`. |
| 103 | + |
| 104 | +## Translated sites |
| 105 | + |
| 106 | +Link pages inside your language folder as usual. If a page isn't translated yet, the link opens the default language's version, so you can translate at your own pace. |
| 107 | + |
| 108 | +## Quick reference |
| 109 | + |
| 110 | +| To reach | Write | |
| 111 | +| --- | --- | |
| 112 | +| A page in the same folder | `./themes` | |
| 113 | +| A page in a subfolder | `./components/buttons` | |
| 114 | +| A folder's main page | `./components/` | |
| 115 | +| The page one folder up | `../` | |
| 116 | +| A section of another page | `./themes#create-a-skin` | |
| 117 | +| A section of this page | `#quick-reference` | |
| 118 | +| A page built separately | `/blog/` | |
| 119 | +| Another website | `https://example.com` | |
| 120 | + |
| 121 | +<AlertBox tone="accent" icon="tabler:bolt" title="Rule of thumb"> |
| 122 | + Link your own pages relative to your file. Use a full path or address for |
| 123 | + everything else. |
| 124 | +</AlertBox> |
0 commit comments