Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: markdown config typechecking #2970

Merged
merged 19 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Removed the ability to have a custom markdown renderer internally
  • Loading branch information
JuanM04 committed Apr 11, 2022
commit 210496f80aa9c797d35fb77181baedbdbedd17e6
2 changes: 1 addition & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class App {
legacyBuild: false,
links,
logging: this.#logging,
markdownRender: manifest.markdown.render,
markdown: manifest.markdown,
mod,
origin: url.origin,
pathname: url.pathname,
Expand Down
6 changes: 2 additions & 4 deletions packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {
RouteData,
SerializedRouteData,
MarkdownRenderOptions,
ComponentInstance,
SSRLoadedRenderer,
} from '../../@types/astro';
import type { MarkdownRenderingOptions } from '@astrojs/markdown-remark';

export type ComponentPath = string;

Expand All @@ -22,9 +22,7 @@ export type SerializedRouteInfo = Omit<RouteInfo, 'routeData'> & {
export interface SSRManifest {
routes: RouteInfo[];
site?: string;
markdown: {
render: MarkdownRenderOptions;
};
markdown: MarkdownRenderingOptions;
pageMap: Map<ComponentPath, ComponentInstance>;
renderers: SSRLoadedRenderer[];
entryModules: Record<string, string>;
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import astroRemark from '@astrojs/markdown-remark';
import fs from 'fs';
import { bgGreen, black, cyan, dim, green, magenta } from 'kleur/colors';
import npath from 'path';
Expand Down Expand Up @@ -197,7 +196,7 @@ async function generatePath(
legacyBuild: false,
links,
logging,
markdownRender: [astroRemark, astroConfig.markdown],
markdown: astroConfig.markdown,
mod,
origin,
pathname,
Expand Down
5 changes: 1 addition & 4 deletions packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import astroRemark from '@astrojs/markdown-remark';
import type { Plugin as VitePlugin } from 'vite';
import type { BuildInternals } from './internal.js';
import type { AstroAdapter } from '../../@types/astro';
Expand Down Expand Up @@ -110,9 +109,7 @@ function buildManifest(opts: StaticBuildOptions, internals: BuildInternals): Ser
const ssrManifest: SerializedSSRManifest = {
routes,
site: astroConfig.site,
markdown: {
render: [astroRemark, astroConfig.markdown],
},
markdown: astroConfig.markdown,
pageMap: null as any,
renderers: [],
entryModules,
Expand Down
9 changes: 4 additions & 5 deletions packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type {
ComponentInstance,
EndpointHandler,
MarkdownRenderOptions,
Params,
Props,
SSRLoadedRenderer,
RouteData,
SSRElement,
} from '../../@types/astro';
import type { MarkdownRenderingOptions } from '@astrojs/markdown-remark';
import type { LogOptions } from '../logger/core.js';

import { renderHead, renderPage } from '../../runtime/server/index.js';
Expand Down Expand Up @@ -70,7 +69,7 @@ export interface RenderOptions {
legacyBuild: boolean;
logging: LogOptions;
links: Set<SSRElement>;
markdownRender: MarkdownRenderOptions;
markdown: MarkdownRenderingOptions;
mod: ComponentInstance;
origin: string;
pathname: string;
Expand All @@ -92,7 +91,7 @@ export async function render(
links,
logging,
origin,
markdownRender,
markdown,
mod,
pathname,
scripts,
Expand Down Expand Up @@ -132,7 +131,7 @@ export async function render(
legacyBuild,
links,
logging,
markdownRender,
markdown,
origin,
params,
pathname,
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/render/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import astroRemark from '@astrojs/markdown-remark';
import { fileURLToPath } from 'url';
import type * as vite from 'vite';
import type {
Expand Down Expand Up @@ -164,7 +163,7 @@ export async function render(
legacyBuild: isLegacyBuild,
links,
logging,
markdownRender: [astroRemark, astroConfig.markdown],
markdown: astroConfig.markdown,
mod,
origin,
pathname,
Expand Down
32 changes: 5 additions & 27 deletions packages/astro/src/core/render/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { bold } from 'kleur/colors';
import type {
AstroGlobal,
AstroGlobalPartial,
MarkdownParser,
MarkdownRenderOptions,
Params,
SSRElement,
SSRLoadedRenderer,
SSRResult,
} from '../../@types/astro';
import { renderMarkdown, MarkdownRenderingOptions } from '@astrojs/markdown-remark';
import { renderSlot } from '../../runtime/server/index.js';
import { LogOptions, warn } from '../logger/core.js';
import { createCanonicalURL, isCSSRequest } from './util.js';
Expand All @@ -26,7 +25,7 @@ export interface CreateResultArgs {
legacyBuild: boolean;
logging: LogOptions;
origin: string;
markdownRender: MarkdownRenderOptions;
markdown: MarkdownRenderingOptions;
params: Params;
pathname: string;
renderers: SSRLoadedRenderer[];
Expand Down Expand Up @@ -100,7 +99,7 @@ class Slots {
}

export function createResult(args: CreateResultArgs): SSRResult {
const { legacyBuild, markdownRender, params, pathname, renderers, request, resolve, site } = args;
const { legacyBuild, markdown, params, pathname, renderers, request, resolve, site } = args;

const url = new URL(request.url);
const canonicalURL = createCanonicalURL('.' + pathname, site ?? url.origin);
Expand Down Expand Up @@ -181,29 +180,8 @@ ${extra}`
writable: false,
// TODO: remove 1. markdown parser logic 2. update MarkdownRenderOptions to take a function only
// <Markdown> also needs the same `astroConfig.markdownOptions.render` as `.md` pages
value: async function (content: string, opts: any) {
let [mdRender, renderOpts] = markdownRender;
let parser: MarkdownParser | null = null;
//let renderOpts = {};
if (Array.isArray(mdRender)) {
renderOpts = mdRender[1];
mdRender = mdRender[0];
}
// ['rehype-toc', opts]
if (typeof mdRender === 'string') {
const mod: { default: MarkdownParser } = await import(mdRender);
parser = mod.default;
}
// [import('rehype-toc'), opts]
else if (mdRender instanceof Promise) {
const mod: { default: MarkdownParser } = await mdRender;
parser = mod.default;
} else if (typeof mdRender === 'function') {
parser = mdRender;
} else {
throw new Error('No Markdown parser found.');
}
const { code } = await parser(content, { ...renderOpts, ...(opts ?? {}) });
value: async function (content: string, opts: MarkdownRenderingOptions) {
const { code } = await renderMarkdown(content, { ...markdown, ...(opts ?? {}) });
return code;
},
});
Expand Down