gatsby-transformer-documentationjs is a Gatsby transformer plugin that integrates Documentation.js to extract and process JavaScript documentation from source code files. It automatically parses JSDoc comments from JavaScript, TypeScript, JSX, and TSX files, transforming them into GraphQL-queryable nodes within the Gatsby data layer.
npm install gatsby-transformer-documentationjsgatsby ^4.0.0-next>=14.15.0This plugin provides no direct imports - it exports Gatsby lifecycle hooks that are automatically called by Gatsby's plugin system.
// In gatsby-config.js
module.exports = {
plugins: [
`gatsby-transformer-documentationjs`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `source`,
path: `${__dirname}/src/`,
},
},
],
};// Example JavaScript file with JSDoc comments (src/utils.js)
/**
* A utility function that adds two numbers
* @param {number} a First number
* @param {number} b Second number
* @returns {number} Sum of a and b
* @example
* const result = add(5, 3);
* console.log(result); // 8
*/
export function add(a, b) {
return a + b;
}Then query the documentation in GraphQL:
{
allDocumentationJs {
edges {
node {
name
description {
childMarkdownRemark {
html
}
}
params {
name
type {
name
}
description {
childMarkdownRemark {
html
}
}
}
returns {
type {
name
}
description {
childMarkdownRemark {
html
}
}
}
examples {
raw
highlighted
caption
}
}
}
}
}The plugin operates through Gatsby's plugin lifecycle system:
The plugin exports the following Gatsby lifecycle hooks and utility functions:
/**
* Exported lifecycle hooks and functions
*/
exports.createSchemaCustomization = createSchemaCustomization;
exports.createResolvers = createResolvers;
exports.onCreateNode = onCreateNode;
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode;Defines the GraphQL schema types for documentation nodes.
/**
* Gatsby lifecycle hook that defines GraphQL schema types
* @param {Object} params - Gatsby API parameters
* @param {Object} params.actions - Gatsby actions
* @param {Function} params.actions.createTypes - Function to create GraphQL types
*/
function createSchemaCustomization({ actions });Creates the following GraphQL types:
DocumentationJs: Main documentation nodeDocumentationJSComponentDescription: Markdown description contentDocumentationJsExample: Code examples with syntax highlightingDocumentationJsMembers: Categorized members (static, instance, events, etc.)DoctrineType: Type information from Doctrine parserDocumentationJSLocation: Source location informationCreates custom resolvers for complex field relationships.
/**
* Gatsby lifecycle hook that adds custom GraphQL field resolvers
* @param {Object} params - Gatsby API parameters
* @param {Function} params.createResolvers - Function to register custom resolvers
*/
function createResolvers({ createResolvers });Provides recursive type definition resolution for the DocumentationJs.type field.
Determines if a file node should be processed for documentation extraction. This function is exported by the plugin.
/**
* Filter function to determine if a node should be processed
* @param {Object} params - Parameters
* @param {Object} params.node - Gatsby file node
* @returns {boolean} True if node should be processed
*/
function unstable_shouldOnCreateNode({ node });Export: This function is exported as exports.unstable_shouldOnCreateNode for use by Gatsby's plugin system.
Processes files with these characteristics:
Fileapplication/javascript.js, .jsx, .ts, .tsxMain transformation function that processes JavaScript files to extract documentation.
/**
* Gatsby lifecycle hook that creates documentation nodes from source files
* @param {Object} params - Gatsby API parameters
* @param {Object} params.node - Source file node
* @param {Object} params.actions - Gatsby actions
* @param {Function} params.actions.createNode - Function to create new nodes
* @param {Function} params.actions.createParentChildLink - Function to link parent/child nodes
* @param {Function} params.createNodeId - Function to generate node IDs
* @param {Function} params.createContentDigest - Function to generate content digests
* @param {Object} params.reporter - Gatsby reporter for logging
* @returns {Promise<boolean|null>} True if nodes were created, null if no documentation found
*/
async function onCreateNode({ node, actions, createNodeId, createContentDigest, reporter });The function:
DocumentationJs nodes for each documented elementMain documentation node representing a documented code element.
interface DocumentationJs {
/** Element name */
name: string;
/** Element kind (function, class, typedef, etc.) */
kind: string;
/** Parent element name */
memberof?: string;
/** Scope (instance, static, inner, global) */
scope?: string;
/** Access level (public, private, protected) */
access?: string;
/** Whether parameter is optional */
optional: boolean;
/** Whether element is readonly */
readonly?: boolean;
/** Whether element is abstract */
abstract?: boolean;
/** Whether function is a generator */
generator?: boolean;
/** Whether function is async */
async?: boolean;
/** Whether element overrides parent */
override?: boolean;
/** Whether to hide constructor */
hideconstructor?: boolean;
/** Element alias */
alias?: string;
/** Copyright information */
copyright?: string;
/** Author information */
author?: string;
/** License information */
license?: string;
/** Since version information */
since?: string;
/** Lends information */
lends?: string;
/** Type information */
type?: DoctrineType;
/** Default value */
default?: any;
/** Markdown description */
description?: DocumentationJSComponentDescription;
/** Deprecation notice */
deprecated?: DocumentationJSComponentDescription;
/** Extended/inherited elements */
augments?: DocumentationJs[];
/** Code examples */
examples?: DocumentationJsExample[];
/** Implemented interfaces */
implements?: DocumentationJs[];
/** Function parameters */
params?: DocumentationJs[];
/** Object properties */
properties?: DocumentationJs[];
/** Return values */
returns?: DocumentationJs[];
/** Thrown exceptions */
throws?: DocumentationJs[];
/** Todo items */
todos?: DocumentationJs[];
/** Yielded values */
yields?: DocumentationJs[];
/** Categorized members */
members?: DocumentationJsMembers;
/** Code location in source */
codeLocation?: DocumentationJSLocationRange;
/** Documentation location in source */
docsLocation?: DocumentationJSLocationRange;
}Structure for code examples with syntax highlighting.
interface DocumentationJsExample {
/** Example caption */
caption?: string;
/** Example description */
description?: string;
/** Syntax-highlighted code */
highlighted: string;
/** Raw code content */
raw: string;
}Categorized members of documented elements.
interface DocumentationJsMembers {
/** Static members */
static?: DocumentationJs[];
/** Instance members */
instance?: DocumentationJs[];
/** Event members */
events?: DocumentationJs[];
/** Global members */
global?: DocumentationJs[];
/** Inner members */
inner?: DocumentationJs[];
}Type information parsed by Doctrine.
interface DoctrineType {
/** Type category */
type: string;
/** Type name */
name?: string;
/** Array/union elements */
elements?: any[];
/** Expression for complex types */
expression?: any;
/** Type applications */
applications?: any[];
/** Function parameters */
params?: any[];
/** Object fields */
fields?: any[];
/** Function result type */
result?: any;
/** Referenced type definition */
typeDef?: DocumentationJs;
}Source code location information.
interface DocumentationJSLocation {
/** Line number */
line: number;
/** Column number */
column: number;
}
interface DocumentationJSLocationRange {
/** Start position */
start: DocumentationJSLocation;
/** End position */
end: DocumentationJSLocation;
}@param - Parameter documentation@returns - Return value documentation@example - Code examples (with optional <caption>)@throws - Exception documentation@deprecated - Deprecation notices@since - Version information@author - Author information@copyright - Copyright information@license - License information@typedef - Custom type definitions@callback - Callback type definitionsThe plugin depends on the following packages (as declared in package.json):
Internal Dependencies (used but not declared in package.json, provided by Gatsby environment):
remark - For markdown processing of JSDoc descriptionslodash - For utility functions like pick, reduce, isPlainObjectThe plugin gracefully handles parsing errors: