Skip to content

Commit be62503

Browse files
committed
Squashed commit of the following:
commit 54fa83d7770fcc93d676bbef1d8f798946a7f5c8 Author: babakfp <[email protected]> Date: Thu Mar 14 14:51:40 2024 -0700 Cleaned code by removing default values from optional config options commit a9a42072ffe6742a5cedf6cd3f068a415fcb5de3 Author: babakfp <[email protected]> Date: Thu Mar 14 14:42:40 2024 -0700 Moved plugin types to schema commit e6c4b0df6e025781d5d7cf38ab870183de7d26d2 Author: babakfp <[email protected]> Date: Thu Mar 14 14:36:46 2024 -0700 Removed TS `declaration` option
1 parent b34498e commit be62503

File tree

6 files changed

+70
-111
lines changed

6 files changed

+70
-111
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44

55
- [ ] Add lots of JSDoc comments.
66
- [ ] A Prettier plugin for Svelte inside Markdown.
7+
- [ ] Should the `declarationMap` option be set to `true` in `tsconfig.json`.
8+
- [ ] Add back the `declaration` option to `tsconfig.json` and fix type issues.

src/isFileIgnored.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const isFileIgnored = (
1616
return true
1717
}
1818

19-
for (const extension of config.extensions) {
19+
for (const extension of config?.extensions ?? []) {
2020
if (!filename.endsWith(extension)) {
2121
return true
2222
}

src/main.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
type ConfigInput,
66
type ConfigCallbacks,
77
ConfigSchema,
8-
ConfigOutput,
98
} from "./types.js"
109
import { markupPreprocessor } from "./markupPreprocessor.js"
1110

@@ -14,8 +13,7 @@ export const svelteInMarkdown = (
1413
callbacks?: ConfigCallbacks
1514
) => {
1615
// 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.
17-
// 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
18-
const finalConfig: ConfigOutput = v.parse(ConfigSchema, config)
16+
const finalConfig = v.parse(ConfigSchema, config)
1917

2018
return {
2119
name: "svelte-in-markdown",

src/transformer.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export const transformer = async (
2323

2424
processor.use(remarkParse)
2525

26-
if (config.builtInPlugins.remarkFrontmatter.enable) {
26+
if (config?.builtInPlugins?.remarkFrontmatter?.enable) {
2727
processor.use(remarkFrontmatter, {
28-
type: config.builtInPlugins.remarkFrontmatter.options.lang,
28+
type: config.builtInPlugins.remarkFrontmatter.lang,
2929
fence: { open: "---", close: "---" },
3030
...config.builtInPlugins.remarkFrontmatter.options,
3131
})
@@ -35,37 +35,37 @@ export const transformer = async (
3535
// NOTE: The content is striped no matter the value of this option (`strip`).
3636
// NOTE: The content won't be striped if the `type` option in `remarkFrontmatter` is set to anything other than `"yaml"`.
3737
strip: true,
38-
yaml: config.builtInPlugins.vfileMatter.options,
38+
yaml: config?.builtInPlugins?.vfileMatter?.options,
3939
})
4040
}
4141
})
4242
}
4343

44-
if (config.builtInPlugins.remarkGfm.enable) {
44+
if (config?.builtInPlugins?.remarkGfm?.enable) {
4545
processor.use(remarkGfm, config.builtInPlugins.remarkGfm.options)
4646
}
4747

48-
if (config.builtInPlugins.remarkUnwrapImages.enable) {
48+
if (config?.builtInPlugins?.remarkUnwrapImages?.enable) {
4949
processor.use(remarkUnwrapImages)
5050
}
5151

5252
processor.use(remarkRehype, {
53-
...config.builtInPlugins.remarkRehype.options,
53+
...config?.builtInPlugins?.remarkRehype?.options,
5454
allowDangerousHtml: true,
5555
})
5656

57-
if (config.builtInPlugins.rehypeSlug.enable) {
57+
if (config?.builtInPlugins?.rehypeSlug?.enable) {
5858
processor.use(rehypeSlug, config.builtInPlugins.rehypeSlug.options)
5959
}
6060

61-
if (config.builtInPlugins.rehypeAutolinkHeadings.enable) {
61+
if (config?.builtInPlugins?.rehypeAutolinkHeadings?.enable) {
6262
processor.use(
6363
rehypeAutolinkHeadings,
6464
config.builtInPlugins.rehypeAutolinkHeadings.options
6565
)
6666
}
6767

68-
if (config.builtInPlugins.rehypeShiki.enable) {
68+
if (config?.builtInPlugins?.rehypeShiki?.enable) {
6969
processor.use(rehypeShiki, {
7070
themes: {
7171
light: "vitesse-light",
@@ -75,7 +75,7 @@ export const transformer = async (
7575
})
7676
}
7777

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

9494
processor.use(rehypeStringify, {
95-
...config.builtInPlugins.rehypeStringify.options,
95+
...config?.builtInPlugins?.rehypeStringify?.options,
9696
allowDangerousCharacters: true,
9797
allowDangerousHtml: true,
9898
})

src/types.ts

Lines changed: 54 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ export const ConfigSchema = v.optional(
5454
* [View on NPM](https://npmjs.com/package/vfile-matter).
5555
* Can be disabled by disabling the `remarkFrontmatter` plugin.
5656
*/
57-
vfileMatter: v.optional(v.object({}), {}),
57+
vfileMatter: v.optional(
58+
v.object({
59+
options: v.optional(
60+
v.special<VfileMatterYamlOptions>(() => true)
61+
),
62+
})
63+
),
5864

5965
/**
6066
* [View on NPM](https://npmjs.com/package/remark-frontmatter).
@@ -63,19 +69,15 @@ export const ConfigSchema = v.optional(
6369
v.object({
6470
/** @default true */
6571
enable: v.optional(v.boolean(), true),
72+
// TODO: Add `"toml"`, `"json"`, `"jsonc"` and `"json5"` support.
73+
/** Only `"yaml"` is supported for now. */
74+
lang: v.optional(v.union([v.literal("yaml")]), "yaml"),
6675
options: v.optional(
67-
v.object({
68-
// TODO: Add `"toml"`, `"json"`, `"jsonc"` and `"json5"` support.
69-
/** Only `"yaml"` is supported for now. */
70-
lang: v.optional(
71-
v.union([v.literal("yaml")]),
72-
"yaml"
73-
),
74-
}),
75-
{}
76+
v.special<RemarkFrontmatterCustomOptions>(
77+
() => true
78+
)
7679
),
77-
}),
78-
{}
80+
})
7981
),
8082

8183
/**
@@ -85,8 +87,10 @@ export const ConfigSchema = v.optional(
8587
v.object({
8688
/** @default true */
8789
enable: v.optional(v.boolean(), true),
88-
}),
89-
{}
90+
options: v.optional(
91+
v.special<RemarkGfmOptions>(() => true)
92+
),
93+
})
9094
),
9195

