Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 8006c083ddddf6cba04007e464b1932bc3de8b9f
Author: babakfp <babak.bxf@gmail.com>
Date:   Fri Mar 15 15:40:54 2024 -0700

    Added todo, formatted import order, updated config schema.

commit b14eb14ca6b8705d55fd18b7057bcf0cefcbc7e7
Author: babakfp <babak.bxf@gmail.com>
Date:   Fri Mar 15 15:34:34 2024 -0700

    Revert "Squashed commit of the following:"

    This reverts commit be62503.

Because of: microsoft/TypeScript#42873
  • Loading branch information
babakfp committed Mar 15, 2024
1 parent be62503 commit 01499f9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 67 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
- [ ] Add lots of JSDoc comments.
- [ ] A Prettier plugin for Svelte inside Markdown.
- [ ] Should the `declarationMap` option be set to `true` in `tsconfig.json`.
- [ ] Add back the `declaration` option to `tsconfig.json` and fix type issues.
2 changes: 1 addition & 1 deletion src/isFileIgnored.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const isFileIgnored = (
return true
}

for (const extension of config?.extensions ?? []) {
for (const extension of config.extensions) {
if (!filename.endsWith(extension)) {
return true
}
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as v from "valibot"

import {
type ConfigInput,
type ConfigOutput,
type ConfigCallbacks,
ConfigSchema,
} from "./types.js"
Expand All @@ -13,7 +14,8 @@ export const svelteInMarkdown = (
callbacks?: ConfigCallbacks
) => {
// NOTE: I created this new variable because TypeScript is stupid and complains about types. We receive the expected values and their types by parsing, but when assigning the variable to the parsed result, TypeScript still complains that the values may be nullable.
const finalConfig = v.parse(ConfigSchema, config)
// NOTE: Added the explicit type because generating Valibot schema with TypeScript types is impossible. Some of the types are added the schema and some other by TypeScript. https://github.com/fabian-hiller/valibot/discussions/477
const finalConfig: ConfigOutput = v.parse(ConfigSchema, config)

return {
name: "svelte-in-markdown",
Expand Down
20 changes: 10 additions & 10 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const transformer = async (

processor.use(remarkParse)

if (config?.builtInPlugins?.remarkFrontmatter?.enable) {
if (config.builtInPlugins.remarkFrontmatter.enable) {
processor.use(remarkFrontmatter, {
type: config.builtInPlugins.remarkFrontmatter.lang,
fence: { open: "---", close: "---" },
Expand All @@ -35,37 +35,37 @@ export const transformer = async (
// NOTE: The content is striped no matter the value of this option (`strip`).
// NOTE: The content won't be striped if the `type` option in `remarkFrontmatter` is set to anything other than `"yaml"`.
strip: true,
yaml: config?.builtInPlugins?.vfileMatter?.options,
yaml: config.builtInPlugins.vfileMatter.options,
})
}
})
}

if (config?.builtInPlugins?.remarkGfm?.enable) {
if (config.builtInPlugins.remarkGfm.enable) {
processor.use(remarkGfm, config.builtInPlugins.remarkGfm.options)
}

if (config?.builtInPlugins?.remarkUnwrapImages?.enable) {
if (config.builtInPlugins.remarkUnwrapImages.enable) {
processor.use(remarkUnwrapImages)
}

processor.use(remarkRehype, {
...config?.builtInPlugins?.remarkRehype?.options,
...config.builtInPlugins.remarkRehype.options,
allowDangerousHtml: true,
})

if (config?.builtInPlugins?.rehypeSlug?.enable) {
if (config.builtInPlugins.rehypeSlug.enable) {
processor.use(rehypeSlug, config.builtInPlugins.rehypeSlug.options)
}

if (config?.builtInPlugins?.rehypeAutolinkHeadings?.enable) {
if (config.builtInPlugins.rehypeAutolinkHeadings.enable) {
processor.use(
rehypeAutolinkHeadings,
config.builtInPlugins.rehypeAutolinkHeadings.options
)
}

if (config?.builtInPlugins?.rehypeShiki?.enable) {
if (config.builtInPlugins.rehypeShiki.enable) {
processor.use(rehypeShiki, {
themes: {
light: "vitesse-light",
Expand All @@ -75,7 +75,7 @@ export const transformer = async (
})
}

if (config?.builtInPlugins?.rehypeExternalLinks?.enable) {
if (config.builtInPlugins.rehypeExternalLinks.enable) {
processor.use(rehypeExternalLinks, {
rel: (element) => {
if (isHrefExternal(element.properties.href?.toString())) {
Expand All @@ -92,7 +92,7 @@ export const transformer = async (
}

processor.use(rehypeStringify, {
...config?.builtInPlugins?.rehypeStringify?.options,
...config.builtInPlugins.rehypeStringify.options,
allowDangerousCharacters: true,
allowDangerousHtml: true,
})
Expand Down
141 changes: 88 additions & 53 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ export const ConfigSchema = v.optional(
* [View on NPM](https://npmjs.com/package/vfile-matter).
* Can be disabled by disabling the `remarkFrontmatter` plugin.
*/
vfileMatter: v.optional(
v.object({
options: v.optional(
v.special<VfileMatterYamlOptions>(() => true)
),
})
),
vfileMatter: v.optional(v.object({}), {}),

/**
* [View on NPM](https://npmjs.com/package/remark-frontmatter).
Expand All @@ -72,12 +66,9 @@ export const ConfigSchema = v.optional(
// TODO: Add `"toml"`, `"json"`, `"jsonc"` and `"json5"` support.
/** Only `"yaml"` is supported for now. */
lang: v.optional(v.union([v.literal("yaml")]), "yaml"),
options: v.optional(
v.special<RemarkFrontmatterCustomOptions>(
() => true
)
),
})
options: v.optional(v.object({}), {}),
}),
{}
),

/**
Expand All @@ -87,10 +78,8 @@ export const ConfigSchema = v.optional(
v.object({
/** @default true */
enable: v.optional(v.boolean(), true),
options: v.optional(
v.special<RemarkGfmOptions>(() => true)
),
})
}),
{}
),

/**
Expand All @@ -100,20 +89,15 @@ export const ConfigSchema = v.optional(
v.object({
/** @default true */
enable: v.optional(v.boolean(), true),
})
}),
{}
),

/**
* [View on NPM](https://npmjs.com/package/remark-rehype).
* Can't be disabled.
*/
remarkRehype: v.optional(
v.object({
options: v.optional(
v.special<OmittedRemarkRehypeOptions>(() => true)
),
})
),
remarkRehype: v.optional(v.object({}), {}),

/**
* [View on NPM](https://npmjs.com/package/rehype-slug).
Expand All @@ -122,10 +106,8 @@ export const ConfigSchema = v.optional(
v.object({
/** @default false */
enable: v.optional(v.boolean(), false),
options: v.optional(
v.special<RehypeSlugOptions>(() => true)
),
})
}),
{}
),

