Skip to content

Commit 4a8a346

Browse files
authored
feat: add support for other markdown file extensions (#5164)
* fix: add `.markdown ` file extension support adds `.markdown` file extension support for markdown files * test: add test case * chore: adds changeset * test: move test and fixture to relevant locations * test: update test * feat: add multiple markdown file extension support * feat: add module declaration for different markdown file extensions * refactor: markdown module declarations for ease of TS refactoring * fix: add .js extension to module imports * test: update test * chore: update changeset * chore: update changeset * test: add new test cases * test: update tests * fix: correct typo
1 parent d151d9f commit 4a8a346

22 files changed

Lines changed: 228 additions & 32 deletions

File tree

.changeset/many-rockets-admire.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'astro': minor
3+
'@astrojs/rss': patch
4+
---
5+
6+
Add support for markdown files with the following extensions:
7+
- `.markdown`
8+
- `.mdown`
9+
- `.mkdn`
10+
- `.mkd`
11+
- `.mdwn`

packages/astro-rss/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type RSSOptions = {
1717
/**
1818
* List of RSS feed items to render. Accepts either:
1919
* a) list of RSSFeedItems
20-
* b) import.meta.glob result. You can only glob ".md" files within src/pages/ when using this method!
20+
* b) import.meta.glob result. You can only glob ".md" (or alternative extensions for markdown files like ".markdown") files within src/pages/ when using this method!
2121
*/
2222
items: RSSFeedItem[] | GlobResult;
2323
/** Specify arbitrary metadata on opening <xml> tag */
@@ -58,7 +58,7 @@ function mapGlobResult(items: GlobResult): Promise<RSSFeedItem[]> {
5858
const { url, frontmatter } = await getInfo();
5959
if (url === undefined || url === null) {
6060
throw new Error(
61-
`[RSS] When passing an import.meta.glob result directly, you can only glob ".md" files within /pages! Consider mapping the result to an array of RSSFeedItems. See the RSS docs for usage examples: https://docs.astro.build/en/guides/rss/#2-list-of-rss-feed-objects`
61+
`[RSS] When passing an import.meta.glob result directly, you can only glob ".md" (or alternative extensions for markdown files like ".markdown") files within /pages! Consider mapping the result to an array of RSSFeedItems. See the RSS docs for usage examples: https://docs.astro.build/en/guides/rss/#2-list-of-rss-feed-objects`
6262
);
6363
}
6464
if (!Boolean(frontmatter.title) || !Boolean(frontmatter.pubDate)) {

packages/astro-rss/test/rss.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ describe('rss', () => {
198198
});
199199
chai.expect(false).to.equal(true, 'Should have errored');
200200
} catch (err) {
201-
chai.expect(err.message).to.contain('you can only glob ".md" files within /pages');
201+
chai
202+
.expect(err.message)
203+
.to.contain(
204+
'you can only glob ".md" (or alternative extensions for markdown files like ".markdown") files within /pages'
205+
);
202206
}
203207
});
204208
});

packages/astro/client-base.d.ts

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,106 @@
11
/// <reference path="./import-meta.d.ts" />
22

3+
type MD = import('./dist/@types/astro').MarkdownInstance<Record<string, any>>;
4+
interface ExportedMarkdownModuleEntities {
5+
frontmatter: MD['frontmatter'];
6+
file: MD['file'];
7+
url: MD['url'];
8+
getHeadings: MD['getHeadings'];
9+
/** @deprecated Renamed to `getHeadings()` */
10+
getHeaders: () => void;
11+
Content: MD['Content'];
12+
rawContent: MD['rawContent'];
13+
compiledContent: MD['compiledContent'];
14+
load: MD['default'];
15+
}
16+
317
declare module '*.md' {
4-
type MD = import('./dist/@types/astro').MarkdownInstance<Record<string, any>>;
18+
const { load }: ExportedMarkdownModuleEntities;
19+
export const {
20+
frontmatter,
21+
file,
22+
url,
23+
getHeadings,
24+
getHeaders,
25+
Content,
26+
rawContent,
27+
compiledContent,
28+
}: ExportedMarkdownModuleEntities;
29+
export default load;
30+
}
531

