mirror of
https://github.com/withastro/astro.git
synced 2025-03-10 23:01:26 -05:00
wip
This commit is contained in:
parent
939886ff47
commit
99c4148ff0
14 changed files with 149 additions and 25 deletions
|
@ -6,7 +6,7 @@ import Pagination from '../../components/Pagination.astro';
|
|||
import authorData from '../../data/authors.json';
|
||||
|
||||
export function getStaticPaths() {
|
||||
const allPosts = Astro.fetchContent<MarkdownFrontmatter>('../post/*.md');
|
||||
const allPosts = Astro.fetchContent<MarkdownFrontmatter>('../posts/*.md');
|
||||
let allAuthorsUnique = [...new Set(allPosts.map((p) => p.author))];
|
||||
return allAuthorsUnique.map((author) => ({ params: { author }, props: { allPosts } }));
|
||||
}
|
|
@ -21,7 +21,7 @@ let description = 'An example blog on Astro';
|
|||
let canonicalURL = Astro.request.canonicalURL;
|
||||
|
||||
// Data Fetching: List all Markdown posts in the repo.
|
||||
let allPosts = Astro.fetchContent<MarkdownFrontmatter>('./post/*.md');
|
||||
let allPosts = Astro.fetchContent<MarkdownFrontmatter>('./posts/*.md');
|
||||
allPosts.sort((a, b) => new Date(b.date).valueOf() - new Date(a.date).valueOf());
|
||||
let firstPage = allPosts.slice(0, 2);
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
import MainHead from '../components/MainHead.astro';
|
||||
import Nav from '../components/Nav.astro';
|
||||
import authorData from '../data/authors.json';
|
||||
import MainHead from '../../components/MainHead.astro';
|
||||
import Nav from '../../components/Nav.astro';
|
||||
import authorData from '../../data/authors.json';
|
||||
|
||||
const { content } = Astro.props;
|
||||
let canonicalURL = Astro.request.canonicalURL;
|
|
@ -6,7 +6,7 @@ import Pagination from '../../components/Pagination.astro';
|
|||
import authorData from '../../data/authors.json';
|
||||
|
||||
export async function getStaticPaths({ paginate, rss }) {
|
||||
const allPosts = Astro.fetchContent<MarkdownFrontmatter>('../post/*.md');
|
||||
const allPosts = Astro.fetchContent<MarkdownFrontmatter>('./*.md');
|
||||
const sortedPosts = allPosts.sort((a, b) => new Date(b.date).valueOf() - new Date(a.date).valueOf());
|
||||
|
||||
// Generate an RSS feed from this collection of posts.
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
layout: ../../layouts/post.astro
|
||||
layout: ./Layout.astro
|
||||
title: Chapter I
|
||||
tag: movie
|
||||
date: 2021-05-17
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
layout: ../../layouts/post.astro
|
||||
layout: ./Layout.astro
|
||||
title: Chapter II
|
||||
tag: movie
|
||||
date: 2021-05-18
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
layout: ../../layouts/post.astro
|
||||
layout: ./Layout.astro
|
||||
title: Chapter III
|
||||
tag: movie
|
||||
date: 2021-05-19
|
82
examples/blog/src/routes/index.route.astro
Normal file
82
examples/blog/src/routes/index.route.astro
Normal file
|
@ -0,0 +1,82 @@
|
|||
---
|
||||
// Component Imports
|
||||
import BaseHead from '../components/BaseHead.astro';
|
||||
import BlogHeader from '../components/BlogHeader.astro';
|
||||
import BlogPostPreview from '../components/BlogPostPreview.astro';
|
||||
|
||||
interface MarkdownFrontmatter {
|
||||
publishDate: number;
|
||||
}
|
||||
|
||||
// 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.
|
||||
let title = 'Example Blog';
|
||||
let description = 'The perfect starter for your perfect blog.';
|
||||
let permalink = 'https://example.com/';
|
||||
|
||||
// Data Fetching: List all Markdown posts in the repo.
|
||||
|
||||
let allPosts = await Astro.fetchContent('./posts/*.md');
|
||||
allPosts = allPosts.sort((a, b) => new Date(b.publishDate).valueOf() - new Date(a.publishDate).valueOf());
|
||||
|
||||
// Full Astro Component Syntax:
|
||||
// https://docs.astro.build/core-concepts/astro-components/
|
||||
---
|
||||
|
||||
<html lang="en">
|
||||
<head>
|
||||
<BaseHead {title} {description} {permalink} />
|
||||
|
||||
<style>
|
||||
header {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: var(--theme-bg-offset);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.content {
|
||||
margin-top: 4rem;
|
||||
margin-bottom: 8rem;
|
||||
}
|
||||
|
||||
.content :global(main > * + *) {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.intro {
|
||||
padding-bottom: 4rem;
|
||||
margin-bottom: 2rem;
|
||||
border-bottom: 4px solid var(--theme-divider);
|
||||
}
|
||||
|
||||
.intro > * {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.latest {
|
||||
font-size: 2.5rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<BlogHeader />
|
||||
<div class="wrapper">
|
||||
<main class="content">
|
||||
<section class="intro">
|
||||
<h1 class="latest">{title}</h1>
|
||||
<p>{description}</p>
|
||||
</section>
|
||||
<section aria-label="Blog post list">
|
||||
{allPosts.map((p) => <BlogPostPreview post={p} />)}
|
||||
</section>
|
||||
</main>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
24
examples/blog/src/routes/posts/Layout.astro
Normal file
24
examples/blog/src/routes/posts/Layout.astro
Normal file
|
@ -0,0 +1,24 @@
|
|||
---
|
||||
import BaseHead from '../../components/BaseHead.astro';
|
||||
import BlogHeader from '../../components/BlogHeader.astro';
|
||||
import BlogPost from '../../components/BlogPost.astro';
|
||||
|
||||
|
||||
const { content } = Astro.props;
|
||||
const { title, description, publishDate, author, heroImage, permalink, alt } = content;
|
||||
---
|
||||
|
||||
<html lang={content.lang || 'en'}>
|
||||
<head>
|
||||
<BaseHead {title} {description} {permalink} />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<BlogHeader />
|
||||
<div class="wrapper">
|
||||
<BlogPost {title} {author} {heroImage} {publishDate} {alt}>
|
||||
<slot />
|
||||
</BlogPost>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
16
examples/blog/src/routes/posts/index.route.md
Normal file
16
examples/blog/src/routes/posts/index.route.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
---
|
||||
setup: |
|
||||
import Layout from './Layout.astro'
|
||||
import Cool from '../../components/Author.astro'
|
||||
title: Hello world!
|
||||
publishDate: 12 Sep 2021
|
||||
name: Nate Moore
|
||||
value: 128
|
||||
description: Just a Hello World Post!
|
||||
---
|
||||
|
||||
<Cool name={frontmatter.name} href="https://twitter.com/n_moore" client:load />
|
||||
|
||||
This is so cool!
|
||||
|
||||
Do variables work {frontmatter.value * 2}?
|
|
@ -24,7 +24,7 @@ export const AstroConfigSchema = z.object({
|
|||
pages: z
|
||||
.string()
|
||||
.optional()
|
||||
.default('./src/pages')
|
||||
.default('./src/routes')
|
||||
.transform((val) => new URL(val)),
|
||||
layouts: z
|
||||
.string()
|
||||
|
@ -97,7 +97,7 @@ export async function validateConfig(userConfig: any, root: string): Promise<Ast
|
|||
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
|
||||
pages: z
|
||||
.string()
|
||||
.default('./src/pages')
|
||||
.default('./src/routes')
|
||||
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
|
||||
layouts: z
|
||||
.string()
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
import type { AstroConfig, ComponentInstance, GetStaticPathsResult, ManifestData, Params, RouteData } from '../../@types/astro';
|
||||
import type { LogOptions } from '../logger';
|
||||
|
||||
import fs from 'fs';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { compile } from 'path-to-regexp';
|
||||
import slash from 'slash';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { warn } from '../logger.js';
|
||||
import { warn, debug, LogOptions } from '../logger.js';
|
||||
|
||||
/**
|
||||
* given an array of params like `['x', 'y', 'z']` for
|
||||
|
@ -94,10 +92,10 @@ export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?
|
|||
const resolved = path.join(dir, basename);
|
||||
const file = slash(path.relative(cwd || fileURLToPath(config.projectRoot), resolved));
|
||||
const isDir = fs.statSync(resolved).isDirectory();
|
||||
|
||||
const ext = path.extname(basename);
|
||||
const name = ext ? basename.slice(0, -ext.length) : basename;
|
||||
|
||||
const [name, ...extParts] = basename.split(/(?<!\.)\.(?!\.)/);
|
||||
const ext = extParts ? `.${extParts[extParts.length - 1]}` : '';
|
||||
const isRoute = extParts && extParts.length === 2 && extParts[0] === 'route' && validExtensions.has(ext);
|
||||
console.log(basename);
|
||||
if (name[0] === '_') {
|
||||
return;
|
||||
}
|
||||
|
@ -108,10 +106,13 @@ export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?
|
|||
if (!isDir && !validExtensions.has(ext)) {
|
||||
return;
|
||||
}
|
||||
const segment = isDir ? basename : name;
|
||||
if (/^\$/.test(segment)) {
|
||||
throw new Error(`Invalid route ${file} — Astro's Collections API has been replaced by dynamic route params.`);
|
||||
// filter out non-route files
|
||||
if (!isDir && !isRoute) {
|
||||
return;
|
||||
}
|
||||
console.log(basename, extParts, isRoute);
|
||||
|
||||
const segment = isDir ? basename : name;
|
||||
if (/\]\[/.test(segment)) {
|
||||
throw new Error(`Invalid route ${file} — parameters must be separated`);
|
||||
}
|
||||
|
@ -124,8 +125,7 @@ export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?
|
|||
|
||||
const parts = getParts(segment, file);
|
||||
const isIndex = isDir ? false : basename.startsWith('index.');
|
||||
const routeSuffix = basename.slice(basename.indexOf('.'), -ext.length);
|
||||
|
||||
const routeSuffix = ''; //basename.slice(basename.indexOf('.'), -ext.length);
|
||||
items.push({
|
||||
basename,
|
||||
ext,
|
||||
|
@ -203,6 +203,8 @@ export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?
|
|||
warn(logging, 'astro', `Missing pages directory: ${pagesDirRootRelative}`);
|
||||
}
|
||||
|
||||
debug('route', 'route maninfest created:', routes);
|
||||
|
||||
return {
|
||||
routes,
|
||||
};
|
||||
|
|
|
@ -287,7 +287,7 @@ function createFetchContentFn(url: URL, site: URL) {
|
|||
Content: mod.default,
|
||||
content: mod.metadata,
|
||||
file: new URL(spec, url),
|
||||
url: urlSpec.includes('/pages/') ? urlSpec.replace(/^.*\/pages\//, sitePathname).replace(/(\/index)?\.md$/, '') : undefined,
|
||||
url: urlSpec.includes('/routes/') ? urlSpec.replace(/^.*\/routes\//, sitePathname).replace(/(\/index)?\.route\.md$/, '') : undefined,
|
||||
};
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
|
Loading…
Add table
Reference in a new issue