Skip to content

Commit

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

    Cleaned code by removing default values from optional config options

commit a9a42072ffe6742a5cedf6cd3f068a415fcb5de3
Author: babakfp <babak.bxf@gmail.com>
Date:   Thu Mar 14 14:42:40 2024 -0700

    Moved plugin types to schema

commit e6c4b0df6e025781d5d7cf38ab870183de7d26d2
Author: babakfp <babak.bxf@gmail.com>
Date:   Thu Mar 14 14:36:46 2024 -0700

    Removed TS `declaration` option
  • Loading branch information
babakfp committed Mar 14, 2024
1 parent b34498e commit be62503
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 111 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

- [ ] 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: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
type ConfigInput,
type ConfigCallbacks,
ConfigSchema,
ConfigOutput,
} from "./types.js"
import { markupPreprocessor } from "./markupPreprocessor.js"

Expand All @@ -14,8 +13,7 @@ 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.
// 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)
const finalConfig = v.parse(ConfigSchema, config)

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

processor.use(remarkParse)

if (config.builtInPlugins.remarkFrontmatter.enable) {
if (config?.builtInPlugins?.remarkFrontmatter?.enable) {
processor.use(remarkFrontmatter, {
type: config.builtInPlugins.remarkFrontmatter.options.lang,
type: config.builtInPlugins.remarkFrontmatter.lang,
fence: { open: "---", close: "---" },
...config.builtInPlugins.remarkFrontmatter.options,
})
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
148 changes: 54 additions & 94 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ 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({}), {}),
vfileMatter: v.optional(
v.object({
options: v.optional(
v.special<VfileMatterYamlOptions>(() => true)
),
})
),

/**
* [View on NPM](https://npmjs.com/package/remark-frontmatter).
Expand All @@ -63,19 +69,15 @@ export const ConfigSchema = v.optional(
v.object({
/** @default true */
enable: v.optional(v.boolean(), true),
// 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.object({
// TODO: Add `"toml"`, `"json"`, `"jsonc"` and `"json5"` support.
/** Only `"yaml"` is supported for now. */
lang: v.optional(
v.union([v.literal("yaml")]),
"yaml"
),
}),
{}
v.special<RemarkFrontmatterCustomOptions>(
() => true
)
),
}),
{}
})
),

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

/**
Expand All @@ -96,15 +100,20 @@ 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({}), {}),
remarkRehype: v.optional(
v.object({
options: v.optional(
v.special<OmittedRemarkRehypeOptions>(() => true)
),
})
),

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

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

/**
Expand All @@ -135,8 +148,11 @@ 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 @@ -147,20 +163,26 @@ 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({}), {}),
}),
{}
rehypeStringify: v.optional(
v.object({
options: v.optional(
v.special<OmittedRehypeStringifyOptions>(() => true)
),
})
),
})
),
}),
{}
})
)

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

// 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
}
}
}
export type ConfigInput = v.Input<typeof ConfigSchema>
export type ConfigOutput = v.Output<typeof ConfigSchema>
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"moduleResolution": "NodeNext",
"outDir": "dist",
"sourceMap": true,
"strict": true,
"declaration": true
"strict": true
},
"include": ["./src/**/*"]
}

0 comments on commit be62503

Please sign in to comment.