/**
Expand All @@ -135,10 +117,8 @@ export const ConfigSchema = v.optional(
v.object({
/** @default false */
enable: v.optional(v.boolean(), false),
options: v.optional(
v.special<RehypeAutolinkHeadingsOptions>(() => true)
),
})
}),
{}
),

/**
Expand All @@ -148,11 +128,8 @@ export const ConfigSchema = v.optional(
v.object({
/** @default true */
enable: v.optional(v.boolean(), true),
options: v.optional(
v.special<RehypeShikiOptions>(() => true),
undefined
),
})
}),
{}
),

/**
Expand All @@ -163,26 +140,20 @@ export const ConfigSchema = v.optional(
v.object({
/** @default true */
enable: v.optional(v.boolean(), true),
options: v.optional(
v.special<RehypeExternalLinksOptions>(() => true)
),
})
}),
{}
),

/**
* [View on NPM](https://npmjs.com/package/rehype-stringify).
* Can't be disabled.
*/
rehypeStringify: v.optional(
v.object({
options: v.optional(
v.special<OmittedRehypeStringifyOptions>(() => true)
),
})
),
})
rehypeStringify: v.optional(v.object({}), {}),
}),
{}
),
})
}),
{}
)

// The original types for options suck, this way users will have easier type configuring their custom options.
Expand Down Expand Up @@ -213,5 +184,69 @@ type OmittedRehypeStringifyOptions = Omit<
"allowDangerousCharacters" | "allowDangerousHtml"
>

export type ConfigInput = v.Input<typeof ConfigSchema>
export type ConfigOutput = v.Output<typeof ConfigSchema>
// NOTE: Generating Valibot schema with TypeScript types is impossible. https://github.com/fabian-hiller/valibot/discussions/477
export type ConfigInput = v.Input<typeof ConfigSchema> & {
builtInPlugins?: {
vfileMatter?: {
options?: VfileMatterYamlOptions
}
remarkFrontmatter?: {
options?: RemarkFrontmatterCustomOptions
}
remarkGfm?: {
options?: RemarkGfmOptions
}
remarkRehype?: {
options?: OmittedRemarkRehypeOptions
}
rehypeSlug?: {
options?: RehypeSlugOptions
}
rehypeAutolinkHeadings?: {
options?: RehypeAutolinkHeadingsOptions
}
rehypeShiki?: {
options?: RehypeShikiOptions
}
rehypeExternalLinks?: {
options?: RehypeExternalLinksOptions
}
rehypeStringify?: {
options?: OmittedRehypeStringifyOptions
}
}
}
// NOTE: Generating Valibot schema with TypeScript types is impossible. https://github.com/fabian-hiller/valibot/discussions/477
export type ConfigOutput = v.Output<typeof ConfigSchema> & {
builtInPlugins: {
vfileMatter: {
options?: VfileMatterYamlOptions
}
remarkFrontmatter: {
options?: RemarkFrontmatterCustomOptions
}
remarkGfm: {
options?: RemarkGfmOptions
}
remarkRehype: {
options?: OmittedRemarkRehypeOptions
}
rehypeSlug: {
options?: RehypeSlugOptions
}
rehypeAutolinkHeadings: {
options?: RehypeAutolinkHeadingsOptions
}
rehypeShiki: {
options?: RehypeShikiOptions
}
rehypeExternalLinks: {
options?: RehypeExternalLinksOptions
}
rehypeStringify: {
options?: OmittedRehypeStringifyOptions
}
}
}

// TODO: Whenever [this issue](https://github.com/microsoft/TypeScript/issues/42873) resolves, I can move the extra types from `ConfigInput` and `ConfigOutput` to the schema itself.
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"moduleResolution": "NodeNext",
"outDir": "dist",
"sourceMap": true,
"strict": true
"strict": true,
"declaration": true
},
"include": ["./src/**/*"]
}

0 comments on commit 01499f9

Please sign in to comment.