KitDocs Documentation

repository·main·Indexed 19 days ago

https://github.com/svelteness/kit-docs

A 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.

Tokens
44.5K
Snippets
179
Records
202
Agent score
67%

What's inside KitDocs

  1. Overview of KitDocs

    main
    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.
  2. Introduction to KitDocs

    main

    KitDocs 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.svelte to :::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.
  3. Distinguish between HTML elements and Svelte components

    main

    In 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>
  4. Use reactive statements with `$:`

    main

    You 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 let declaration 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>
  5. How sidebar title resolution works

    main

    The sidebar title for a page is determined using the following priority order:

    1. The value returned by the resolveTitle option in the sidebar request handler.
    2. The sidebar_title property in the Markdown file's frontmatter.
    3. The title property in the Markdown file's frontmatter.
    4. The first heading (# Heading) found in the file.
    5. The filename converted from kebab-case to Title Case (e.g., my-file becomes My File).
  6. Understand Svelte accessibility (a11y) warnings

    main
    Svelte 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.
  7. Scope CSS in Svelte components

    main

    By 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>
  8. Trigger reactivity with assignments

    main

    Svelte'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 += 1 or property assignments like obj.x = y trigger 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>
  9. Use un-scoped nested <style> tags

    main

    While 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>
  10. Key features of KitDocs

    main

    KitDocs provides several developer-facing features for documentation workflows:

    • Markdown Transformation: A Vite plugin that transforms .md files 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.svelte becomes :::button).
    • Search: Built-in Algolia search integration with a default design.
    • Accessibility: Accessible menus and popovers with full keyboard support.
  11. How `crossfade` works

    main

    The crossfade function creates a pair of transitions called send and receive.

    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 fallback transition is used.

    Parameters:

    • delay (number, default 0): Milliseconds to wait before starting.
    • duration (number | function, default 800): Duration of the transition in milliseconds.
    • easing (function, default cubicOut): 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}