Skip to content

Commit

Permalink
feat: add { extends } to markdown config
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Aug 24, 2022
1 parent 62d98e4 commit 5d050bb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
34 changes: 20 additions & 14 deletions packages/astro/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
},
};

const remarkPluginSchema = z
.union([
z.string(),
z.tuple([z.string(), z.any()]),
z.custom<RemarkPlugin>((data) => typeof data === 'function'),
z.tuple([z.custom<RemarkPlugin>((data) => typeof data === 'function'), z.any()]),
])
.array();

const rehypePluginSchema = z
.union([
z.string(),
z.tuple([z.string(), z.any()]),
z.custom<RehypePlugin>((data) => typeof data === 'function'),
z.tuple([z.custom<RehypePlugin>((data) => typeof data === 'function'), z.any()]),
])
.array();

async function resolvePostcssConfig(inlineOptions: any, root: URL): Promise<PostCSSConfigResult> {
if (isObject(inlineOptions)) {
const options = { ...inlineOptions };
Expand Down Expand Up @@ -197,22 +215,10 @@ export const AstroConfigSchema = z.object({
})
.default({}),
remarkPlugins: z
.union([
z.string(),
z.tuple([z.string(), z.any()]),
z.custom<RemarkPlugin>((data) => typeof data === 'function'),
z.tuple([z.custom<RemarkPlugin>((data) => typeof data === 'function'), z.any()]),
])
.array()
.union([remarkPluginSchema, z.object({ extends: remarkPluginSchema })])
.default(ASTRO_CONFIG_DEFAULTS.markdown.remarkPlugins),
rehypePlugins: z
.union([
z.string(),
z.tuple([z.string(), z.any()]),
z.custom<RehypePlugin>((data) => typeof data === 'function'),
z.tuple([z.custom<RehypePlugin>((data) => typeof data === 'function'), z.any()]),
])
.array()
.union([rehypePluginSchema, z.object({ extends: rehypePluginSchema })])
.default(ASTRO_CONFIG_DEFAULTS.markdown.rehypePlugins),
})
.default({}),
Expand Down
21 changes: 17 additions & 4 deletions packages/markdown/remark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ import markdown from 'remark-parse';
import markdownToHtml from 'remark-rehype';
import { unified } from 'unified';
import { VFile } from 'vfile';
import type { WithExtends } from './types.js';

export * from './types.js';

export const DEFAULT_REMARK_PLUGINS = ['remark-gfm', 'remark-smartypants'];
export const DEFAULT_REHYPE_PLUGINS = [];

function handleExtends<T>(config: WithExtends<T[] | undefined>, defaults: T[] = []): T[] {
if (Array.isArray(config)) return config;

return [...defaults, ...(config?.extends ?? [])];
}

/** Shared utility for rendering markdown */
export async function renderMarkdown(
content: string,
Expand All @@ -49,13 +56,19 @@ export async function renderMarkdown(
.use(remarkInitializeAstroData)
.use(isAstroFlavoredMd ? [remarkMdxish, remarkMarkAndUnravel, remarkUnwrap, remarkEscape] : []);

if (remarkPlugins.length === 0 && rehypePlugins.length === 0) {
if (Array.isArray(remarkPlugins) && remarkPlugins.length === 0) {
remarkPlugins = [...DEFAULT_REMARK_PLUGINS];
rehypePlugins = [...DEFAULT_REHYPE_PLUGINS];
}
if (Array.isArray(rehypePlugins) && rehypePlugins.length === 0) {
remarkPlugins = [...DEFAULT_REHYPE_PLUGINS];
}

const loadedRemarkPlugins = await Promise.all(loadPlugins(remarkPlugins));
const loadedRehypePlugins = await Promise.all(loadPlugins(rehypePlugins));
const loadedRemarkPlugins = await Promise.all(
loadPlugins(handleExtends(remarkPlugins, DEFAULT_REMARK_PLUGINS))
);
const loadedRehypePlugins = await Promise.all(
loadPlugins(handleExtends(rehypePlugins, DEFAULT_REHYPE_PLUGINS))
);

loadedRemarkPlugins.forEach(([plugin, pluginOpts]) => {
parser.use([[plugin, pluginOpts]]);
Expand Down
6 changes: 4 additions & 2 deletions packages/markdown/remark/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ export interface ShikiConfig {
wrap?: boolean | null;
}

export type WithExtends<T> = T | { extends: T };

export interface AstroMarkdownOptions {
mode?: 'md' | 'mdx';
drafts?: boolean;
syntaxHighlight?: 'shiki' | 'prism' | false;
shikiConfig?: ShikiConfig;
remarkPlugins?: RemarkPlugins;
rehypePlugins?: RehypePlugins;
remarkPlugins?: WithExtends<RemarkPlugins>;
rehypePlugins?: WithExtends<RehypePlugins>;
}

export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
Expand Down

0 comments on commit 5d050bb

Please sign in to comment.