6-
export const frontmatter: MD['frontmatter'];
7-
export const file: MD['file'];
8-
export const url: MD['url'];
9-
export const getHeadings: MD['getHeadings'];
10-
/** @deprecated Renamed to `getHeadings()` */
11-
export const getHeaders: () => void;
12-
export const Content: MD['Content'];
13-
export const rawContent: MD['rawContent'];
14-
export const compiledContent: MD['compiledContent'];
32+
declare module '*.markdown' {
33+
const { load }: ExportedMarkdownModuleEntities;
34+
export const {
35+
frontmatter,
36+
file,
37+
url,
38+
getHeadings,
39+
getHeaders,
40+
Content,
41+
rawContent,
42+
compiledContent,
43+
}: ExportedMarkdownModuleEntities;
44+
export default load;
45+
}
46+
47+
declare module '*.mkdn' {
48+
const { load }: ExportedMarkdownModuleEntities;
49+
export const {
50+
frontmatter,
51+
file,
52+
url,
53+
getHeadings,
54+
getHeaders,
55+
Content,
56+
rawContent,
57+
compiledContent,
58+
}: ExportedMarkdownModuleEntities;
59+
export default load;
60+
}
61+
62+
declare module '*.mkd' {
63+
const { load }: ExportedMarkdownModuleEntities;
64+
export const {
65+
frontmatter,
66+
file,
67+
url,
68+
getHeadings,
69+
getHeaders,
70+
Content,
71+
rawContent,
72+
compiledContent,
73+
}: ExportedMarkdownModuleEntities;
74+
export default load;
75+
}
76+
77+
declare module '*.mdwn' {
78+
const { load }: ExportedMarkdownModuleEntities;
79+
export const {
80+
frontmatter,
81+
file,
82+
url,
83+
getHeadings,
84+
getHeaders,
85+
Content,
86+
rawContent,
87+
compiledContent,
88+
}: ExportedMarkdownModuleEntities;
89+
export default load;
90+
}
1591

16-
const load: MD['default'];
92+
declare module '*.mdown' {
93+
const { load }: ExportedMarkdownModuleEntities;
94+
export const {
95+
frontmatter,
96+
file,
97+
url,
98+
getHeadings,
99+
getHeaders,
100+
Content,
101+
rawContent,
102+
compiledContent,
103+
}: ExportedMarkdownModuleEntities;
17104
export default load;
18105
}
19106

packages/astro/src/@types/astro.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../core/constants.js';
12
import type {
23
MarkdownHeading,
34
MarkdownMetadata,
@@ -246,6 +247,9 @@ export interface AstroGlobal<Props extends Record<string, any> = Record<string,
246247
};
247248
}
248249

