From 2e8db7ad2384b756894eac6be72bcf720f7f28fa Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Thu, 26 Aug 2021 15:02:38 -0700 Subject: [PATCH] add algolia and cleanup docs readme --- examples/docs/README.md | 139 ++++++++-- examples/docs/public/theme.css | 10 +- examples/docs/src/components/HeadSEO.astro | 2 +- .../src/components/Header/AstroLogo.astro | 6 +- .../docs/src/components/Header/Header.astro | 56 ++-- .../src/components/Header/LanguageSelect.tsx | 6 +- .../docs/src/components/Header/Search.tsx | 7 +- .../components/LeftSidebar/LeftSidebar.astro | 2 +- examples/docs/src/components/util.ts | 4 - examples/docs/src/config.ts | 38 ++- examples/docs/src/languages.ts | 23 +- examples/docs/src/layouts/Main.astro | 239 ------------------ examples/docs/src/pages/en/getting-started.md | 190 -------------- examples/docs/src/pages/en/index.astro | 5 - examples/docs/src/pages/en/introduction.md | 63 +---- examples/docs/src/pages/en/page-2.md | 50 ++++ .../src/pages/en/{example.md => page-3.md} | 2 +- examples/docs/src/pages/en/page-4.md | 36 +++ examples/docs/src/pages/index.astro | 25 +- 19 files changed, 286 insertions(+), 617 deletions(-) delete mode 100644 examples/docs/src/components/util.ts delete mode 100644 examples/docs/src/layouts/Main.astro delete mode 100644 examples/docs/src/pages/en/getting-started.md delete mode 100644 examples/docs/src/pages/en/index.astro create mode 100644 examples/docs/src/pages/en/page-2.md rename examples/docs/src/pages/en/{example.md => page-3.md} (99%) create mode 100644 examples/docs/src/pages/en/page-4.md diff --git a/examples/docs/README.md b/examples/docs/README.md index 15a852654..75e3b4f97 100644 --- a/examples/docs/README.md +++ b/examples/docs/README.md @@ -4,25 +4,134 @@ npm init astro -- --template docs ``` -> 🧑‍🚀 **Seasoned astronaut?** Delete this file. Have fun! +## Features -Features: +- ✅ **Full Markdown support** +- ✅ **Responsive mobile-friendly design** +- ✅ **Sidebar navigation** +- ✅ **Search (powered by Algolia)** +- ✅ **Multi-language i18n** +- ✅ **Automatic table of contents** +- ✅ **Automatic list of contributors** +- ✅ (and, best of all) **dark mode** -- ✅ CSS Grid Layout -- ✅ Full Markdown support -- ✅ Automatic header navigation sidebar -- ✅ Dark mode enabled by default - -## 🧞 Commands +## Commands Cheatsheet All commands are run from the root of the project, from a terminal: -| Command | Action | -|:----------------|:--------------------------------------------| -| `npm install` | Installs dependencies | -| `npm run dev` | Starts local dev server at `localhost:3000` | -| `npm run build` | Build your production site to `./dist/` | +| Command | Action | +|:-----------------|:---------------------------------------------| +| `npm install` | Installs dependencies | +| `npm run dev` | Starts local dev server at `localhost:3000` | +| `npm run build` | Build your production site to `./dist/` | +| `npm run preview`| Preview your build locally, before deploying | -## 👀 Want to learn more? +To deploy your site to production, check out our [Deploy an Astro Website](https://docs.astro.build/guides/deploy) guide. -Feel free to check [our documentation](https://github.com/snowpackjs/astro) or jump into our [Discord server](https://astro.build/chat). +## New to Astro? + +Welcome! Check out [our documentation](https://github.com/snowpackjs/astro) or jump into our [Discord server](https://astro.build/chat). + +## Customize This Theme + +### Site metadata + +`src/config.ts` contains several data objects that describe metadata about your site like title, description, default language, and Open Graph details. You can customize these to match your project. +### CSS styling + +The theme's look and feel is controlled by a few key variables that you can customize yourself. You'll find them in the `public/theme.css` CSS file. + +If you've never worked with CSS variables before, give [MDN's guide on CSS variables](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties) a quick read. + +This theme uses a "cool blue" accent color by default. To customize this for your project, change the `--theme-accent` variable to whatever color you'd like: + +```diff +/* public/theme.css */ +:root { + color-scheme: light; +- --theme-accent: hsla(var(--color-blue), 1); ++ --theme-accent: hsla(var(--color-red), 1); /* or: hsla(#FF0000, 1); */ +``` + + +### Sidebar navigation + +The sidebar navigation is controlled by the `SIDEBAR` variable in your `src/config.ts` file. You can customize the sidebar by modifying this object. A default, starter navigation has already been created for you. + +```ts +export const SIDEBAR = { + en: [ + { text: 'Section Header', header: true, }, + { text: 'Introduction', link: 'en/introduction' }, + { text: 'Page 2', link: 'en/page-2' }, + { text: 'Page 3', link: 'en/page-3' }, + + { text: 'Another Section', header: true }, + { text: 'Page 4', link: 'en/page-4' }, + ], +}; +``` + +Note the top-level `en` key: This is needed for multi-language support. You can change it to whatever language you'd like, or add new languages as you go. More details on this below. + +### Multiple Languages support + +The Astro docs template supports multiple langauges out of the box. The default theme only shows `en` documentation, but you can enable multi-language support features by adding a second language to your project. + +To add a new language to your project, you'll want to extend the current `src/pages/[lang]/...` layout: + +```diff + 📂 src/pages + ┣ 📂 en + ┃ ┣ 📜 page-1.md + ┃ ┣ 📜 page-2.md + ┃ ┣ 📜 page-3.astro ++ ┣ 📂 es ++ ┃ ┣ 📜 page-1.md ++ ┃ ┣ 📜 page-2.md ++ ┃ ┣ 📜 page-3.astro +``` + +You'll also need to add the new language name to the `KNOWN_LANGUAGES` map in your `src/config.ts` file. This will enable your new language switcher in the site header. + +```diff +// src/config.ts +export const KNOWN_LANGUAGES = { + English: 'en', ++ Spanish: 'es', +}; +``` + +Last step: you'll need to add a new entry to your sidebar, to create the table of contents for that language. While duplicating every page might not sound ideal to everyone, this extra control allows you to create entirely custom content for every language. + +> Make sure the sidebar `link` value points to the correct language! + +```diff +// src/config.ts +export const SIDEBAR = { + en: [ + { text: 'Section Header', header: true, }, + { text: 'Introduction', link: 'en/introduction' }, + // ... + ], ++ es: [ ++ { text: 'Encabezado de sección', header: true, }, ++ { text: 'Introducción', link: 'es/introduction' }, ++ // ... ++ ], +}; + +// ... +``` + +### What if I don't plan to support multiple languages? + +That's totally fine! Not all projects need (or can support) multiple languages. You can continue to use this theme without ever adding a second language. + +### Search (Powered by Algolia) + +[Algolia](https://www.algolia.com/) offers a free service to qualified open source projects called [DocSearch](https://docsearch.algolia.com/). If you are accepted to the DocSearch program, provide your API Key & index name in `src/config.ts` and a search box will automatically appear in your site header. + +Note that Aglolia and Astro are not affiliated. We have no say over acceptance to the DocSearch program. + +If you'd prefer to remove Algolia's search and replace it with your own, check out the `src/components/Header.astro` component to see where the component is added. \ No newline at end of file diff --git a/examples/docs/public/theme.css b/examples/docs/public/theme.css index 587ddf29d..3089840dd 100644 --- a/examples/docs/public/theme.css +++ b/examples/docs/public/theme.css @@ -56,8 +56,8 @@ :root { color-scheme: light; - --theme-accent: hsla(var(--color-orange), 1); - --theme-text-accent: hsla(var(--color-orange), 1); + --theme-accent: hsla(var(--color-blue), 1); + --theme-text-accent: hsla(var(--color-blue), 1); --theme-accent-opacity: 0.1; --theme-divider: hsla(var(--color-gray-95), 1); --theme-text: hsla(var(--color-gray-10), 1); @@ -67,15 +67,15 @@ --theme-bg: hsla(var(--color-base-white), 100%, 1); --theme-bg-hover: hsla(var(--color-gray-95), 1); --theme-bg-offset: hsla(var(--color-gray-90), 1); - --theme-bg-accent: hsla(var(--color-orange), var(--theme-accent-opacity)); + --theme-bg-accent: hsla(var(--color-blue), var(--theme-accent-opacity)); --theme-code-inline-bg: hsla(var(--color-gray-95), 1); --theme-code-inline-text: var(--theme-text); --theme-code-bg: hsla(217, 19%, 27%, 1); --theme-code-text: hsla(var(--color-gray-95), 1); --theme-navbar-bg: hsla(var(--color-base-white), 100%, 1); --theme-navbar-height: 6rem; - --theme-selection-color: hsla(var(--color-orange), 1); - --theme-selection-bg: hsla(var(--color-orange), var(--theme-accent-opacity)); + --theme-selection-color: hsla(var(--color-blue), 1); + --theme-selection-bg: hsla(var(--color-blue), var(--theme-accent-opacity)); } body { diff --git a/examples/docs/src/components/HeadSEO.astro b/examples/docs/src/components/HeadSEO.astro index 5553eb2d0..268809a87 100644 --- a/examples/docs/src/components/HeadSEO.astro +++ b/examples/docs/src/components/HeadSEO.astro @@ -18,7 +18,7 @@ const imageAlt = content?.image?.alt ?? OPEN_GRAPH.image.alt; - + diff --git a/examples/docs/src/components/Header/AstroLogo.astro b/examples/docs/src/components/Header/AstroLogo.astro index ff1939ad9..124865f1b 100644 --- a/examples/docs/src/components/Header/AstroLogo.astro +++ b/examples/docs/src/components/Header/AstroLogo.astro @@ -4,12 +4,10 @@ const {size} = Astro.props;
- {lang && } -
+ {KNOWN_LANGUAGE_CODES.length > 1 && } + {CONFIG.ALGOLIA &&
} \ No newline at end of file diff --git a/examples/docs/src/components/Header/LanguageSelect.tsx b/examples/docs/src/components/Header/LanguageSelect.tsx index cf325eedc..8b9807fe8 100644 --- a/examples/docs/src/components/Header/LanguageSelect.tsx +++ b/examples/docs/src/components/Header/LanguageSelect.tsx @@ -1,7 +1,7 @@ import type { FunctionalComponent } from 'preact'; import { h } from 'preact'; import './LanguageSelect.css'; -import { LANGUAGE_NAMES, langPathRegex } from '../../languages'; +import { KNOWN_LANGUAGES, langPathRegex } from '../../languages'; const LanguageSelect: FunctionalComponent<{ lang: string }> = ({ lang }) => { return ( @@ -23,9 +23,9 @@ const LanguageSelect: FunctionalComponent<{ lang: string }> = ({ lang }) => { window.location.pathname = '/' + newLang + actualDest; }} > - {Object.keys(LANGUAGE_NAMES).map((key) => { + {Object.keys(KNOWN_LANGUAGES).map((key) => { return ( - ); diff --git a/examples/docs/src/components/Header/Search.tsx b/examples/docs/src/components/Header/Search.tsx index d842e007f..19ee513f1 100644 --- a/examples/docs/src/components/Header/Search.tsx +++ b/examples/docs/src/components/Header/Search.tsx @@ -2,7 +2,8 @@ import { useState, useCallback, useRef } from 'react'; import { createPortal } from 'react-dom'; import { DocSearchModal, useDocSearchKeyboardEvents } from '@docsearch/react'; -import '@docsearch/css//dist/style.css'; +import * as CONFIG from '../../config.js'; +import '@docsearch/css/dist/style.css'; import './Search.css'; export default function Search() { @@ -53,8 +54,8 @@ export default function Search() { initialQuery={initialQuery} initialScrollY={window.scrollY} onClose={onClose} - indexName="astro" - apiKey="0f387260ad74f9cbf4353facd29c919c" + indexName={(CONFIG as any).ALGOLIA.indexName} + apiKey={(CONFIG as any).ALGOLIA.apiKey} transformItems={(items) => { return items.map((item) => { // We transform the absolute URL into a relative URL to diff --git a/examples/docs/src/components/LeftSidebar/LeftSidebar.astro b/examples/docs/src/components/LeftSidebar/LeftSidebar.astro index 96bd36fba..e979dc80e 100644 --- a/examples/docs/src/components/LeftSidebar/LeftSidebar.astro +++ b/examples/docs/src/components/LeftSidebar/LeftSidebar.astro @@ -1,6 +1,6 @@ --- +import { getLanguageFromURL } from '../../languages.ts'; import { SIDEBAR } from '../../config.ts'; -import { getLanguageFromURL } from '../util.ts'; const {currentPage} = Astro.props; const currentPageMatch = currentPage.slice(1); const langCode = getLanguageFromURL(currentPage); diff --git a/examples/docs/src/components/util.ts b/examples/docs/src/components/util.ts deleted file mode 100644 index 0ec91bce0..000000000 --- a/examples/docs/src/components/util.ts +++ /dev/null @@ -1,4 +0,0 @@ -export function getLanguageFromURL(pathname: string) { - const langCodeMatch = pathname.match(/\/([a-z]{2}-?[A-Z]{0,2})\//); - return langCodeMatch ? langCodeMatch[1] : 'en'; -} diff --git a/examples/docs/src/config.ts b/examples/docs/src/config.ts index cf0d58ed5..a9001b8ab 100644 --- a/examples/docs/src/config.ts +++ b/examples/docs/src/config.ts @@ -1,22 +1,36 @@ -export const SIDEBAR = { - en: [ - { text: 'Getting Started', header: true }, - { text: 'Introduction', link: 'en/introduction' }, - { text: 'Getting Started', link: 'en/getting-started' }, - { text: 'Example', link: 'en/example' }, - ], -}; - export const SITE = { - title: 'Astro Documentation', - description: 'Build faster websites with less client-side Javascript.', + title: 'Your Documentation Website', + description: 'Your website description.', + defaultLanguage: 'en_US', }; export const OPEN_GRAPH = { - locale: 'en_US', image: { src: 'https://github.com/snowpackjs/astro/blob/main/assets/social/banner.png?raw=true', alt: 'astro logo on a starry expanse of space,' + ' with a purple saturn-like planet floating in the right foreground', }, twitter: 'astrodotbuild', }; + +export const KNOWN_LANGUAGES = { + English: 'en', +}; + +// Uncomment this to enable site search. +// See "Algolia" section of the README for more information. +// export const ALGOLIA = { +// indexName: 'XXXXXXXXXX', +// apiKey: 'XXXXXXXXXX', +// } + +export const SIDEBAR = { + en: [ + { text: 'Section Header', header: true }, + { text: 'Introduction', link: 'en/introduction' }, + { text: 'Page 2', link: 'en/page-2' }, + { text: 'Page 3', link: 'en/page-3' }, + + { text: 'Another Section', header: true }, + { text: 'Page 4', link: 'en/page-4' }, + ], +}; diff --git a/examples/docs/src/languages.ts b/examples/docs/src/languages.ts index e56855631..389c3d7a7 100644 --- a/examples/docs/src/languages.ts +++ b/examples/docs/src/languages.ts @@ -1,19 +1,8 @@ -export const LANGUAGE_NAMES = { - English: 'en', -}; +import { KNOWN_LANGUAGES } from './config.js'; -export const KNOWN_LANGUAGES = Object.values(LANGUAGE_NAMES); -export const langPathRegex = new RegExp(`\/(${KNOWN_LANGUAGES.join('|')})\/`); -export const getLanguageDetails = () => { - // @ts-ignore - let newLangWithRegion = (window.navigator.userLanguage || window.navigator.language || 'en-US').substr(0, 5); - let newLang = newLangWithRegion.substr(0, 2); +export const KNOWN_LANGUAGE_CODES = Object.values(KNOWN_LANGUAGES); - let actualDest = window.location.pathname.replace(langPathRegex, '/'); - return { - newLangWithRegion, - newLang, - langPathRegex, - actualDest, - }; -}; +export function getLanguageFromURL(pathname: string) { + const langCodeMatch = pathname.match(/\/([a-z]{2}-?[A-Z]{0,2})\//); + return langCodeMatch ? langCodeMatch[1] : 'en'; +} diff --git a/examples/docs/src/layouts/Main.astro b/examples/docs/src/layouts/Main.astro deleted file mode 100644 index bce5b1e6e..000000000 --- a/examples/docs/src/layouts/Main.astro +++ /dev/null @@ -1,239 +0,0 @@ ---- -// Component Imports -import ArticleFooter from '../components/ArticleFooter.astro'; -import SiteSidebar from '../components/SiteSidebar.astro'; -import ThemeToggle from '../components/ThemeToggle.tsx'; -import DocSidebar from '../components/DocSidebar.tsx'; - -// Component Script: -// You can write any JavaScript/TypeScript that you'd like here. -// It will run during the build, but never in the browser. -// All variables are available to use in the HTML template below. -const { content } = Astro.props; -const headers = content.astro.headers; -const currentPage = Astro.request.url.pathname; -const currentFile = currentPage === '/' ? 'src/pages/index.md' : `src/pages${currentPage.replace(/\/$/, "")}.md`; -const githubEditUrl = `https://github.com/USER/REPO/blob/main/${currentFile}` - -// Full Astro Component Syntax: -// https://docs.astro.build/core-concepts/astro-components/ ---- - - - {content.title} - - - - -