mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
wip
This commit is contained in:
parent
2ab625bee8
commit
7bd836f030
6 changed files with 50 additions and 9 deletions
11
examples/blog/src/pages/posts/test.mdc
Normal file
11
examples/blog/src/pages/posts/test.mdc
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
---
|
||||||
|
import Logo from '../../components/Logo.astro';
|
||||||
|
---
|
||||||
|
|
||||||
|
# Hello, World!
|
||||||
|
|
||||||
|
hi
|
||||||
|
|
||||||
|
<Logo />
|
||||||
|
|
||||||
|
hi
|
|
@ -12,7 +12,7 @@
|
||||||
"build": "yarn build:core",
|
"build": "yarn build:core",
|
||||||
"build:all": "lerna run build",
|
"build:all": "lerna run build",
|
||||||
"build:one": "lerna run build --scope",
|
"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",
|
"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",
|
"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}\"",
|
"format": "prettier -w \"**/*.{js,jsx,ts,tsx,md,json}\"",
|
||||||
|
|
|
@ -31,7 +31,7 @@ module.exports = (snowpackConfig, options = {}) => {
|
||||||
'astring'
|
'astring'
|
||||||
],
|
],
|
||||||
resolve: {
|
resolve: {
|
||||||
input: ['.astro', '.md'],
|
input: ['.astro', '.md', '.mdc'],
|
||||||
output: ['.js', '.css'],
|
output: ['.js', '.css'],
|
||||||
},
|
},
|
||||||
async transform({contents, id, fileExt}) {
|
async transform({contents, id, fileExt}) {
|
||||||
|
|
|
@ -23,7 +23,7 @@ export function getDistPath(specifier: string, { astroConfig, srcPath }: { astro
|
||||||
const projectLoc = fileLoc.pathname.replace(projectRoot.pathname, '');
|
const projectLoc = fileLoc.pathname.replace(projectRoot.pathname, '');
|
||||||
const ext = path.extname(fileLoc.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 this lives in src/pages, return that URL
|
||||||
if (isPage) {
|
if (isPage) {
|
||||||
const [, publicURL] = projectLoc.split(pagesRoot.pathname);
|
const [, publicURL] = projectLoc.split(pagesRoot.pathname);
|
||||||
|
|
|
@ -3,7 +3,7 @@ import type { CompileResult, TransformResult } from '../@types/astro';
|
||||||
import type { CompileOptions } from '../@types/compiler.js';
|
import type { CompileOptions } from '../@types/compiler.js';
|
||||||
|
|
||||||
import path from 'path';
|
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 { parse } from '@astrojs/parser';
|
||||||
import { transform } from './transform/index.js';
|
import { transform } from './transform/index.js';
|
||||||
|
@ -80,6 +80,33 @@ async function convertMdToJsx(
|
||||||
return await convertAstroToJsx(raw, convertOptions);
|
return await convertAstroToJsx(raw, convertOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* .md -> .astro source
|
||||||
|
*/
|
||||||
|
export async function convertMdcToAstroSource(contents: string, opts?: MarkdownRenderingOptions): Promise<string> {
|
||||||
|
const [,frontmatterScript, markdownContent] = contents.split('---');
|
||||||
|
return `---
|
||||||
|
import { Markdown } from 'astro/components';
|
||||||
|
${frontmatterScript}
|
||||||
|
---
|
||||||
|
<Markdown>
|
||||||
|
${markdownContent}
|
||||||
|
</Markdown>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* .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<TransformResult> {
|
||||||
|
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 */
|
/** Given a file, process it either as .astro, .md */
|
||||||
async function transformFromSource(
|
async function transformFromSource(
|
||||||
contents: string,
|
contents: string,
|
||||||
|
@ -87,10 +114,13 @@ async function transformFromSource(
|
||||||
): Promise<TransformResult> {
|
): Promise<TransformResult> {
|
||||||
const fileID = path.relative(projectRoot, filename);
|
const fileID = path.relative(projectRoot, filename);
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case filename.slice(-6) === '.astro':
|
case filename.endsWith('.astro'):
|
||||||
return await convertAstroToJsx(contents, { compileOptions, filename, fileID });
|
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 });
|
return await convertMdToJsx(contents, { compileOptions, filename, fileID });
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -48,7 +48,7 @@ export function searchForPage(url: URL, astroConfig: AstroConfig): SearchResult
|
||||||
|
|
||||||
// Try to find index.astro/md paths
|
// Try to find index.astro/md paths
|
||||||
if (reqPath.endsWith('/')) {
|
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);
|
const location = findAnyPage(candidates, astroConfig);
|
||||||
if (location) {
|
if (location) {
|
||||||
return {
|
return {
|
||||||
|
@ -59,7 +59,7 @@ export function searchForPage(url: URL, astroConfig: AstroConfig): SearchResult
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Try to find the page by its name.
|
// 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);
|
let location = findAnyPage(candidates, astroConfig);
|
||||||
if (location) {
|
if (location) {
|
||||||
return {
|
return {
|
||||||
|
@ -71,7 +71,7 @@ export function searchForPage(url: URL, astroConfig: AstroConfig): SearchResult
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to find name/index.astro/md
|
// 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);
|
const location = findAnyPage(candidates, astroConfig);
|
||||||
if (location) {
|
if (location) {
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Reference in a new issue