KitDocs Documentation
repository·main·Indexed 19 days ago
https://github.com/svelteness/kit-docsA documentation framework for SvelteKit that transforms Markdown files into high-performance Svelte components. It serves as a Svelte-native alternative to VitePress, featuring Shiki syntax highlighting, a polished accessible theme, Algolia search integration, and advanced Markdown extensions including custom containers and global Svelte component mapping via the kitDocsPlugin.
What's inside KitDocs
- KitDocs is a documentation integration for SvelteKit, serving as a Svelte-based alternative to VitePress. It provides a complete ecosystem for building documentation sites, including Vite plugins for Markdown-to-Svelte transformation, metadata loaders, and a pre-designed theme inspired by Tailwind CSS. It features built-in accessibility, Algolia search integration, and advanced Markdown extensions.
Introduction to KitDocs
mainKitDocs is a documentation site builder for SvelteKit. It serves as a Svelte-native alternative to VitePress, providing a complete ecosystem for transforming Markdown into high-quality documentation sites.
Key Features
- Markdown Transformation: A Vite plugin that converts Markdown files into Svelte components with Hot Module Replacement (HMR) support.
- Metadata & Configuration: Loaders and endpoint handlers for managing Markdown metadata (titles, frontmatter) and sidebar configurations.
- Theming: A pre-designed theme inspired by Tailwind CSS documentation.
- Accessibility: Built-in accessible menus and popovers with full keyboard support.
- Markdown Extensions:
- Header anchors and file links.
- YAML frontmatter and emoji support.
- Custom containers (e.g., mapping
Button.svelteto:::button). - Table of contents and code fences.
- Directly importing code snippets.
- Code Highlighting: Powered by Shiki, featuring pre-designed code blocks with support for titles, line highlighting, and copy buttons.
- Prebuilt Components: Specialized Markdown components for steps, admonitions (callouts), tabbed links, responsive tables, and yes/no blocks.
- Search: Integrated Algolia search with a clean default design.
Distinguish between HTML elements and Svelte components
mainIn Svelte templates, the casing of a tag determines its type:
- Lowercase tags (e.g.,
<div>,<button>) are treated as standard HTML elements. - Capitalized tags (e.g.,
<Widget>,<Namespace.Widget>) are treated as Svelte components.
Components must be imported in the
<script>block before use.<script> import Widget from './Widget.svelte'; </script> <div> <Widget/> </div>- Lowercase tags (e.g.,
Use reactive statements with `$:`
mainYou can create reactive statements by prefixing a top-level statement with the
$:label. These statements run immediately before the component updates, whenever the values they depend on change.Rules:
- Dependency Tracking: Svelte automatically determines dependencies by looking at which variables appear inside the
$:block. - Multiple Statements: You can combine multiple statements within a single
$:block using curly braces. - Automatic Declaration: If a reactive statement assigns a value to an undeclared variable, Svelte will inject a
letdeclaration for you.
<script> export let title; // updates whenever `title` changes $: document.title = title; $: { console.log(`multiple statements can be combined`); console.log(`the current title is ${title}`); } </script>- Dependency Tracking: Svelte automatically determines dependencies by looking at which variables appear inside the
How sidebar title resolution works
mainThe sidebar title for a page is determined using the following priority order:
- The value returned by the
resolveTitleoption in the sidebar request handler. - The
sidebar_titleproperty in the Markdown file's frontmatter. - The
titleproperty in the Markdown file's frontmatter. - The first heading (
# Heading) found in the file. - The filename converted from
kebab-casetoTitle Case(e.g.,my-filebecomesMy File).
- The value returned by the
Understand Svelte accessibility (a11y) warnings
mainSvelte provides compile-time warnings to help you identify inaccessible markup. While these warnings catch many common issues, they are not exhaustive. Many accessibility problems can only be identified at runtime through automated tools or manual testing. Svelte's checks focus on enforcing best practices for ARIA attributes, element roles, and standard HTML accessibility requirements.Scope CSS in Svelte components
mainBy default, CSS defined within a
<style>block in a Svelte component is scoped to that component. Svelte achieves this by adding a unique hash-based class (e.g.,svelte-123xyz) to the elements affected by the styles, ensuring they do not leak to other components.<style> p { /* this will only affect <p> elements in this component */ color: burlywood; } </style>Trigger reactivity with assignments
mainSvelte's reactivity is triggered by assignments. To update component state and cause a re-render, assign a new value to a locally declared variable.
Important Notes:
- Update Expressions: Expressions like
count += 1or property assignments likeobj.x = ytrigger updates. - Array/Object Methods: Methods that mutate in-place (like
.push()or.splice()) do not trigger updates. To trigger an update after using these methods, you must perform a subsequent assignment to the variable (e.g.,arr = arr).
<script> let arr = [0, 1]; function handleClick () { // this method call does not trigger an update arr.push(2); // this assignment will trigger an update arr = arr; } </script>- Update Expressions: Expressions like
Use un-scoped nested <style> tags
mainWhile a component should ideally have only one top-level
<style>tag, you can nest<style>tags inside other elements or logic blocks. Note that nested<style>tags are inserted into the DOM exactly as-is; they are not processed or scoped by Svelte, meaning they will act as global styles.<div> <style> /* this style tag will be inserted as-is */ div { /* this will apply to all `<div />` elements in the DOM */ color: red; } </style> </div>Key features of KitDocs
mainKitDocs provides several developer-facing features for documentation workflows:
- Markdown Transformation: A Vite plugin that transforms
.mdfiles into Svelte components with Hot Module Replacement (HMR) support. - Data Loading: Loaders and endpoint handlers for retrieving Markdown metadata (frontmatter, titles) and sidebar configurations.
- Markdown Extensions: Support for header anchors, file links, YAML frontmatter, emojis, custom containers, table of contents, code fences, and importing code snippets.
- Syntax Highlighting: Powered by Shiki, featuring pre-designed code blocks with support for titles, line highlighting, and copy buttons.
- UI Components: Prebuilt Markdown components for steps, admonitions (callouts), tabbed links, responsive tables, and yes/no blocks.
- Global Components: A global components folder is automatically imported into all Markdown files and can be mapped to custom containers (e.g.,
Button.sveltebecomes:::button). - Search: Built-in Algolia search integration with a default design.
- Accessibility: Accessible menus and popovers with full keyboard support.
- Markdown Transformation: A Vite plugin that transforms
How `crossfade` works
mainThe
crossfadefunction creates a pair of transitions calledsendandreceive.When an element is 'sent', it looks for a corresponding element being 'received' and generates a transition that transforms the element to its counterpart's position and fades it out. When an element is 'received', the reverse happens. If no counterpart is found, the
fallbacktransition is used.Parameters:
delay(number, default0): Milliseconds to wait before starting.duration(number | function, default800): Duration of the transition in milliseconds.easing(function, defaultcubicOut): An easing function.fallback(function): A fallback transition to use when a matching element is not found.
<script> import { crossfade } from 'svelte/transition'; import { quintOut } from 'svelte/easing'; const [send, receive] = crossfade({ duration:1500, easing: quintOut }); </script> {#if condition} <h1 in:send={{key}} out:receive={{key}}>BIG ELEM</h1> {:else} <small in:send={{key}} out:receive={{key}}>small elem</small> {/if}Configure Markdown Frontmatter
mainKitDocs uses
gray-matterto process YAML frontmatter at the top of your Markdown files. You can use these properties to define page titles, descriptions, and custom sidebar titles.--- title: Page Title description: Page description. sidebar_title: Custom Sidebar Title --- # {$frontmatter.title}