Skip to content

Commit

Permalink
Merge branch 'main' into fix/vercel-trailing-non-html
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanM04 committed Apr 25, 2022
2 parents 1494d38 + 1a86e77 commit 6df0516
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-experts-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Added better types to importing Markdown
5 changes: 5 additions & 0 deletions .changeset/large-birds-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Markdown file.url now respects `trailingSlash` and `base`
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,30 @@ The above copyright notice and this permission notice shall be included in all c

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""


"""
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/vitejs/vite repository:

MIT License

Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
13 changes: 13 additions & 0 deletions packages/astro/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,16 @@ type Astro = import('astro').AstroGlobal;
declare const Astro: Readonly<Astro>;

declare const Fragment: any;

declare module '*.md' {
type MD = import('astro').MarkdownInstance<Record<string, any>>;

export const frontmatter: MD['frontmatter'];
export const file: MD['file'];
export const url: MD['url'];
export const getHeaders: MD['getHeaders'];
export const Content: MD['Content'];

const load: MD['default'];
export default load;
}
35 changes: 29 additions & 6 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,13 @@ export interface MarkdownInstance<T extends Record<string, any>> {
file: string;
url: string | undefined;
Content: AstroComponentFactory;
getHeaders(): Promise<{ depth: number; slug: string; text: string }[]>;
getHeaders(): Promise<MarkdownHeader[]>;
default: () => Promise<{
metadata: MarkdownMetadata;
frontmatter: MarkdownContent;
$$metadata: Metadata;
default: AstroComponentFactory;
}>;
}

export type GetHydrateCallback = () => Promise<
Expand Down Expand Up @@ -767,18 +773,35 @@ export interface ManifestData {
routes: RouteData[];
}

export interface MarkdownHeader {
depth: number;
slug: string;
text: string;
}

export interface MarkdownMetadata {
headers: MarkdownHeader[];
source: string;
html: string;
}

export interface MarkdownParserResponse {
frontmatter: {
[key: string]: any;
};
metadata: {
headers: any[];
source: string;
html: string;
};
metadata: MarkdownMetadata;
code: string;
}

/**
* The `content` prop given to a Layout
* https://docs.astro.build/guides/markdown-content/#markdown-layouts
*/
export interface MarkdownContent {
[key: string]: any;
astro: MarkdownMetadata;
}

/**
* paginate() Options
* Docs: https://docs.astro.build/guides/pagination/#calling-the-paginate-function
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ async function generatePath(
// If a base path was provided, append it to the site URL. This ensures that
// all injected scripts and links are referenced relative to the site and subpath.
const site =
astroConfig.base && astroConfig.base !== './'
astroConfig.base !== '/'
? joinPaths(astroConfig.site?.toString() || 'http://localhost/', astroConfig.base)
: astroConfig.site;
const links = createLinkStylesheetElementSet(linkIds.reverse(), site);
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ export const AstroConfigSchema = z.object({
base: z
.string()
.optional()
.default('./')
.transform((val) => (val ? appendForwardSlash(trimSlashes(val)) : val)),
.default('/')
.transform((val) => appendForwardSlash(trimSlashes(val))),
trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')])
.optional()
Expand Down
26 changes: 2 additions & 24 deletions packages/astro/src/core/render/dev/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,10 @@ export function collectResources(html: string): Resource[] {
// Vite’s `transformIndexHtml()` API for ease-of-use and consistency. But we need
// to borrow a few private methods in Vite to make that available here.
// https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/html.ts
//
// See LICENSE for more info.
// -------------------------------------------------------------------------------

// Vite is released under the MIT license:

// MIT License

// Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

const unaryTags = new Set(['link', 'meta', 'base']);

function serializeTag({ tag, attrs, children }: vite.HtmlTagDescriptor, indent = ''): string {
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ async function handle404Response(
// HACK: redirect without the base path for assets in publicDir
const redirectTo =
req.method === 'GET' &&
config.base &&
config.base !== './' &&
config.base !== '/' &&
pathname.startsWith(config.base) &&
pathname.replace(config.base, '/');

Expand Down
15 changes: 10 additions & 5 deletions packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,18 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
// Return the file's JS representation, including all Markdown
// frontmatter and a deferred `import() of the compiled markdown content.
if (id.endsWith(`.md${MARKDOWN_IMPORT_FLAG}`)) {
const sitePathname = config.site
? appendForwardSlash(new URL(config.base, config.site).pathname)
: '/';
const sitePathname = appendForwardSlash(
config.site ? new URL(config.base, config.site).pathname : config.base
);

const fileId = id.replace(MARKDOWN_IMPORT_FLAG, '');
const fileUrl = fileId.includes('/pages/')
? fileId.replace(/^.*\/pages\//, sitePathname).replace(/(\/index)?\.md$/, '')
let fileUrl = fileId.includes('/pages/')
? fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.md$/, '')
: undefined;
if (fileUrl && config.trailingSlash === 'always') {
fileUrl = appendForwardSlash(fileUrl);
}

const source = await fs.promises.readFile(fileId, 'utf8');
const { data: frontmatter } = matter(source);
return {
Expand Down
100 changes: 100 additions & 0 deletions packages/astro/test/astro-markdown-url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Astro Markdown URL', () => {
describe('With subpath', () => {
const baseUrl = `/my-cool-base/docs/pages/how-to-make-a-page`;

it('trailingSlash: always', async () => {
let fixture = await loadFixture({
root: './fixtures/astro-markdown-url/',
outDir: new URL('./fixtures/astro-markdown-url/with-subpath-always/', import.meta.url),
base: '/my-cool-base',
trailingSlash: 'always',
});
await fixture.build();

const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#url').attr('href')).to.equal(baseUrl + '/');
});

it('trailingSlash: never', async () => {
let fixture = await loadFixture({
root: './fixtures/astro-markdown-url/',
outDir: new URL('./fixtures/astro-markdown-url/with-subpath-never/', import.meta.url),
base: '/my-cool-base',
trailingSlash: 'never',
});
await fixture.build();

const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#url').attr('href')).to.equal(baseUrl);
});

it('trailingSlash: ignore', async () => {
let fixture = await loadFixture({
root: './fixtures/astro-markdown-url/',
outDir: new URL('./fixtures/astro-markdown-url/with-subpath-ignore/', import.meta.url),
base: '/my-cool-base',
trailingSlash: 'ignore',
});
await fixture.build();

const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#url').attr('href')).to.equal(baseUrl);
});
});

describe('Without subpath', () => {
const baseUrl = `/docs/pages/how-to-make-a-page`;

it('trailingSlash: always', async () => {
let fixture = await loadFixture({
root: './fixtures/astro-markdown-url/',
outDir: new URL('./fixtures/astro-markdown-url/without-subpath-always/', import.meta.url),
trailingSlash: 'always',
});
await fixture.build();

const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#url').attr('href')).to.equal(baseUrl + '/');
});

it('trailingSlash: never', async () => {
let fixture = await loadFixture({
root: './fixtures/astro-markdown-url/',
outDir: new URL('./fixtures/astro-markdown-url/without-subpath-never/', import.meta.url),
trailingSlash: 'never',
});
await fixture.build();

const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#url').attr('href')).to.equal(baseUrl);
});

it('trailingSlash: ignore', async () => {
let fixture = await loadFixture({
root: './fixtures/astro-markdown-url/',
outDir: new URL('./fixtures/astro-markdown-url/without-subpath-ignore/', import.meta.url),
trailingSlash: 'ignore',
});
await fixture.build();

const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#url').attr('href')).to.equal(baseUrl);
});
});
});
6 changes: 6 additions & 0 deletions packages/astro/test/fixtures/astro-markdown-url/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
with-subpath-always/
with-subpath-never/
with-subpath-ignore/
without-subpath-always/
without-subpath-never/
without-subpath-ignore/
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/astro-markdown-url/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/astro-markdown-url",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: How to make a page
---

Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
import * as page from './docs/pages/how-to-make-a-page.md'
---

<a id="url" href={page.url}>{page.frontmatter.title}</a>
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions scripts/stats/stats.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date,Commits (24hr),Issues (24hr),Issues:BUG (24hr),Issues:RFC (24hr),Issues:DOC (24hr),PRs (24hr),Open PRs,Open Issues,Bugs: Needs Triage,Bugs: Accepted,RFC: In Progress,RFC: Accepted,Date (ISO)
"Monday, April 25, 2022",2,3,3,0,0,2,14,105,66,35,0,0,"2022-04-25T12:06:54.900Z"
"Sunday, April 24, 2022",1,4,4,0,0,1,13,102,63,35,0,0,"2022-04-24T12:01:45.655Z"
"Saturday, April 23, 2022",10,1,1,0,0,9,12,98,59,35,0,0,"2022-04-23T12:05:15.509Z"
"Friday, April 22, 2022",20,4,4,0,0,16,12,98,59,35,0,0,"2022-04-22T12:02:01.807Z"
"Thursday, April 21, 2022",7,3,3,0,0,6,9,101,58,37,0,0,"2022-04-21T12:04:09.984Z"
"Wednesday, April 20, 2022",19,0,0,0,0,7,6,108,61,40,0,0,"2022-04-20T12:02:15.590Z"
Expand Down

0 comments on commit 6df0516

Please sign in to comment.