Skip to content

Commit

Permalink
feat: empty content message
Browse files Browse the repository at this point in the history
  • Loading branch information
zce committed May 8, 2024
1 parent db4375f commit 60d233f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/guide/using-mdx.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export const mdxBundle = (options: MdxOptions = {}) =>
custom<string>().transform<string>(async (value, { meta: { path, content, config }, addIssue }) => {
value = value ?? content
if (value == null) {
addIssue({ fatal: true, code: 'custom', message: 'Empty content' })
addIssue({ fatal: true, code: 'custom', message: 'The content is empty' })
return null as never
}

Expand Down
4 changes: 2 additions & 2 deletions src/schemas/excerpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export interface ExcerptOptions {
}

export const excerpt = ({ length = 260 }: ExcerptOptions = {}) =>
custom<string | null | undefined>().transform<string>(async (value, { meta: { plain }, addIssue }) => {
custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(async (value, { meta: { plain }, addIssue }) => {
value = value ?? plain
if (value == null || value.length === 0) {
addIssue({ code: 'custom', message: 'Empty content' })
addIssue({ code: 'custom', message: 'The content is empty' })
return ''
}

Expand Down
4 changes: 2 additions & 2 deletions src/schemas/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ const rehypeMetaString = () => (tree: Hast) => {
}

export const markdown = (options: MarkdownOptions = {}) =>
custom<string | null | undefined>().transform<string>(async (value, { meta: { path, content, config }, addIssue }) => {
custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(async (value, { meta: { path, content, config }, addIssue }) => {
value = value ?? content
if (value == null || value.length === 0) {
addIssue({ code: 'custom', message: 'Empty content' })
addIssue({ code: 'custom', message: 'The content is empty' })
return ''
}

Expand Down
4 changes: 2 additions & 2 deletions src/schemas/mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const remarkRemoveComments = () => (tree: Root) => {
}

export const mdx = (options: MdxOptions = {}) =>
custom<string | null | undefined>().transform<string>(async (value, { meta: { path, content, config }, addIssue }) => {
custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(async (value, { meta: { path, content, config }, addIssue }) => {
value = value ?? content
if (value == null || value.length === 0) {
addIssue({ code: 'custom', message: 'Empty content' })
addIssue({ code: 'custom', message: 'The content is empty' })
return ''
}

Expand Down
4 changes: 2 additions & 2 deletions src/schemas/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export interface Metadata {
}

export const metadata = () =>
custom<string | null | undefined>().transform<Metadata>(async (value, { meta: { plain }, addIssue }) => {
custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<Metadata>(async (value, { meta: { plain }, addIssue }) => {
value = value ?? plain
if (value == null || value.length === 0) {
addIssue({ code: 'custom', message: 'Empty content' })
addIssue({ code: 'custom', message: 'The content is empty' })
return { readingTime: 0, wordCount: 0 }
}

Expand Down
2 changes: 1 addition & 1 deletion src/schemas/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface PathOptions {
* @returns flattened path based on the file path
*/
export const path = (options?: PathOptions) =>
custom<string | null | undefined>().transform<string>(async (value, { meta: { path, config }, addIssue }) => {
custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(async (value, { meta: { path, config }, addIssue }) => {
if (value != null) {
addIssue({ fatal: false, code: 'custom', message: '`s.path()` schema will resolve the flattening path based on the file path' })
}
Expand Down
3 changes: 2 additions & 1 deletion src/schemas/raw.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { custom } from './zod'

export const raw = () => custom<string | null | undefined>().transform<string>(async (value, { meta: { content } }) => value ?? content ?? '')
export const raw = () =>
custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(async (value, { meta: { content } }) => value ?? content ?? '')
36 changes: 19 additions & 17 deletions src/schemas/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,23 @@ const parse = (tree?: List): TocEntry[] => {
}

export const toc = <T extends TocOptions>(options?: T) =>
custom<string | null | undefined>().transform<T extends { original: true } ? TocTree : TocEntry[]>(async (value, { meta, addIssue }) => {
if (value == null || value.length === 0) {
addIssue({ code: 'custom', message: 'Empty content' })
return (options?.original ? {} : []) as T extends { original: true } ? TocTree : TocEntry[]
custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<T extends { original: true } ? TocTree : TocEntry[]>(
async (value, { meta, addIssue }) => {
if (value == null || value.length === 0) {
addIssue({ code: 'custom', message: 'The content is empty' })
return (options?.original ? {} : []) as T extends { original: true } ? TocTree : TocEntry[]
}
try {
// extract ast tree from markdown/mdx content
const tree = value != null ? fromMarkdown(value) : meta.mdast
if (tree == null) throw new Error('No tree found')
const tocTree = extractToc(tree, options)
// return the original tree if requested
if (options?.original) return tocTree as T extends { original: true } ? TocTree : TocEntry[]
return parse(tocTree.map) as T extends { original: true } ? TocTree : TocEntry[]
} catch (err: any) {
addIssue({ fatal: true, code: 'custom', message: err.message })
return null as never
}
}
try {
// extract ast tree from markdown/mdx content
const tree = value != null ? fromMarkdown(value) : meta.mdast
if (tree == null) throw new Error('No tree found')
const tocTree = extractToc(tree, options)
// return the original tree if requested
if (options?.original) return tocTree as T extends { original: true } ? TocTree : TocEntry[]
return parse(tocTree.map) as T extends { original: true } ? TocTree : TocEntry[]
} catch (err: any) {
addIssue({ fatal: true, code: 'custom', message: err.message })
return null as never
}
})
)

0 comments on commit 60d233f

Please sign in to comment.