From 4e2bd173932c231697a17a3098dc22ef3e481525 Mon Sep 17 00:00:00 2001 From: Tony Sullivan Date: Tue, 1 Nov 2022 16:20:04 +0000 Subject: [PATCH] Adds a Hackernews example site (#5213) * adds the hackernews example - TODO add readme content * refactor: moving styles from root.css into components * chore: add README content * chore: lint fixes + prettier-plugin-astro@0.4.0 * Update examples/hackernews/README.md Co-authored-by: Sarah Rainsberger * lint: remove unused variable * nit: adding check command to example Co-authored-by: Sarah Rainsberger --- examples/hackernews/.gitignore | 20 ++++ examples/hackernews/.vscode/extensions.json | 4 + examples/hackernews/.vscode/launch.json | 11 ++ examples/hackernews/README.md | 57 ++++++++++ examples/hackernews/astro.config.mjs | 10 ++ examples/hackernews/package.json | 18 +++ examples/hackernews/public/favicon.svg | 13 +++ examples/hackernews/sandbox.config.json | 11 ++ .../hackernews/src/components/Comment.astro | 59 ++++++++++ examples/hackernews/src/components/For.astro | 21 ++++ examples/hackernews/src/components/Nav.astro | 97 ++++++++++++++++ examples/hackernews/src/components/Show.astro | 9 ++ .../hackernews/src/components/Story.astro | 77 +++++++++++++ .../hackernews/src/components/Toggle.astro | 78 +++++++++++++ examples/hackernews/src/env.d.ts | 1 + examples/hackernews/src/layouts/Layout.astro | 35 ++++++ examples/hackernews/src/lib/api.ts | 24 ++++ .../hackernews/src/pages/[...stories].astro | 105 ++++++++++++++++++ .../hackernews/src/pages/stories/[id].astro | 96 ++++++++++++++++ .../hackernews/src/pages/users/[id].astro | 69 ++++++++++++ examples/hackernews/src/types.ts | 27 +++++ examples/hackernews/tsconfig.json | 3 + package.json | 2 +- pnpm-lock.yaml | 16 ++- 24 files changed, 858 insertions(+), 5 deletions(-) create mode 100644 examples/hackernews/.gitignore create mode 100644 examples/hackernews/.vscode/extensions.json create mode 100644 examples/hackernews/.vscode/launch.json create mode 100644 examples/hackernews/README.md create mode 100644 examples/hackernews/astro.config.mjs create mode 100644 examples/hackernews/package.json create mode 100644 examples/hackernews/public/favicon.svg create mode 100644 examples/hackernews/sandbox.config.json create mode 100644 examples/hackernews/src/components/Comment.astro create mode 100644 examples/hackernews/src/components/For.astro create mode 100644 examples/hackernews/src/components/Nav.astro create mode 100644 examples/hackernews/src/components/Show.astro create mode 100644 examples/hackernews/src/components/Story.astro create mode 100644 examples/hackernews/src/components/Toggle.astro create mode 100644 examples/hackernews/src/env.d.ts create mode 100644 examples/hackernews/src/layouts/Layout.astro create mode 100644 examples/hackernews/src/lib/api.ts create mode 100644 examples/hackernews/src/pages/[...stories].astro create mode 100644 examples/hackernews/src/pages/stories/[id].astro create mode 100644 examples/hackernews/src/pages/users/[id].astro create mode 100644 examples/hackernews/src/types.ts create mode 100644 examples/hackernews/tsconfig.json diff --git a/examples/hackernews/.gitignore b/examples/hackernews/.gitignore new file mode 100644 index 0000000000..7329a851d0 --- /dev/null +++ b/examples/hackernews/.gitignore @@ -0,0 +1,20 @@ +# build output +dist/ +.output/ + +# dependencies +node_modules/ + +# logs +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* + + +# environment variables +.env +.env.production + +# macOS-specific files +.DS_Store diff --git a/examples/hackernews/.vscode/extensions.json b/examples/hackernews/.vscode/extensions.json new file mode 100644 index 0000000000..22a15055d6 --- /dev/null +++ b/examples/hackernews/.vscode/extensions.json @@ -0,0 +1,4 @@ +{ + "recommendations": ["astro-build.astro-vscode"], + "unwantedRecommendations": [] +} diff --git a/examples/hackernews/.vscode/launch.json b/examples/hackernews/.vscode/launch.json new file mode 100644 index 0000000000..d642209762 --- /dev/null +++ b/examples/hackernews/.vscode/launch.json @@ -0,0 +1,11 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "command": "./node_modules/.bin/astro dev", + "name": "Development server", + "request": "launch", + "type": "node-terminal" + } + ] +} diff --git a/examples/hackernews/README.md b/examples/hackernews/README.md new file mode 100644 index 0000000000..aeb16745ab --- /dev/null +++ b/examples/hackernews/README.md @@ -0,0 +1,57 @@ +# Astro Starter Kit: Hackernews + +``` +npm create astro@latest -- --template hackernews +``` + +[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/withastro/astro/tree/latest/examples/hackernews) + +> 🧑‍🚀 **Seasoned astronaut?** Delete this file. Have fun! + +## 🚀 Project Structure + +Inside of your Astro project, you'll see the following folders and files: + +``` +/ +├── public/ +│ └── favicon.svg +├── src/ +│ ├── components/ +│ ├── layouts/ +│ │ └── Layout.astro +│ └── pages/ + └── stories/ + └── [id].astro + └── users/ + └── [id].astro +│ └── [...stories].astro +└── package.json +``` + +Astro looks for `.astro` or `.md` files in the `src/pages/` directory. Each page is exposed as a route based on its file name. Because the list of stories and users is always changing, dynamic routes like `[id].astro` are used to build pages when a specific page is requested. + +There's nothing special about `src/components/`, but that's where we like to put any Astro/React/Vue/Svelte/Preact components. + +Any static assets, like images, can be placed in the `public/` directory. + +## Server-side rendering (SSR) + +This project uses the [`@astrojs/node`](https://docs.astro.build/en/guides/integrations-guide/node/) adapter to deploy the SSR site to Node targets. Check out Astro's [deployment docs](https://docs.astro.build/en/guides/deploy/) for details on other adapters and hosting environments. + +## 🧞 Commands + +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/` | +| `npm run preview` | Preview your build locally, before deploying | +| `npm run astro ...` | Run CLI commands like `astro add`, `astro check` | +| `npm run astro --help` | Get help using the Astro CLI | + +## 👀 Want to learn more? + +Feel free to check [our documentation](https://docs.astro.build) or jump into our [Discord server](https://astro.build/chat). diff --git a/examples/hackernews/astro.config.mjs b/examples/hackernews/astro.config.mjs new file mode 100644 index 0000000000..68ba7fac58 --- /dev/null +++ b/examples/hackernews/astro.config.mjs @@ -0,0 +1,10 @@ +import { defineConfig } from 'astro/config'; +import node from '@astrojs/node'; + +// https://astro.build/config +export default defineConfig({ + output: 'server', + adapter: node({ + mode: 'standalone', + }), +}); diff --git a/examples/hackernews/package.json b/examples/hackernews/package.json new file mode 100644 index 0000000000..ae79e4bfda --- /dev/null +++ b/examples/hackernews/package.json @@ -0,0 +1,18 @@ +{ + "name": "@example/hackernews", + "type": "module", + "version": "0.0.1", + "private": true, + "scripts": { + "dev": "astro dev", + "start": "astro dev", + "check": "astro check && tsc", + "build": "astro build", + "preview": "astro preview", + "astro": "astro" + }, + "dependencies": { + "@astrojs/node": "^2.0.1", + "astro": "^1.5.2" + } +} diff --git a/examples/hackernews/public/favicon.svg b/examples/hackernews/public/favicon.svg new file mode 100644 index 0000000000..0f39062978 --- /dev/null +++ b/examples/hackernews/public/favicon.svg @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/examples/hackernews/sandbox.config.json b/examples/hackernews/sandbox.config.json new file mode 100644 index 0000000000..9178af77d7 --- /dev/null +++ b/examples/hackernews/sandbox.config.json @@ -0,0 +1,11 @@ +{ + "infiniteLoopProtection": true, + "hardReloadOnChange": false, + "view": "browser", + "template": "node", + "container": { + "port": 3000, + "startScript": "start", + "node": "14" + } +} diff --git a/examples/hackernews/src/components/Comment.astro b/examples/hackernews/src/components/Comment.astro new file mode 100644 index 0000000000..50fa3e9e42 --- /dev/null +++ b/examples/hackernews/src/components/Comment.astro @@ -0,0 +1,59 @@ +--- +import For from './For.astro'; +import Show from './Show.astro'; +import Toggle from './Toggle.astro'; +import type { IComment } from '../types.js'; + +export interface Props { + comment: IComment; +} + +const { comment } = Astro.props; +--- + +
  • +
    + {comment.user}{' '} + {comment.time_ago} ago +
    +
    + + + {(comment: IComment) => } + + +
  • + + diff --git a/examples/hackernews/src/components/For.astro b/examples/hackernews/src/components/For.astro new file mode 100644 index 0000000000..b784f8f8a3 --- /dev/null +++ b/examples/hackernews/src/components/For.astro @@ -0,0 +1,21 @@ +--- +import Show from './Show.astro'; + +export interface Props { + each: Iterable; +} + +const { each } = Astro.props; +--- + +{(async function* () { + for await (const value of each) { + let html = await Astro.slots.render('default', [value]); + yield ; + yield '\n'; + } +})()} + + + + diff --git a/examples/hackernews/src/components/Nav.astro b/examples/hackernews/src/components/Nav.astro new file mode 100644 index 0000000000..10bf0f8991 --- /dev/null +++ b/examples/hackernews/src/components/Nav.astro @@ -0,0 +1,97 @@ +--- +interface Link { + href: string; + text: string; +} + +const links: Link[] = [ + { href: '/', text: 'HN' }, + { href: '/new', text: 'New' }, + { href: '/show', text: 'Show' }, + { href: '/ask', text: 'Ask' }, + { href: '/job', text: 'Jobs' }, +]; +--- + +
    + +
    + + diff --git a/examples/hackernews/src/components/Show.astro b/examples/hackernews/src/components/Show.astro new file mode 100644 index 0000000000..7e08877849 --- /dev/null +++ b/examples/hackernews/src/components/Show.astro @@ -0,0 +1,9 @@ +--- +export interface Props { + when: T | number | boolean | undefined | null; +} + +const { when } = Astro.props; +--- + +{!!when ? : } diff --git a/examples/hackernews/src/components/Story.astro b/examples/hackernews/src/components/Story.astro new file mode 100644 index 0000000000..ee43bab17b --- /dev/null +++ b/examples/hackernews/src/components/Story.astro @@ -0,0 +1,77 @@ +--- +import Show from './Show.astro'; +import type { IStory } from '../types.js'; + +export interface Props { + story: IStory; +} + +const { story } = Astro.props; +--- + +
  • + {story.points} + + + + {story.title} + + ({story.domain}) + {story.title} + + +
    + + + by {story.user}{' '} + {story.time_ago}{' '}|{' '} + + {story.comments_count ? `${story.comments_count} comments` : 'discuss'} + + {story.time_ago} + + + +   + {story.type} + +
  • + + diff --git a/examples/hackernews/src/components/Toggle.astro b/examples/hackernews/src/components/Toggle.astro new file mode 100644 index 0000000000..87b686981e --- /dev/null +++ b/examples/hackernews/src/components/Toggle.astro @@ -0,0 +1,78 @@ +--- +export interface Props { + open?: boolean; +} + +const { open = false } = Astro.props; +--- + + + +
      + +
    +
    + + + + diff --git a/examples/hackernews/src/env.d.ts b/examples/hackernews/src/env.d.ts new file mode 100644 index 0000000000..f964fe0cff --- /dev/null +++ b/examples/hackernews/src/env.d.ts @@ -0,0 +1 @@ +/// diff --git a/examples/hackernews/src/layouts/Layout.astro b/examples/hackernews/src/layouts/Layout.astro new file mode 100644 index 0000000000..b1630da8aa --- /dev/null +++ b/examples/hackernews/src/layouts/Layout.astro @@ -0,0 +1,35 @@ +--- +import Nav from '../components/Nav.astro'; +--- + + + + + + + + Astro - Hacker News + + + +