const env = require( 'jsdoc/env' ); const fs = require( 'jsdoc/fs' ); const helper = require( 'jsdoc/util/templateHelper' ); const logger = require( 'jsdoc/util/logger' ); const path = require( 'jsdoc/path' ); const { taffy } = require( '@jsdoc/salty' ); const template = require( 'jsdoc/template' ); const util = require( 'util' ); const htmlsafe = helper.htmlsafe; const linkto = helper.linkto; const resolveAuthorLinks = helper.resolveAuthorLinks; const hasOwnProp = Object.prototype.hasOwnProperty; let data; let view; const outdir = path.normalize( env.opts.destination ); const themeOpts = ( env.opts.themeOpts ) || {}; const categoryMap = {}; // Maps class names to their categories (Core, Addons, TSL) function mkdirSync( filepath ) { return fs.mkdirSync( filepath, { recursive: true } ); } function find( spec ) { return helper.find( data, spec ); } function getAncestorLinks( doclet ) { return helper.getAncestorLinks( data, doclet ); } function hashToLink( doclet, hash ) { let url; if ( ! /^(#.+)/.test( hash ) ) { return hash; } url = helper.createLink( doclet ); url = url.replace( /(#.+|$)/, hash ); return `${hash}`; } function needsSignature( { kind, type, meta } ) { let needsSig = false; // function and class definitions always get a signature if ( kind === 'function' || kind === 'class' ) { needsSig = true; } else if ( kind === 'typedef' && type && type.names && type.names.length ) { // typedefs that contain functions get a signature, too for ( let i = 0, l = type.names.length; i < l; i ++ ) { if ( type.names[ i ].toLowerCase() === 'function' ) { needsSig = true; break; } } } else if ( kind === 'namespace' && meta && meta.code && meta.code.type && meta.code.type.match( /[Ff]unction/ ) ) { // and namespaces that are functions get a signature (but finding them is a // bit messy) needsSig = true; } return needsSig; } function updateItemName( item ) { let itemName = item.name || ''; if ( item.variable ) { itemName = `…${itemName}`; } return itemName; } function addParamAttributes( params ) { return params.filter( ( { name } ) => name && ! name.includes( '.' ) ).map( param => { let itemName = updateItemName( param ); if ( param.type && param.type.names && param.type.names.length ) { const escapedTypes = param.type.names.map( name => htmlsafe( name ) ); itemName += ' : ' + escapedTypes.join( ' | ' ) + ''; } return itemName; } ); } function buildItemTypeStrings( item ) { const types = []; if ( item && item.type && item.type.names ) { item.type.names.forEach( name => { types.push( linkto( name, htmlsafe( name ) ) ); } ); } return types; } function buildSearchListForData() { const categories = { 'Core': [], 'Addons': [], 'Global': [], 'TSL': [] }; data().each( ( item ) => { if ( item.kind !== 'package' && item.kind !== 'typedef' && ! item.inherited ) { // Extract the class name from the longname (e.g., "Animation#getAnimationLoop" -> "Animation") const parts = item.longname.split( /[#~]/ ); const className = parts[ 0 ]; // If this item is a member/method of a class, check if the parent class exists if ( parts.length > 1 ) { // Find the parent class/module const parentClass = find( { longname: className, kind: [ 'class', 'module' ] } ); // Only include if parent exists and is not private if ( parentClass && parentClass.length > 0 && parentClass[ 0 ].access !== 'private' ) { const category = categoryMap[ className ]; const entry = { title: item.longname, kind: item.kind }; if ( category ) { categories[ category ].push( entry ); } } } else { // This is a top-level class/module/function - include if not private if ( item.access !== 'private' ) { let category = categoryMap[ className ]; // If not in categoryMap, determine category from @tsl tag if ( ! category ) { const hasTslTag = Array.isArray( item.tags ) && item.tags.some( tag => tag.title === 'tsl' ); if ( hasTslTag ) { category = 'TSL'; } else { category = 'Global'; } } const entry = { title: item.longname, kind: item.kind }; categories[ category ].push( entry ); } } } } ); return categories; } function buildAttribsString( attribs ) { let attribsString = ''; if ( attribs && attribs.length ) { attribsString = htmlsafe( util.format( '(%s) ', attribs.join( ', ' ) ) ); } return attribsString; } function addNonParamAttributes( items ) { let types = []; items.forEach( item => { types = types.concat( buildItemTypeStrings( item ) ); } ); return types; } function addSignatureParams( f ) { const params = f.params ? addParamAttributes( f.params ) : []; const paramsString = params.join( ', ' ); f.signature = util.format( '%s(%s)', ( f.signature || '' ), paramsString ? ' ' + paramsString + ' ' : '' ); } function addSignatureReturns( f ) { let returnTypes = []; let returnTypesString = ''; const source = f.yields || f.returns; if ( source ) { returnTypes = addNonParamAttributes( source ); } if ( returnTypes.length ) { returnTypesString = util.format( ' : %s', returnTypes.join( ' | ' ) ); } f.signature = `${f.signature || ''}${returnTypesString ? `${returnTypesString}` : ''}`; } function addSignatureTypes( f ) { const types = f.type ? buildItemTypeStrings( f ) : []; f.signature = `${f.signature || ''}${types.length ? ` : ${types.join( ' | ' )}` : ''}`; } function addAttribs( f ) { const attribs = helper.getAttribs( f ).filter( attrib => attrib !== 'static' && attrib !== 'nullable' ); const attribsString = buildAttribsString( attribs ); f.attribs = attribsString ? util.format( '%s', attribsString ) : ''; } function shortenPaths( files, commonPrefix ) { Object.keys( files ).forEach( file => { files[ file ].shortened = files[ file ].resolved.replace( commonPrefix, '' ) // always use forward slashes .replace( /\\/g, '/' ); } ); return files; } function getPathFromDoclet( { meta } ) { if ( ! meta ) { return null; } return meta.path && meta.path !== 'null' ? path.join( meta.path, meta.filename ) : meta.filename; } function getFullAugmentsChain( doclet ) { const chain = []; if ( ! doclet || ! doclet.augments || ! doclet.augments.length ) { return chain; } // Start with the immediate parent const parentName = doclet.augments[0]; chain.push( parentName ); // Recursively find the parent's ancestors const parentDoclet = find( { longname: parentName } ); if ( parentDoclet && parentDoclet.length > 0 ) { const parentChain = getFullAugmentsChain( parentDoclet[0] ); chain.unshift( ...parentChain ); } return chain; } function generate( title, docs, filename, resolveLinks ) { let html; resolveLinks = resolveLinks !== false; const docData = { env: env, title: title, docs: docs, augments: docs && docs[0] ? getFullAugmentsChain( docs[0] ) : null }; // Put HTML files in pages/ subdirectory const pagesDir = path.join( outdir, 'pages' ); mkdirSync( pagesDir ); const outpath = path.join( pagesDir, filename ); html = view.render( 'container.tmpl', docData ); if ( resolveLinks ) { html = helper.resolveLinks( html ); // turn {@link foo} into foo } // Remove lines that only contain whitespace html = html.replace( /^\s*\n/gm, '' ); fs.writeFileSync( outpath, html, 'utf8' ); } function generateSourceFiles( sourceFiles, encoding = 'utf8' ) { Object.keys( sourceFiles ).forEach( file => { let source; // links are keyed to the shortened path in each doclet's `meta.shortpath` property const sourceOutfile = helper.getUniqueFilename( sourceFiles[ file ].shortened ); helper.registerLink( sourceFiles[ file ].shortened, sourceOutfile ); try { source = { kind: 'source', code: helper.htmlsafe( fs.readFileSync( sourceFiles[ file ].resolved, encoding ) ) }; } catch ( e ) { logger.error( 'Error while generating source file %s: %s', file, e.message ); } generate( `Source: ${sourceFiles[ file ].shortened}`, [ source ], sourceOutfile, false ); } ); } function buildMainNav( items, itemsSeen, linktoFn ) { const coreDirectory = 'src'; const addonsDirectory = 'examples/jsm'; const hierarchy = new Map(); hierarchy.set( 'Core', new Map() ); hierarchy.set( 'Addons', new Map() ); let nav = ''; if ( items.length ) { items.forEach( item => { let displayName; let itemNav = ''; if ( ! hasOwnProp.call( itemsSeen, item.longname ) ) { if ( env.conf.templates.default.useLongnameInNav ) { displayName = item.longname; } else { displayName = item.name; } itemNav += `
([\s\S]*?)<\/code><\/pre>/;
const match = doclet.classdesc.match( codeBlockRegex );
if ( match ) {
doclet.codeExample = match[ 0 ];
// Remove the code example from classdesc
doclet.classdesc = doclet.classdesc.replace( codeBlockRegex, '' ).trim();
}
}
}
} );
const members = helper.getMembers( data );
members.tutorials = tutorials.children;
// output pretty-printed source files by default
const outputSourceFiles = conf.default && conf.default.outputSourceFiles !== false;
// add template helpers
view.find = find;
view.linkto = linkto;
view.resolveAuthorLinks = resolveAuthorLinks;
view.htmlsafe = htmlsafe;
view.outputSourceFiles = outputSourceFiles;
view.ignoreInheritedSymbols = themeOpts.ignoreInheritedSymbols;
// Empty nav in templates - will be loaded from nav.html client-side
view.nav = '';
// generate the pretty-printed source files first so other pages can link to them
if ( outputSourceFiles ) {
generateSourceFiles( sourceFiles, opts.encoding );
}
if ( members.globals.length ) {
// Split globals into TSL and non-TSL
const tslGlobals = [];
const nonTslGlobals = [];
const originalGlobals = members.globals;
originalGlobals.forEach( item => {
const hasTslTag = Array.isArray( item.tags ) && item.tags.some( tag => tag.title === 'tsl' );
if ( hasTslTag ) {
tslGlobals.push( item );
// Register each TSL item to link to TSL.html
helper.registerLink( item.longname, 'TSL.html#' + item.name );
} else {
nonTslGlobals.push( item );
}
} );
// Generate TSL.html for TSL functions
if ( tslGlobals.length ) {
generate( 'TSL', [ { kind: 'globalobj', isTSL: true } ], 'TSL.html' );
}
// Generate global.html for remaining globals
if ( nonTslGlobals.length ) {
generate( 'Global', [ { kind: 'globalobj' } ], globalUrl );
}
}
// index page displays information from package.json and lists files
const files = find( { kind: 'file' } );
const packages = find( { kind: 'package' } );
generate( '', // MODIFIED (Remove Home title)
packages.concat(
[ {
kind: 'mainpage',
readme: opts.readme,
longname: ( opts.mainpagetitle ) ? opts.mainpagetitle : 'Main Page'
} ]
).concat( files ), indexUrl );
// set up the lists that we'll use to generate pages
const classes = taffy( members.classes );
const modules = taffy( members.modules );
Object.keys( helper.longnameToUrl ).forEach( longname => {
const myClasses = helper.find( classes, { longname: longname } );
const myModules = helper.find( modules, { longname: longname } );
if ( myClasses.length ) {
generate( `${myClasses[ 0 ].name}`, myClasses, helper.longnameToUrl[ longname ] );
}
if ( myModules.length ) {
generate( `${myModules[ 0 ].name}`, myModules, helper.longnameToUrl[ longname ] );
}
} );
// Build navigation HTML
const navHtml = buildNav( members );
// Generate index.html with embedded navigation
const indexTemplatePath = path.join( templatePath, 'static', 'index.html' );
let indexHtml = fs.readFileSync( indexTemplatePath, 'utf8' );
// Replace placeholder with actual navigation
indexHtml = indexHtml.replace( '', navHtml );
fs.writeFileSync(
path.join( outdir, 'index.html' ),
indexHtml,
'utf8'
);
// search
const searchList = buildSearchListForData();
fs.writeFileSync(
path.join( outdir, 'search.json' ),
JSON.stringify( searchList, null, '\t' )
);
};