250+
/** Union type of supported markdown file extensions */
251+
type MarkdowFileExtension = typeof SUPPORTED_MARKDOWN_FILE_EXTENSIONS[number];
252+
249253
export interface AstroGlobalPartial {
250254
/**
251255
* @deprecated since version 0.24. See the {@link https://astro.build/deprecated/resolve upgrade guide} for more details.
@@ -264,7 +268,9 @@ export interface AstroGlobalPartial {
264268
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astroglob)
265269
*/
266270
glob(globStr: `${any}.astro`): Promise<AstroInstance[]>;
267-
glob<T extends Record<string, any>>(globStr: `${any}.md`): Promise<MarkdownInstance<T>[]>;
271+
glob<T extends Record<string, any>>(
272+
globStr: `${any}${MarkdowFileExtension}`
273+
): Promise<MarkdownInstance<T>[]>;
268274
glob<T extends Record<string, any>>(globStr: `${any}.mdx`): Promise<MDXInstance<T>[]>;
269275
glob<T extends Record<string, any>>(globStr: string): Promise<T[]>;
270276
/**
@@ -868,7 +874,7 @@ export interface AstroUserConfig {
868874
* @default `false`
869875
* @version 1.0.0-rc.1
870876
* @description
871-
* Enable Astro's pre-v1.0 support for components and JSX expressions in `.md` Markdown files.
877+
* Enable Astro's pre-v1.0 support for components and JSX expressions in `.md` (and alternative extensions for markdown files like ".markdown") Markdown files.
872878
* In Astro `1.0.0-rc`, this original behavior was removed as the default, in favor of our new [MDX integration](/en/guides/integrations-guide/mdx/).
873879
*
874880
* To enable this behavior, set `legacy.astroFlavoredMarkdown` to `true` in your [`astro.config.mjs` configuration file](/en/guides/configuring-astro/#the-astro-config-file).

packages/astro/src/core/build/graph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function moduleIsTopLevelPage(info: ModuleInfo): boolean {
3232
}
3333

3434
// This function walks the dependency graph, going up until it finds a page component.
35-
// This could be a .astro page or a .md page.
35+
// This could be a .astro page, a .markdown or a .md (or really any file extension for markdown files) page.
3636
export function* getTopLevelPages(
3737
id: string,
3838
ctx: { getModuleInfo: GetModuleInfo }

packages/astro/src/core/config/settings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../constants.js';
12
import type { AstroConfig, AstroSettings } from '../../@types/astro';
23

34
import jsxRenderer from '../../jsx/renderer.js';
@@ -13,7 +14,7 @@ export function createSettings(config: AstroConfig, cwd?: string): AstroSettings
1314

1415
adapter: undefined,
1516
injectedRoutes: [],
16-
pageExtensions: ['.astro', '.md', '.html'],
17+
pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS],
1718
renderers: [jsxRenderer],
1819
scripts: [],
1920
watchFiles: tsconfig?.exists ? [tsconfig.path, ...tsconfig.extendedPaths] : [],
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
// process.env.PACKAGE_VERSION is injected when we build and publish the astro package.
22
export const ASTRO_VERSION = process.env.PACKAGE_VERSION ?? 'development';
3+
4+
// possible extensions for markdown files
5+
export const SUPPORTED_MARKDOWN_FILE_EXTENSIONS = [
6+
'.markdown',
7+
'.mdown',
8+
'.mkdn',
9+
'.mkd',
10+
'.mdwn',
11+
'.md',
12+
] as const;

packages/astro/src/core/render/dev/vite.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import npath from 'path';
22
import vite from 'vite';
3+
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from '../../constants.js';
34
import { unwrapId } from '../../util.js';
45
import { STYLE_EXTENSIONS } from '../util.js';
56

67
/**
78
* List of file extensions signalling we can (and should) SSR ahead-of-time
89
* See usage below
910
*/
10-
const fileExtensionsToSSR = new Set(['.astro', '.md']);
11+
const fileExtensionsToSSR = new Set(['.astro', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS]);
1112

1213
const STRIP_QUERY_PARAMS_REGEX = /\?.*$/;
1314

packages/astro/src/core/routing/manifest/create.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { warn } from '../../logger/core.js';
1717
import { removeLeadingForwardSlash } from '../../path.js';
1818
import { resolvePages } from '../../util.js';
1919
import { getRouteGenerator } from './generator.js';
20+
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from '../../constants.js';
2021
const require = createRequire(import.meta.url);
2122

2223
interface Item {
@@ -206,7 +207,11 @@ export function createRouteManifest(
206207
): ManifestData {
207208
const components: string[] = [];
208209
const routes: RouteData[] = [];
209-
const validPageExtensions: Set<string> = new Set(['.astro', '.md', ...settings.pageExtensions]);
210+
const validPageExtensions: Set<string> = new Set([
211+
'.astro',
212+
...SUPPORTED_MARKDOWN_FILE_EXTENSIONS,
213+
...settings.pageExtensions,
214+
]);
210215
const validEndpointExtensions: Set<string> = new Set(['.js', '.ts']);
211216

212217
function walk(dir: string, parentSegments: RoutePart[][], parentParams: string[]) {

0 commit comments

Comments
 (0)