9296
/**
@@ -96,15 +100,20 @@ export const ConfigSchema = v.optional(
96100
v.object({
97101
/** @default true */
98102
enable: v.optional(v.boolean(), true),
99-
}),
100-
{}
103+
})
101104
),
102105

103106
/**
104107
* [View on NPM](https://npmjs.com/package/remark-rehype).
105108
* Can't be disabled.
106109
*/
107-
remarkRehype: v.optional(v.object({}), {}),
110+
remarkRehype: v.optional(
111+
v.object({
112+
options: v.optional(
113+
v.special<OmittedRemarkRehypeOptions>(() => true)
114+
),
115+
})
116+
),
108117

109118
/**
110119
* [View on NPM](https://npmjs.com/package/rehype-slug).
@@ -113,8 +122,10 @@ export const ConfigSchema = v.optional(
113122
v.object({
114123
/** @default false */
115124
enable: v.optional(v.boolean(), false),
116-
}),
117-
{}
125+
options: v.optional(
126+
v.special<RehypeSlugOptions>(() => true)
127+
),
128+
})
118129
),
119130

120131
/**
@@ -124,8 +135,10 @@ export const ConfigSchema = v.optional(
124135
v.object({
125136
/** @default false */
126137
enable: v.optional(v.boolean(), false),
127-
}),
128-
{}
138+
options: v.optional(
139+
v.special<RehypeAutolinkHeadingsOptions>(() => true)
140+
),
141+
})
129142
),
130143

