Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
babakfp committed Apr 11, 2024
1 parent b3211d1 commit c0ba332
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 54 deletions.
5 changes: 2 additions & 3 deletions src/svelteInMarkdown.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as v from "valibot"
import type { PreprocessorGroup } from "svelte/compiler"

import { type ConfigInput, type ConfigOutput, ConfigSchema } from "./types.js"
import { type ConfigInput, ConfigSchema } from "./types.js"
import { markupPreprocessor } from "./markupPreprocessor.js"

/**
Expand Down Expand Up @@ -36,8 +36,7 @@ import { markupPreprocessor } from "./markupPreprocessor.js"
* ```
*/
export const svelteInMarkdown = (config?: ConfigInput) => {
// TODO: [^1]
const config_: ConfigOutput = v.parse(ConfigSchema, config)
const config_ = v.parse(ConfigSchema, config)

return {
name: "svelte-in-markdown",
Expand Down
5 changes: 2 additions & 3 deletions src/transformers/unified/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type {
ConfigInput as SvelteInMarkdownConfigInput,
ConfigOutput as SvelteInMarkdownConfigOutput,
} from "../../types.js"
import { ConfigSchema, type ConfigInput, type ConfigOutput } from "./types.js"
import { ConfigSchema, type ConfigInput } from "./types.js"
import { isHrefExternal } from "./isHrefExternal.js"

/**
Expand All @@ -38,8 +38,7 @@ export const transformer = (async (
svelteInMarkdownConfig: SvelteInMarkdownConfigOutput,
config?: ConfigInput
) => {
// TODO: [^1]
const config_: ConfigOutput = v.parse(ConfigSchema, config)
const config_ = v.parse(ConfigSchema, config)

const processor = unified()

Expand Down
112 changes: 64 additions & 48 deletions src/transformers/unified/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ const CustomPluginsSchema = v.optional(
)
)

// TODO: https://github.com/microsoft/TypeScript/issues/42873
import * as _1 from "../../../node_modules/.pnpm/yaml@2.4.1/node_modules/yaml/dist/index.js"
import * as _2 from "../../../node_modules/remark-gfm/lib/index.js"
import * as _3 from "../../../node_modules/.pnpm/mdast-util-toc@7.0.0/node_modules/mdast-util-toc/lib/index.js"
import * as _4 from "../../../node_modules/rehype-slug/lib/index.js"
import * as _5 from "../../../node_modules/rehype-autolink-headings/lib/index.js"
import * as _6 from "../../../node_modules/rehype-external-links/lib/index.js"

export const ConfigSchema = v.optional(
v.object(
{
Expand All @@ -48,6 +56,12 @@ export const ConfigSchema = v.optional(
"yaml"
),

options: v.optional(
v.special<RemarkFrontmatterCustomOptions>(
() => true
)
),

/** Useful to add a plugin before or after this plugin. */
plugins: CustomPluginsSchema,
},
Expand All @@ -66,6 +80,12 @@ export const ConfigSchema = v.optional(
/** @default true */
enable: v.optional(v.boolean(), true),

options: v.optional(
v.special<RemarkFrontmatterYamlOptions>(
() => true
)
),

/** Useful to add a plugin before or after this plugin. */
plugins: CustomPluginsSchema,
},
Expand All @@ -81,6 +101,10 @@ export const ConfigSchema = v.optional(
/** @default true */
enable: v.optional(v.boolean(), true),

options: v.optional(
v.special<RemarkGfmOptions>(() => true)
),

/** Useful to add a plugin before or after this plugin. */
plugins: CustomPluginsSchema,
},
Expand Down Expand Up @@ -125,6 +149,10 @@ export const ConfigSchema = v.optional(
/** @default true */
enable: v.optional(v.boolean(), true),

options: v.optional(
v.special<RemarkTocOptions>(() => true)
),

/** Useful to add a plugin before or after this plugin. */
plugins: CustomPluginsSchema,
},
Expand All @@ -140,6 +168,12 @@ export const ConfigSchema = v.optional(
remarkRehype: v.optional(
v.object(
{
options: v.optional(
v.special<OmittedRemarkRehypeOptions>(
() => true
)
),

/** Useful to add a plugin before or after this plugin. */
plugins: CustomPluginsSchema,
},
Expand Down Expand Up @@ -167,6 +201,10 @@ export const ConfigSchema = v.optional(
/** @default true */
enable: v.optional(v.boolean(), true),

options: v.optional(
v.special<RehypeSlugOptions>(() => true)
),

/** Useful to add a plugin before or after this plugin. */
plugins: CustomPluginsSchema,
},
Expand Down Expand Up @@ -208,6 +246,12 @@ export const ConfigSchema = v.optional(
/** @default false */
enable: v.optional(v.boolean(), false),

options: v.optional(
v.special<RehypeAutolinkHeadingsOptions>(
() => true
)
),

/** Useful to add a plugin before or after this plugin. */
plugins: CustomPluginsSchema,
},
Expand Down Expand Up @@ -245,6 +289,12 @@ export const ConfigSchema = v.optional(
/** @default true */
enable: v.optional(v.boolean(), true),

options: v.optional(
v.special<RehypeShikiOptions>(
() => true
)
),

/** Useful to add a plugin before or after this plugin. */
plugins: CustomPluginsSchema,
},
Expand Down Expand Up @@ -297,6 +347,12 @@ export const ConfigSchema = v.optional(
/** @default true */
enable: v.optional(v.boolean(), true),

options: v.optional(
v.special<RehypeExternalLinksOptions>(
() => true
)
),

/** Useful to add a plugin before or after this plugin. */
plugins: CustomPluginsSchema,
},
Expand All @@ -312,6 +368,12 @@ export const ConfigSchema = v.optional(
rehypeStringify: v.optional(
v.object(
{
options: v.optional(
v.special<OmittedRehypeStringifyOptions>(
() => true
)
),

/** Useful to add a plugin before or after this plugin. */
plugins: CustomPluginsSchema,
},
Expand Down Expand Up @@ -367,51 +429,5 @@ type OmittedRehypeStringifyOptions = Omit<
"allowDangerousCharacters" | "allowDangerousHtml"
>

// TODO: [^1]
type BuiltInPluginsOptions = {
builtInPlugins: {
remarkFrontmatter: {
options?: RemarkFrontmatterCustomOptions
}
remarkFrontmatterYaml: {
options?: RemarkFrontmatterYamlOptions
}
remarkGfm: {
options?: RemarkGfmOptions
}
remarkToc: {
options?: RemarkTocOptions
}
remarkRehype: {
options?: OmittedRemarkRehypeOptions
}
rehypeSlug: {
options?: RehypeSlugOptions
}
rehypeAutolinkHeadings: {
options?: RehypeAutolinkHeadingsOptions
}
rehypeShiki: {
options?: RehypeShikiOptions
}
rehypeExternalLinks: {
options?: RehypeExternalLinksOptions
}
rehypeStringify: {
options?: OmittedRehypeStringifyOptions
}
}
}

// TODO: [^1]
export type ConfigInput = v.Input<typeof ConfigSchema> &
Partial<BuiltInPluginsOptions>

// TODO: [^1]
export type ConfigOutput = v.Output<typeof ConfigSchema> & BuiltInPluginsOptions

/*
[^1]: TypeScript types with Valibot
- This is how to use TypeScript types with Valibot: https://github.com/fabian-hiller/valibot/discussions/477.
- Whenever https://github.com/microsoft/TypeScript/issues/42873 fixes, move the extra types from `ConfigInput` and `ConfigOutput` to the schema itself.
*/
export type ConfigInput = v.Input<typeof ConfigSchema>
export type ConfigOutput = v.Output<typeof ConfigSchema>

0 comments on commit c0ba332

Please sign in to comment.