From 7bd836f030f525e67c9a3a27e142e41ad1ee77ea Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Fri, 2 Jul 2021 08:59:14 -0700 Subject: [PATCH] wip --- examples/blog/src/pages/posts/test.mdc | 11 ++++++++ package.json | 2 +- packages/astro/snowpack-plugin.cjs | 2 +- packages/astro/src/build/util.ts | 2 +- packages/astro/src/compiler/index.ts | 36 +++++++++++++++++++++++--- packages/astro/src/search.ts | 6 ++--- 6 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 examples/blog/src/pages/posts/test.mdc diff --git a/examples/blog/src/pages/posts/test.mdc b/examples/blog/src/pages/posts/test.mdc new file mode 100644 index 0000000000..6f391435f8 --- /dev/null +++ b/examples/blog/src/pages/posts/test.mdc @@ -0,0 +1,11 @@ +--- +import Logo from '../../components/Logo.astro'; +--- + +# Hello, World! + +hi + + + +hi diff --git a/package.json b/package.json index b68d1596bd..ddd4bee816 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "build": "yarn build:core", "build:all": "lerna run build", "build:one": "lerna run build --scope", - "build:core": "lerna run build --scope astro --scope @astrojs/parser --scope @astrojs/markdown-support --scope create-astro", + "build:core": "lerna run build --scope astro --scope @astrojs/parser --scope @astrojs/markdown-support", "build:vscode": "lerna run build --scope astro-languageserver --scope astro-vscode --scope @astrojs/parser", "dev:vscode": "lerna run dev --scope astro-languageserver --scope astro-vscode --scope @astrojs/parser --parallel --stream", "format": "prettier -w \"**/*.{js,jsx,ts,tsx,md,json}\"", diff --git a/packages/astro/snowpack-plugin.cjs b/packages/astro/snowpack-plugin.cjs index 30f381e560..2d8a0672a3 100644 --- a/packages/astro/snowpack-plugin.cjs +++ b/packages/astro/snowpack-plugin.cjs @@ -31,7 +31,7 @@ module.exports = (snowpackConfig, options = {}) => { 'astring' ], resolve: { - input: ['.astro', '.md'], + input: ['.astro', '.md', '.mdc'], output: ['.js', '.css'], }, async transform({contents, id, fileExt}) { diff --git a/packages/astro/src/build/util.ts b/packages/astro/src/build/util.ts index 9dc78b453d..f8829324d1 100644 --- a/packages/astro/src/build/util.ts +++ b/packages/astro/src/build/util.ts @@ -23,7 +23,7 @@ export function getDistPath(specifier: string, { astroConfig, srcPath }: { astro const projectLoc = fileLoc.pathname.replace(projectRoot.pathname, ''); const ext = path.extname(fileLoc.pathname); - const isPage = fileLoc.pathname.includes(pagesRoot.pathname) && (ext === '.astro' || ext === '.md'); + const isPage = fileLoc.pathname.includes(pagesRoot.pathname) && (ext === '.astro' || ext === '.md' || ext === '.mdc'); // if this lives in src/pages, return that URL if (isPage) { const [, publicURL] = projectLoc.split(pagesRoot.pathname); diff --git a/packages/astro/src/compiler/index.ts b/packages/astro/src/compiler/index.ts index f612e61656..2c6a59818b 100644 --- a/packages/astro/src/compiler/index.ts +++ b/packages/astro/src/compiler/index.ts @@ -3,7 +3,7 @@ import type { CompileResult, TransformResult } from '../@types/astro'; import type { CompileOptions } from '../@types/compiler.js'; import path from 'path'; -import { MarkdownRenderingOptions, renderMarkdownWithFrontmatter } from '@astrojs/markdown-support'; +import { MarkdownRenderingOptions, renderMarkdown, renderMarkdownWithFrontmatter } from '@astrojs/markdown-support'; import { parse } from '@astrojs/parser'; import { transform } from './transform/index.js'; @@ -80,6 +80,33 @@ async function convertMdToJsx( return await convertAstroToJsx(raw, convertOptions); } +/** + * .md -> .astro source + */ + export async function convertMdcToAstroSource(contents: string, opts?: MarkdownRenderingOptions): Promise { + const [,frontmatterScript, markdownContent] = contents.split('---'); +return `--- +import { Markdown } from 'astro/components'; +${frontmatterScript} +--- + +${markdownContent} +`; +} + +/** + * .mdc -> .jsx + * Core function processing Markdown, but along the way also calls convertAstroToJsx(). + */ + async function convertMdcToJsx( + contents: string, + { compileOptions, filename, fileID }: { compileOptions: CompileOptions; filename: string; fileID: string } +): Promise { + const raw = await convertMdcToAstroSource(contents, compileOptions.astroConfig.markdownOptions); + const convertOptions = { compileOptions, filename, fileID }; + return await convertAstroToJsx(raw, convertOptions); +} + /** Given a file, process it either as .astro, .md */ async function transformFromSource( contents: string, @@ -87,10 +114,13 @@ async function transformFromSource( ): Promise { const fileID = path.relative(projectRoot, filename); switch (true) { - case filename.slice(-6) === '.astro': + case filename.endsWith('.astro'): return await convertAstroToJsx(contents, { compileOptions, filename, fileID }); - case filename.slice(-3) === '.md': + case filename.endsWith('.mdc'): + return await convertMdcToJsx(contents, { compileOptions, filename, fileID }); + + case filename.endsWith('.md'): return await convertMdToJsx(contents, { compileOptions, filename, fileID }); default: diff --git a/packages/astro/src/search.ts b/packages/astro/src/search.ts index c9f4f436b8..9397f0e05f 100644 --- a/packages/astro/src/search.ts +++ b/packages/astro/src/search.ts @@ -48,7 +48,7 @@ export function searchForPage(url: URL, astroConfig: AstroConfig): SearchResult // Try to find index.astro/md paths if (reqPath.endsWith('/')) { - const candidates = [`${base}index.astro`, `${base}index.md`]; + const candidates = [`${base}index.astro`, `${base}index.md`, `${base}index.mdc`]; const location = findAnyPage(candidates, astroConfig); if (location) { return { @@ -59,7 +59,7 @@ export function searchForPage(url: URL, astroConfig: AstroConfig): SearchResult } } else { // Try to find the page by its name. - const candidates = [`${base}.astro`, `${base}.md`]; + const candidates = [`${base}.astro`, `${base}.md`, `${base}.mdc`]; let location = findAnyPage(candidates, astroConfig); if (location) { return { @@ -71,7 +71,7 @@ export function searchForPage(url: URL, astroConfig: AstroConfig): SearchResult } // Try to find name/index.astro/md - const candidates = [`${base}/index.astro`, `${base}/index.md`]; + const candidates = [`${base}/index.astro`, `${base}/index.md`, `${base}/index.mdc`]; const location = findAnyPage(candidates, astroConfig); if (location) { return {