JSDoc theme that provides TypeScript support, component documentation with live previews, and enhanced categorization features
npx @tessl/cli install tessl/npm-better-docs@2.7.0Better-docs is a comprehensive JSDoc3 theme that extends documentation capabilities for JavaScript and TypeScript projects. It provides advanced features including TypeScript support with automatic conversion of type aliases and interfaces to JSDoc format, a category plugin for organizing documentation into nested sidebar structures, and a component plugin that generates live previews for React and Vue components.
npm install --save-dev better-docsBetter-docs is a JSDoc theme and its components are accessed through JSDoc configuration and plugin system:
// In jsdoc.json configuration
{
"opts": {
"template": "node_modules/better-docs"
},
"plugins": [
"node_modules/better-docs/typescript",
"node_modules/better-docs/category",
"node_modules/better-docs/component",
"node_modules/better-docs/typedef-import",
"node_modules/better-docs/load"
]
}For programmatic access to utilities:
const bundler = require('better-docs/bundler');
const typeConverter = require('better-docs/typescript/type-converter');
const { parseReact, parseVue } = require('better-docs/component');
const { load } = require('better-docs/lib/load');
const { defineTags } = require('better-docs/load');Configure JSDoc to use better-docs theme:
{
"opts": {
"template": "better-docs",
"destination": "docs/"
},
"templates": {
"better-docs": {
"name": "My Project Documentation",
"logo": "images/logo.png"
}
}
}Enable TypeScript processing:
{
"tags": {
"allowUnknownTags": ["optional"]
},
"plugins": [
"node_modules/better-docs/typescript"
],
"source": {
"includePattern": "\\.(jsx|js|ts|tsx)$"
}
}Enable React/Vue component documentation:
{
"tags": {
"allowUnknownTags": ["component"]
},
"plugins": [
"node_modules/better-docs/component"
]
}Then document components with @component tag:
/**
* Button component with customizable styling
* @component
* @example
* return <Button variant="primary">Click me</Button>
*/
const Button = ({ variant, children }) => {
return <button className={`btn btn-${variant}`}>{children}</button>;
};Better-docs is built around several key components:
Core JSDoc3 theme functionality that generates enhanced HTML documentation with advanced navigation, categorization, and styling.
/**
* Main JSDoc publish function that generates documentation
* @param {Object} taffyData - JSDoc data collection
* @param {Object} opts - JSDoc options
* @param {Object} tutorials - Tutorial data
*/
function publish(taffyData, opts, tutorials): void;Automatic conversion of TypeScript constructs to JSDoc-compatible documentation, including type aliases, interfaces, and class members.
/**
* Converts TypeScript types to JSDoc format
* @param {string} src - TypeScript source code
* @param {string} filename - Source file name
* @returns {string} JSDoc-compatible type definitions
*/
function typeConverter(src: string, filename?: string): string;
interface JSDocHandlers {
beforeParse(e: ParseEvent): void;
newDoclet(docletEvent: DocletEvent): void;
}Live preview system for React and Vue components with automatic prop extraction and interactive examples.
/**
* Parses React component and extracts metadata
* @param {string} filePath - Component file path
* @param {Object} doclet - JSDoc doclet
* @returns {ComponentMetadata} Component information
*/
function parseReact(filePath: string, doclet: Object): ComponentMetadata;
/**
* Parses Vue component and extracts metadata
* @param {string} filePath - Component file path
* @param {Object} doclet - JSDoc doclet
* @returns {ComponentMetadata} Component information
*/
function parseVue(filePath: string, doclet: Object): ComponentMetadata;
interface ComponentMetadata {
displayName: string;
filePath: string;
props: PropInfo[];
slots?: SlotInfo[];
type: 'react' | 'vue';
}Hierarchical organization system that allows grouping documentation elements into categories and subcategories for better navigation.
interface CategoryHandlers {
newDoclet(docletEvent: DocletEvent): void;
}
interface DocletEvent {
doclet: {
tags?: TagInfo[];
category?: string;
subCategory?: string;
};
}Dynamic content loading system that allows including external files and processed content into documentation.
/**
* Defines the @load tag for JSDoc
* @param {Object} dictionary - JSDoc tag dictionary
*/
function defineTags(dictionary: Object): void;
/**
* Loads and processes content from external files
* @param {Object} loadTag - JSDoc load tag
* @param {string} docletFilePath - Base path for relative loading
* @returns {string} Processed content
*/
function load(loadTag: Object, docletFilePath: string): string;Build system that creates interactive component previews using Parcel bundler with support for React and Vue components.
/**
* Bundles React/Vue components for live preview
* @param {ComponentInfo[]} Components - Array of component information
* @param {string} out - Output directory
* @param {Object} config - Build configuration
*/
function bundle(Components: ComponentInfo[], out: string, config: Object): void;
interface ComponentInfo {
component: {
displayName: string;
filePath: string;
type: 'react' | 'vue';
};
}Removes TypeScript-style import type declarations from source code before JSDoc processing to prevent parsing errors.
interface TypeDefImportHandlers {
/**
* Processes source code before JSDoc parsing to remove import type declarations
* @param {Object} e - Parse event containing source code
*/
beforeParse(e: ParseEvent): void;
}Better-docs supports extensive configuration through JSDoc configuration files:
interface BetterDocsConfig {
name?: string;
logo?: string;
title?: string;
css?: string;
trackingCode?: string;
hideGenerator?: boolean;
navButtons?: boolean;
landing?: string;
navLinks?: NavLink[];
component?: ComponentConfig;
}
interface ComponentConfig {
wrapper?: string;
entry?: string[];
}
interface NavLink {
label: string;
href: string;
}interface ParseEvent {
filename: string;
source: string;
componentInfo?: ComponentInfo;
}
interface TagInfo {
title: string;
value: string;
}
interface PropInfo {
name: string;
description?: string;
type: string;
required: boolean;
defaultValue?: string;
}
interface SlotInfo {
name: string;
description?: string;
}