Socket
Book a DemoInstallSign in
Socket

mdx-collections-svelte

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdx-collections-svelte

An easy way to create collections of markdown pages in SvelteKit.

latest
Source
npmnpm
Version
2.0.1
Version published
Maintainers
0
Created
Source

NPM version

MDX Collections Svelte

An easy way to create collections of markdown pages in SvelteKit.

  • 🪵 CHANGELOG
  • 📦 NPM

Example

$lib/collections.ts:

import {
    useTypedCollections,
    type ImportGlobMarkdownMap,
} from "mdx-collections-svelte"
import { z } from "mdx-collections-svelte/zod"

/**
 * All markdown pages.
 *
 * Paths that contain (`_`) in their name are ignored to avoid conflict between pages and components.
 *
 * [Glob Import](https://vite.dev/guide/features.html#glob-import).
 */
export const pages = import.meta.glob(
    [
        "/src/content/*/**/*.md",
        "!/src/content/*/**/_*/*.md", // ignored
        "!/src/content/*/**/_*.md", // ignored
    ],
    { eager: true },
) satisfies ImportGlobMarkdownMap

const postsSchema = z.object({
    title: z.string().min(1),
    description: z.string().min(1),
})

const productsSchema = z.object({
    title: z.string().min(1),
    price: z.number().min(1),
})

export const collections = useTypedCollections(pages, {
    posts: postsSchema,
    products: productsSchema,
})

posts/+page.ts:

import { collections } from "$lib/collections.js"

export const load = async () => {
    const allPosts = collections.getEntries("posts") // You'll get type suggestions.
    const helloWorldPost = collections.getEntry("hello-world") // You'll get type suggestions.
    return { allPosts, helloWorldPost }
}

Empty frontmatter

Use this schema if you don't use frontmatter for a collection.

z.object({}).default({})

Keywords

svelte

FAQs

Package last updated on 01 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.