npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@acdh-oeaw/keystatic-lib

v0.5.3

Published

utilities for working with multi-language [`keystatic`](https://keystatic.com) collections.

Downloads

318

Readme

keystatic lib

utilities for working with multi-language keystatic collections.

how to install

npm i @acdh-oeaw/keystatic-lib

how to use

provide the languages supported in your project via typescript module augmentation:

// ./types/keystatic.d.ts

declare module "@acdh-oeaw/keystatic-lib" {
	export interface KeystaticConfig {
		locales: "de" | "en";
	}
}

create components (rich text editor widgets)

// ./lib/keystatic/components.ts

import { createComponent } from "@acdh-oeaw/keystatic-lib";
import { wrapper } from "@keystatic/core/content-components";

export const Video = createComponent((paths, locale) => {
	return wrapper({
		label: "Video",
		schema: {
			/** ... */
		},
	});
});

create collections and singletons

// ./lib/keystatic/resources.ts

import {
	createAssetOptions,
	createCollection,
	createContentFieldOptions,
	createSingleton,
	createLabel,
} from "@acdh-oeaw/keystatic-lib";
import { collection, singleton } from "@keystatic/core";

import { Video } from "./lib/keystatic/components";

export const pages = createCollection("/pages/", (paths, locale) => {
	return collection({
		label: createLabel("Pages", locale),
		path: paths.contentPath,
		slugField: "title",
		format: { contentField: "content" },
		entryLayout: "content",
		columns: ["title"],
		schema: {
			title: fields.slug({
				name: {
					label: "Title",
					validation: { isRequired: true },
				},
			}),
			image: fields.image({
				label: "Image",
				validation: { isRequired: false },
				...createAssetOptions(paths.assetPath),
			}),
			content: fields.mdx({
				label: "Content",
				options: createContentFieldOptions(paths),
				components: {
					Video: Video(paths, locale),
				},
			}),
		},
	});
});

export const metadata = createSingleton("/metadata/", (paths, locale) => {
	return singleton({
		label: createLabel("Metadata", locale),
		path: paths.contentPath,
		format: { data: "json" },
		entryLayout: "form",
		schema: {
			/** ... */
		},
	});
});
// ./keystatic.config.ts

import { withI18nPrefix } from "@acdh-oeaw/keystatic-lib";
import { config } from "@keystatic/core";

import { metadata, pages } from "./lib/keystatic/resources";

export default config({
	collections: {
		[withI18nPrefix("pages", "de")]: pages("de"),
		[withI18nPrefix("pages", "en")]: pages("en"),
	},
	singletons: {
		[withI18nPrefix("metadata", "de")]: metadata("de"),
		[withI18nPrefix("metadata", "en")]: metadata("en"),
	},
});

read and render entries

create resource readers by passing the keystatic config and a mdx compiler function to createReaders:

// ./lib/keystatic/readers.ts

import { createReaders } from "@acdh-oeaw/keystatic-lib/reader";
import { createFormatAwareProcessors } from "@mdx-js/mdx/internal-create-format-aware-processors";

import config from "../../keystatic.config";

function getMdxContent(code: string, locale: Locale, baseUrl: URL) {
	const processor = await createFormatAwareProcessors({
		/** Set this to `html` in astro. */
		// elementAttributeNameCase: "html",
		format: "mdx",
		outputFormat: "function-body",
		providerImportSource: "#",
		/** ... */
	});
	const file = await processor.process(code);
	return run(file, { ...runtime, baseUrl, useMDXComponents });
}

function useMDXComponents() {
	/** Provide component mappings, which should be available to all mdx content. */
	return {
		// a: LocaleLink,
		// Video,
	};
}

const { createCollectionResource, createSingletonResource } = createReaders(config, getMdxContent);

export { createCollectionResource, createSingletonResource };

a reader has methods for reading a single resource entry (read), reading all resource entries (all), and returning a list of entry identifiers (list).

a resource entry returns id, collection/singleton, and data, as well as a compile method, which can be used to transform rich-text field content to a jsx component.

astro

---
// ./src/pages/[locale]/[id].astro

import type { GetStaticPathsResult } from "astro";

import { locales } from "@/config/i18n.config";
import Layout from "@/layouts/page-layout.astro";
import { createCollectionResource } from "@/lib/keystatic/readers";

export async function getStaticPaths() {
	return (
		await Promise.all(
			locales.map(async (locale) => {
				const ids = await createCollectionResource("pages", locale).list();
				return ids.map((id) => {
					return { params: { id, locale } };
				});
			}),
		)
	).flat() satisfies GetStaticPathsResult;
}

const { id, locale } = Astro.params;

const page = await createCollectionResource("pages", locale).read(id);
const { content, image, title } = page.data;
const { default: Content } = await page.compile(content);
---

<Layout>
	<main>
		<Content />
	</main>
</Layout>

next.js

// ./app/[locale]/[id]/page.tsx

import type { Locale } from "@/config/i18n.config";
import { createCollectionResource } from "@/lib/keystatic/readers";

interface PageProps {
	params: {
		id: string;
		locale: Locale;
	};
}

export const dynamicParams = false;

export async function generateStaticParams(props: PageProps) {
	const { locale } = props.params;
	const ids = await createCollectionResource("pages", locale).list();
	return ids.map((id) => {
		return { id };
	});
}

export default async function Page(props: PageProps) {
	const { id, locale } = props.params;

	const page = await createCollectionResource("pages", locale).read(id);
	const { title, image, content } = page.data;
	const { default: Content } = await page.compile(content);

	return (
		<main>
			<Content />
		</main>
	);
}