Revert "feat: support .md
overrides for content collections"
This reverts commit c06f83ef85
.
This commit is contained in:
parent
3db67a2187
commit
c16e79616a
15 changed files with 40 additions and 94 deletions
|
@ -2,13 +2,13 @@
|
||||||
title: Welcome to Markdoc 👋
|
title: Welcome to Markdoc 👋
|
||||||
---
|
---
|
||||||
|
|
||||||
This simple starter showcases Markdoc's features with Content Collections. All Markdoc features are supported, including this nifty built-in `{% table %}` tag:
|
This simple starter showcases Markdoc with Content Collections. All Markdoc features are supported, including this nifty built-in `{% table %}` tag:
|
||||||
|
|
||||||
{% table %}
|
{% table %}
|
||||||
* Feature
|
* Feature
|
||||||
* Supported
|
* Supported
|
||||||
---
|
---
|
||||||
* `.mdoc` + `.md` in Content Collections
|
* `.mdoc` in Content Collections
|
||||||
* ✅
|
* ✅
|
||||||
---
|
---
|
||||||
* Markdoc transform configuration
|
* Markdoc transform configuration
|
|
@ -979,7 +979,6 @@ export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ContentEntryType {
|
export interface ContentEntryType {
|
||||||
name: string;
|
|
||||||
extensions: string[];
|
extensions: string[];
|
||||||
getEntryInfo(params: { fileUrl: URL; contents: string }): Promise<{
|
getEntryInfo(params: { fileUrl: URL; contents: string }): Promise<{
|
||||||
data: Record<string, unknown>;
|
data: Record<string, unknown>;
|
||||||
|
|
|
@ -6,4 +6,3 @@ export const STYLES_PLACEHOLDER = '@@ASTRO-STYLES@@';
|
||||||
export const SCRIPTS_PLACEHOLDER = '@@ASTRO-SCRIPTS@@';
|
export const SCRIPTS_PLACEHOLDER = '@@ASTRO-SCRIPTS@@';
|
||||||
|
|
||||||
export const CONTENT_TYPES_FILE = 'types.d.ts';
|
export const CONTENT_TYPES_FILE = 'types.d.ts';
|
||||||
export const MARKDOWN_CONTENT_ENTRY_TYPE_NAME = 'astro:markdown';
|
|
||||||
|
|
|
@ -1,12 +1,6 @@
|
||||||
export { attachContentServerListeners } from './server-listeners.js';
|
export { attachContentServerListeners } from './server-listeners.js';
|
||||||
export { createContentTypesGenerator } from './types-generator.js';
|
export { createContentTypesGenerator } from './types-generator.js';
|
||||||
export {
|
export { contentObservable, getContentPaths, getDotAstroTypeReference } from './utils.js';
|
||||||
contentObservable,
|
|
||||||
getContentPaths,
|
|
||||||
getDotAstroTypeReference,
|
|
||||||
hasMdContentEntryTypeOverride,
|
|
||||||
} from './utils.js';
|
|
||||||
export { getMarkdownContentEntryType } from './markdown.js';
|
|
||||||
export { astroContentAssetPropagationPlugin } from './vite-plugin-content-assets.js';
|
export { astroContentAssetPropagationPlugin } from './vite-plugin-content-assets.js';
|
||||||
export { astroContentImportPlugin } from './vite-plugin-content-imports.js';
|
export { astroContentImportPlugin } from './vite-plugin-content-imports.js';
|
||||||
export { astroContentVirtualModPlugin } from './vite-plugin-content-virtual-mod.js';
|
export { astroContentVirtualModPlugin } from './vite-plugin-content-virtual-mod.js';
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
import type fsMod from 'node:fs';
|
|
||||||
import { fileURLToPath } from 'node:url';
|
|
||||||
import { AstroConfig, ContentEntryType } from '../@types/astro.js';
|
|
||||||
import { getContentPaths, parseFrontmatter } from './utils.js';
|
|
||||||
import { MARKDOWN_CONTENT_ENTRY_TYPE_NAME } from './consts.js';
|
|
||||||
|
|
||||||
export async function getMarkdownContentEntryType(
|
|
||||||
config: Pick<AstroConfig, 'root' | 'srcDir'>,
|
|
||||||
fs: typeof fsMod
|
|
||||||
): Promise<ContentEntryType> {
|
|
||||||
const contentPaths = getContentPaths(config, fs);
|
|
||||||
return {
|
|
||||||
name: MARKDOWN_CONTENT_ENTRY_TYPE_NAME,
|
|
||||||
extensions: ['.md'],
|
|
||||||
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
|
||||||
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
|
||||||
return {
|
|
||||||
data: parsed.data,
|
|
||||||
body: parsed.content,
|
|
||||||
slug: parsed.data.slug,
|
|
||||||
rawData: parsed.matter,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
contentModuleTypes: await fs.promises.readFile(
|
|
||||||
new URL('./markdown-types.d.ts', contentPaths.templateDir),
|
|
||||||
'utf-8'
|
|
||||||
),
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
declare module 'astro:content' {
|
|
||||||
interface Render {
|
|
||||||
'.md': Promise<{
|
|
||||||
Content: import('astro').MarkdownInstance<{}>['Content'];
|
|
||||||
headings: import('astro').MarkdownHeading[];
|
|
||||||
remarkPluginFrontmatter: Record<string, any>;
|
|
||||||
}>;
|
|
||||||
}
|
|
||||||
}
|
|
10
packages/astro/src/content/template/types.d.ts
vendored
10
packages/astro/src/content/template/types.d.ts
vendored
|
@ -1,3 +1,13 @@
|
||||||
|
declare module 'astro:content' {
|
||||||
|
interface Render {
|
||||||
|
'.md': Promise<{
|
||||||
|
Content: import('astro').MarkdownInstance<{}>['Content'];
|
||||||
|
headings: import('astro').MarkdownHeading[];
|
||||||
|
remarkPluginFrontmatter: Record<string, any>;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
declare module 'astro:content' {
|
declare module 'astro:content' {
|
||||||
export { z } from 'astro/zod';
|
export { z } from 'astro/zod';
|
||||||
export type CollectionEntry<C extends keyof typeof entryMap> =
|
export type CollectionEntry<C extends keyof typeof entryMap> =
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { ErrorPayload as ViteErrorPayload, normalizePath, ViteDevServer } from '
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { AstroConfig, AstroSettings } from '../@types/astro.js';
|
import { AstroConfig, AstroSettings } from '../@types/astro.js';
|
||||||
import { AstroError, AstroErrorData } from '../core/errors/index.js';
|
import { AstroError, AstroErrorData } from '../core/errors/index.js';
|
||||||
import { CONTENT_TYPES_FILE, MARKDOWN_CONTENT_ENTRY_TYPE_NAME } from './consts.js';
|
import { CONTENT_TYPES_FILE } from './consts.js';
|
||||||
|
|
||||||
export const collectionConfigParser = z.object({
|
export const collectionConfigParser = z.object({
|
||||||
schema: z.any().optional(),
|
schema: z.any().optional(),
|
||||||
|
@ -315,7 +315,6 @@ export function contentObservable(initialCtx: ContentCtx): ContentObservable {
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ContentPaths = {
|
export type ContentPaths = {
|
||||||
templateDir: URL;
|
|
||||||
contentDir: URL;
|
contentDir: URL;
|
||||||
cacheDir: URL;
|
cacheDir: URL;
|
||||||
typesTemplate: URL;
|
typesTemplate: URL;
|
||||||
|
@ -333,7 +332,6 @@ export function getContentPaths(
|
||||||
const configStats = search(fs, srcDir);
|
const configStats = search(fs, srcDir);
|
||||||
const templateDir = new URL('../../src/content/template/', import.meta.url);
|
const templateDir = new URL('../../src/content/template/', import.meta.url);
|
||||||
return {
|
return {
|
||||||
templateDir,
|
|
||||||
cacheDir: new URL('.astro/', root),
|
cacheDir: new URL('.astro/', root),
|
||||||
contentDir: new URL('./content/', srcDir),
|
contentDir: new URL('./content/', srcDir),
|
||||||
typesTemplate: new URL('types.d.ts', templateDir),
|
typesTemplate: new URL('types.d.ts', templateDir),
|
||||||
|
@ -352,11 +350,3 @@ function search(fs: typeof fsMod, srcDir: URL) {
|
||||||
}
|
}
|
||||||
return { exists: false, url: paths[0] };
|
return { exists: false, url: paths[0] };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function hasMdContentEntryTypeOverride(settings: Pick<AstroSettings, 'contentEntryTypes'>) {
|
|
||||||
return settings.contentEntryTypes.some(
|
|
||||||
(contentEntryType) =>
|
|
||||||
contentEntryType.name !== MARKDOWN_CONTENT_ENTRY_TYPE_NAME &&
|
|
||||||
contentEntryType.extensions.includes('.md')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import jsxRenderer from '../../jsx/renderer.js';
|
||||||
import { createDefaultDevConfig } from './config.js';
|
import { createDefaultDevConfig } from './config.js';
|
||||||
import { AstroTimer } from './timer.js';
|
import { AstroTimer } from './timer.js';
|
||||||
import { loadTSConfig } from './tsconfig.js';
|
import { loadTSConfig } from './tsconfig.js';
|
||||||
|
import { markdownContentEntryType } from '../../vite-plugin-markdown/content-entry-type.js';
|
||||||
|
|
||||||
export function createBaseSettings(config: AstroConfig): AstroSettings {
|
export function createBaseSettings(config: AstroConfig): AstroSettings {
|
||||||
return {
|
return {
|
||||||
|
@ -16,7 +17,7 @@ export function createBaseSettings(config: AstroConfig): AstroSettings {
|
||||||
adapter: undefined,
|
adapter: undefined,
|
||||||
injectedRoutes: [],
|
injectedRoutes: [],
|
||||||
pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS],
|
pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS],
|
||||||
contentEntryTypes: [],
|
contentEntryTypes: [markdownContentEntryType],
|
||||||
renderers: [jsxRenderer],
|
renderers: [jsxRenderer],
|
||||||
scripts: [],
|
scripts: [],
|
||||||
watchFiles: [],
|
watchFiles: [],
|
||||||
|
|
|
@ -12,12 +12,10 @@ import {
|
||||||
HookParameters,
|
HookParameters,
|
||||||
RouteData,
|
RouteData,
|
||||||
} from '../@types/astro.js';
|
} from '../@types/astro.js';
|
||||||
import { hasMdContentEntryTypeOverride } from '../content/utils.js';
|
|
||||||
import type { SerializedSSRManifest } from '../core/app/types';
|
import type { SerializedSSRManifest } from '../core/app/types';
|
||||||
import type { PageBuildData } from '../core/build/types';
|
import type { PageBuildData } from '../core/build/types';
|
||||||
import { mergeConfig } from '../core/config/config.js';
|
import { mergeConfig } from '../core/config/config.js';
|
||||||
import { info, LogOptions } from '../core/logger/core.js';
|
import { info, LogOptions } from '../core/logger/core.js';
|
||||||
import { getMarkdownContentEntryType } from '../content/index.js';
|
|
||||||
|
|
||||||
async function withTakingALongTimeMsg<T>({
|
async function withTakingALongTimeMsg<T>({
|
||||||
name,
|
name,
|
||||||
|
@ -128,12 +126,6 @@ export async function runHookConfigSetup({
|
||||||
}
|
}
|
||||||
|
|
||||||
updatedSettings.config = updatedConfig;
|
updatedSettings.config = updatedConfig;
|
||||||
if (!hasMdContentEntryTypeOverride(updatedSettings)) {
|
|
||||||
updatedSettings.contentEntryTypes.push(
|
|
||||||
await getMarkdownContentEntryType(updatedSettings.config, fs)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return updatedSettings;
|
return updatedSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
import { ContentEntryType } from '../@types/astro.js';
|
||||||
|
import { parseFrontmatter } from '../content/utils.js';
|
||||||
|
|
||||||
|
export const markdownContentEntryType: ContentEntryType = {
|
||||||
|
extensions: ['.md'],
|
||||||
|
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
||||||
|
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
||||||
|
return {
|
||||||
|
data: parsed.data,
|
||||||
|
body: parsed.content,
|
||||||
|
slug: parsed.data.slug,
|
||||||
|
rawData: parsed.matter,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
|
@ -10,7 +10,6 @@ import type { Plugin } from 'vite';
|
||||||
import { normalizePath } from 'vite';
|
import { normalizePath } from 'vite';
|
||||||
import type { AstroSettings } from '../@types/astro';
|
import type { AstroSettings } from '../@types/astro';
|
||||||
import { getContentPaths } from '../content/index.js';
|
import { getContentPaths } from '../content/index.js';
|
||||||
import { hasMdContentEntryTypeOverride } from '../content/index.js';
|
|
||||||
import { AstroError, AstroErrorData, MarkdownError } from '../core/errors/index.js';
|
import { AstroError, AstroErrorData, MarkdownError } from '../core/errors/index.js';
|
||||||
import type { LogOptions } from '../core/logger/core.js';
|
import type { LogOptions } from '../core/logger/core.js';
|
||||||
import { warn } from '../core/logger/core.js';
|
import { warn } from '../core/logger/core.js';
|
||||||
|
@ -67,15 +66,6 @@ export default function markdown({ settings, logging }: AstroPluginOptions): Plu
|
||||||
async load(id) {
|
async load(id) {
|
||||||
if (isMarkdownFile(id)) {
|
if (isMarkdownFile(id)) {
|
||||||
const { fileId, fileUrl } = getFileInfo(id, settings.config);
|
const { fileId, fileUrl } = getFileInfo(id, settings.config);
|
||||||
if (
|
|
||||||
// Integrations can override the Markdown parser for content collections.
|
|
||||||
// If an override is present, skip this file.
|
|
||||||
fileId.startsWith(getContentPaths(settings.config).contentDir.pathname) &&
|
|
||||||
hasMdContentEntryTypeOverride(settings)
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const rawFile = await fs.promises.readFile(fileId, 'utf-8');
|
const rawFile = await fs.promises.readFile(fileId, 'utf-8');
|
||||||
const raw = safeMatter(rawFile, id);
|
const raw = safeMatter(rawFile, id);
|
||||||
const renderResult = await renderMarkdown(raw.content, {
|
const renderResult = await renderMarkdown(raw.content, {
|
||||||
|
|
|
@ -6,16 +6,13 @@ import { parseFrontmatter } from './utils.js';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
|
|
||||||
const DEFAULT_MARKDOC_EXTS = ['.mdoc', '.md'];
|
|
||||||
|
|
||||||
export default function markdoc(): AstroIntegration {
|
export default function markdoc(): AstroIntegration {
|
||||||
return {
|
return {
|
||||||
name: '@astrojs/markdoc',
|
name: '@astrojs/markdoc',
|
||||||
hooks: {
|
hooks: {
|
||||||
'astro:config:setup': async ({ updateConfig, config, addContentEntryType, command }: any) => {
|
'astro:config:setup': async ({ updateConfig, config, addContentEntryType, command }: any) => {
|
||||||
const contentEntryType = {
|
const contentEntryType = {
|
||||||
name: 'astro:markdoc',
|
extensions: ['.mdoc'],
|
||||||
extensions: DEFAULT_MARKDOC_EXTS,
|
|
||||||
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
||||||
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
||||||
return {
|
return {
|
||||||
|
@ -37,7 +34,7 @@ export default function markdoc(): AstroIntegration {
|
||||||
{
|
{
|
||||||
name: '@astrojs/markdoc',
|
name: '@astrojs/markdoc',
|
||||||
async transform(code, id) {
|
async transform(code, id) {
|
||||||
if (!DEFAULT_MARKDOC_EXTS.some((ext) => id.endsWith(ext))) return;
|
if (!id.endsWith('.mdoc')) return;
|
||||||
return `import { jsx as h } from 'astro/jsx-runtime';\nimport { Markdoc } from '@astrojs/markdoc';\nimport { Renderer } from '@astrojs/markdoc/components';\nexport const body = ${JSON.stringify(
|
return `import { jsx as h } from 'astro/jsx-runtime';\nimport { Markdoc } from '@astrojs/markdoc';\nimport { Renderer } from '@astrojs/markdoc/components';\nexport const body = ${JSON.stringify(
|
||||||
code
|
code
|
||||||
)};\nexport function getParsed() { return Markdoc.parse(body); }\nexport function getTransformed(inlineConfig) { return Markdoc.transform(getParsed(), inlineConfig) }\nexport async function Content ({ config, components }) { return h(Renderer, { content: getTransformed(config), components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`;
|
)};\nexport function getParsed() { return Markdoc.parse(body); }\nexport function getTransformed(inlineConfig) { return Markdoc.transform(getParsed(), inlineConfig) }\nexport async function Content ({ config, components }) { return h(Renderer, { content: getTransformed(config), components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`;
|
||||||
|
|
|
@ -9,15 +9,12 @@ declare module 'astro:content' {
|
||||||
}): Record<string, any>;
|
}): Record<string, any>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type RenderResult = Promise<{
|
|
||||||
Content(props: {
|
|
||||||
config?: import('@astrojs/markdoc').MarkdocConfig;
|
|
||||||
components?: Record<string, ComponentRenderer>;
|
|
||||||
}): import('astro').MarkdownInstance<{}>['Content'];
|
|
||||||
}>;
|
|
||||||
|
|
||||||
interface Render {
|
interface Render {
|
||||||
'.md': RenderResult;
|
'.mdoc': Promise<{
|
||||||
'.mdoc': RenderResult;
|
Content(props: {
|
||||||
|
config?: import('@astrojs/markdoc').MarkdocConfig;
|
||||||
|
components?: Record<string, ComponentRenderer>;
|
||||||
|
}): import('astro').MarkdownInstance<{}>['Content'];
|
||||||
|
}>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,6 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
|
||||||
command,
|
command,
|
||||||
}: any) => {
|
}: any) => {
|
||||||
const contentEntryType = {
|
const contentEntryType = {
|
||||||
name: 'astro:mdx',
|
|
||||||
extensions: ['.mdx'],
|
extensions: ['.mdx'],
|
||||||
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
||||||
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
||||||
|
|
Loading…
Reference in a new issue