131144
/**
@@ -135,8 +148,11 @@ export const ConfigSchema = v.optional(
135148
v.object({
136149
/** @default true */
137150
enable: v.optional(v.boolean(), true),
138-
}),
139-
{}
151+
options: v.optional(
152+
v.special<RehypeShikiOptions>(() => true),
153+
undefined
154+
),
155+
})
140156
),
141157

142158
/**
@@ -147,20 +163,26 @@ export const ConfigSchema = v.optional(
147163
v.object({
148164
/** @default true */
149165
enable: v.optional(v.boolean(), true),
150-
}),
151-
{}
166+
options: v.optional(
167+
v.special<RehypeExternalLinksOptions>(() => true)
168+
),
169+
})
152170
),
153171

154172
/**
155173
* [View on NPM](https://npmjs.com/package/rehype-stringify).
156174
* Can't be disabled.
157175
*/
158-
rehypeStringify: v.optional(v.object({}), {}),
159-
}),
160-
{}
176+
rehypeStringify: v.optional(
177+
v.object({
178+
options: v.optional(
179+
v.special<OmittedRehypeStringifyOptions>(() => true)
180+
),
181+
})
182+
),
183+
})
161184
),
162-
}),
163-
{}
185+
})
164186
)
165187

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

194-
// NOTE: Generating Valibot schema with TypeScript types is impossible. https://github.com/fabian-hiller/valibot/discussions/477
195-
export type ConfigInput = v.Input<typeof ConfigSchema> & {
196-
builtInPlugins?: {
197-
vfileMatter?: {
198-
options?: VfileMatterYamlOptions
199-
}
200-
remarkFrontmatter?: {
201-
options?: RemarkFrontmatterCustomOptions
202-
}
203-
remarkGfm?: {
204-
options?: RemarkGfmOptions
205-
}
206-
remarkRehype?: {
207-
options?: OmittedRemarkRehypeOptions
208-
}
209-
rehypeSlug?: {
210-
options?: RehypeSlugOptions
211-
}
212-
rehypeAutolinkHeadings?: {
213-
options?: RehypeAutolinkHeadingsOptions
214-
}
215-
rehypeShiki?: {
216-
options?: RehypeShikiOptions
217-
}
218-
rehypeExternalLinks?: {
219-
options?: RehypeExternalLinksOptions
220-
}
221-
rehypeStringify?: {
222-
options?: OmittedRehypeStringifyOptions
223-
}
224-
}
225-
}
226-
// NOTE: Generating Valibot schema with TypeScript types is impossible. https://github.com/fabian-hiller/valibot/discussions/477
227-
export type ConfigOutput = v.Output<typeof ConfigSchema> & {
228-
builtInPlugins: {
229-
vfileMatter: {
230-
options?: VfileMatterYamlOptions
231-
}
232-
remarkFrontmatter: {
233-
options?: RemarkFrontmatterCustomOptions
234-
}
235-
remarkGfm: {
236-
options?: RemarkGfmOptions
237-
}
238-
remarkRehype: {
239-
options?: OmittedRemarkRehypeOptions
240-
}
241-
rehypeSlug: {
242-
options?: RehypeSlugOptions
243-
}
244-
rehypeAutolinkHeadings: {
245-
options?: RehypeAutolinkHeadingsOptions
246-
}
247-
rehypeShiki: {
248-
options?: RehypeShikiOptions
249-
}
250-
rehypeExternalLinks: {
251-
options?: RehypeExternalLinksOptions
252-
}
253-
rehypeStringify: {
254-
options?: OmittedRehypeStringifyOptions
255-
}
256-
}
257-
}
216+
export type ConfigInput = v.Input<typeof ConfigSchema>
217+
export type ConfigOutput = v.Output<typeof ConfigSchema>

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
"moduleResolution": "NodeNext",
55
"outDir": "dist",
66
"sourceMap": true,
7-
"strict": true,
8-
"declaration": true
7+
"strict": true
98
},
109
"include": ["./src/**/*"]
1110
}

0 commit comments

Comments
 (0)