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

Add markdown options to AuthorOptions and add example usage #187

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/weak-lemons-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-theme-provider": minor
---

Add markdown options to AuthorOptions and add example usage
73 changes: 71 additions & 2 deletions package/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from "node:fs";
import { basename, extname, join, resolve } from "node:path";
import type { AstroIntegration } from "astro";
import type { AstroConfig, AstroIntegration } from "astro";
import { addIntegration, addVitePlugin, watchDirectory } from "astro-integration-kit";
import { addPageDir } from "astro-pages";
import type { IntegrationOption as PageDirIntegrationOption } from "astro-pages";
Expand Down Expand Up @@ -29,6 +29,7 @@ import {
validatePattern,
} from "./utils/path.js";
import { createVirtualResolver } from "./utils/resolver.ts";
import type { DeepPartial } from "./utils/type-utils.ts";

const thisFile = resolveFilepath("./", import.meta.url);

Expand All @@ -48,6 +49,7 @@ export default function <ThemeName extends string, Schema extends z.ZodTypeAny>(
middlewareDir = "./",
imports: themeImports = {},
integrations: themeIntegrations = [],
markdown: markdownOptions = {},
} = partialAuthorOptions;

themeEntrypoint = resolveFilepath("./", themeEntrypoint);
Expand Down Expand Up @@ -140,7 +142,7 @@ export default function <ThemeName extends string, Schema extends z.ZodTypeAny>(
if (existsSync(seedEntrypoint)) extendDb({ seedEntrypoint });
},
"astro:config:setup": (params) => {
const { config, logger, injectRoute, addMiddleware } = params;
const { config, logger, injectRoute, addMiddleware, updateConfig } = params;

const projectRoot = resolveDirectory("./", config.root);

Expand Down Expand Up @@ -232,6 +234,73 @@ export default function <ThemeName extends string, Schema extends z.ZodTypeAny>(
interfaceBuffers.ThemeIntegrationsResolved =
`${JSON.stringify({ ...integrationsInjected, ...integrationsIgnored }, null, 4).slice(1, -1)}` || "\n";

// Add markdown options
if (markdownOptions) {
let markdown:
| ReturnType<Extract<typeof markdownOptions, (...args: any[]) => any>>
| typeof markdownOptions
| false
| null
| undefined;
const results: DeepPartial<AstroConfig["markdown"]> = {};
if (typeof markdownOptions === "function")
markdown = markdownOptions({ config: userConfig, astroConfig: config });
else markdown = markdownOptions;

if (markdown) {
const { syntaxHighlight, shikiConfig, rehypePlugins, remarkPlugins, remarkRehype, gfm, smartypants } =
markdown;
if (syntaxHighlight && userConfig.markdown?.syntaxHighlight !== false) {
if (typeof syntaxHighlight === "function") {
const r = syntaxHighlight({ config: userConfig, astroConfig: config });
if (r) results.syntaxHighlight = r;
} else results.syntaxHighlight = syntaxHighlight;
}
if (shikiConfig && userConfig.markdown?.shikiConfig !== false) {
if (typeof shikiConfig === "function") {
const r = shikiConfig({ config: userConfig, astroConfig: config });
if (r) results.shikiConfig = r;
} else results.shikiConfig = shikiConfig;
}
function rmInvalid<T extends T[number][]>(r: T): Array<Exclude<NonNullable<T[number]>, false>> {
return r.filter((i) => i != null && i !== false) as Array<Exclude<NonNullable<T[number]>, false>>;
}
if (rehypePlugins && userConfig.markdown?.rehypePlugins !== false) {
if (typeof rehypePlugins === "function") {
const r = rehypePlugins({ config: userConfig, astroConfig: config });
if (r) results.rehypePlugins = rmInvalid(r);
} else results.rehypePlugins = rmInvalid(rehypePlugins);
}
if (remarkPlugins && userConfig.markdown?.remarkPlugins !== false) {
if (typeof remarkPlugins === "function") {
const r = remarkPlugins({ config: userConfig, astroConfig: config });
if (r) results.remarkPlugins = rmInvalid(r);
} else results.remarkPlugins = rmInvalid(remarkPlugins);
}
if (remarkRehype && userConfig.markdown?.remarkRehype !== false) {
if (typeof remarkRehype === "function") {
const r = remarkRehype({ config: userConfig, astroConfig: config });
if (r) results.remarkRehype = r;
} else results.remarkRehype = remarkRehype;
}
if (typeof gfm === "function" && userConfig.markdown?.gfm !== false) {
const r = gfm({ config: userConfig, astroConfig: config });
if (typeof r === "boolean") results.gfm = r;
} else if (typeof gfm === "boolean") {
results.gfm = gfm;
}
if (typeof smartypants === "function" && userConfig.markdown?.smartypants !== false) {
const r = smartypants({ config: userConfig, astroConfig: config });
if (typeof r === "boolean") results.smartypants = r;
} else if (typeof smartypants === "boolean") {
results.smartypants = smartypants;
}

updateConfig({ markdown: results });
}
}


// Add middleware
if (middlewareDir) {
const middlewareGlob = ["middleware.{ts,js}", "middleware/*{ts,js}", GLOB_IGNORE].flat();
Expand Down
53 changes: 52 additions & 1 deletion package/src/internal/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { AstroIntegration } from "astro";
import type { AstroConfig, AstroIntegration } from "astro";
import type { Option as PageDirOption } from "astro-pages";
import type { Option as StaticDirOption } from "astro-public/types";
import type { z } from "astro/zod";
import type { ModuleExports, ModuleImports, ModuleObject } from "../utils/modules.ts";
import type { DeepPartial } from "../utils/type-utils.ts";

export type ValueOrArray<T> = T | ValueOrArray<T>[];

Expand Down Expand Up @@ -44,6 +45,56 @@ export type AuthorOptions<ThemeName extends string, Schema extends z.ZodTypeAny>
| undefined
| void)
>;
markdown?:
| DeepPartial<AstroConfig["markdown"]>
| ((options: { config: z.infer<Schema>; astroConfig: AstroConfig }) =>
| DeepPartial<AstroConfig["markdown"]>
| false
| null
| undefined)
| {
syntaxHighlight?:
| DeepPartial<AstroConfig["markdown"]["syntaxHighlight"]>
| ((options: { config: z.infer<Schema>; astroConfig: AstroConfig }) =>
| DeepPartial<AstroConfig["markdown"]["syntaxHighlight"]>
| false
| null
| undefined);
shikiConfig?:
| DeepPartial<AstroConfig["markdown"]["shikiConfig"]>
| ((options: { config: z.infer<Schema>; astroConfig: AstroConfig }) =>
| DeepPartial<AstroConfig["markdown"]["shikiConfig"]>
| false
| null
| undefined);
remarkPlugins?:
| (false | undefined | null | AstroConfig["markdown"]["remarkPlugins"][number])[]
| ((options: { config: z.infer<Schema>; astroConfig: AstroConfig }) =>
| (false | undefined | null | AstroConfig["markdown"]["remarkPlugins"][number])[]
| false
| null
| undefined);
rehypePlugins?:
| (false | undefined | null | AstroConfig["markdown"]["rehypePlugins"][number])[]
| ((options: { config: z.infer<Schema>; astroConfig: AstroConfig }) =>
| (false | undefined | null | AstroConfig["markdown"]["rehypePlugins"][number])[]
| false
| null
| undefined);
remarkRehype?:
| DeepPartial<AstroConfig["markdown"]["remarkRehype"]>
| ((options: { config: z.infer<Schema>; astroConfig: AstroConfig }) =>
| DeepPartial<AstroConfig["markdown"]["remarkRehype"]>
| false
| null
| undefined);
gfm?:
| boolean
| ((options: { config: z.infer<Schema>; astroConfig: AstroConfig }) => boolean | null | undefined);
smartypants?:
| boolean
| ((options: { config: z.infer<Schema>; astroConfig: AstroConfig }) => boolean | null | undefined);
};
}>;

export type UserOptions<ThemeName extends string, Schema extends z.ZodTypeAny = z.ZodTypeAny> = {
Expand Down
7 changes: 7 additions & 0 deletions package/src/utils/type-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? DeepPartial<U>[]
: T[P] extends object | undefined
? DeepPartial<T[P]>
: T[P];
};
3 changes: 2 additions & 1 deletion playground/astro.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export default defineConfig({
config: {
title: "Hey!",
description: "This is a theme created using",
// sitemap: false
// sitemap: false,
// toc: false,
},
pages: {
// '/cats': '/dogs',
Expand Down
14 changes: 14 additions & 0 deletions playground/src/pages/toc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

using `toc` heading to generate a table of contents

## toc

## h2-1

### h3-1-1

#### h4-1-1-1

## h2-2

### h3-2-1
Loading