11'use strict' ;
22
3- import { create , insert } from '@orama/orama' ;
3+ import { create , insertMultiple } from '@orama/orama' ;
44import { persistToFile } from '@orama/plugin-data-persistence/server' ;
55
6- import { enforceArray } from '../../utils/array .mjs' ;
6+ import { SCHEMA } from './constants .mjs' ;
77import { groupNodesByModule } from '../../utils/generators.mjs' ;
8- import { createSectionBuilder } from '../legacy-json/utils/buildSection.mjs' ;
98
109/**
11- * Schema definition for the Orama database
10+ * Builds a hierarchical title chain based on heading depths
11+ *
12+ * @param {ApiDocMetadataEntry[] } headings - All headings sorted by order
13+ * @param {number } currentIndex - Index of current heading
14+ * @returns {string } Hierarchical title
1215 */
13- const ORAMA_SCHEMA = {
14- name : 'string' ,
15- type : 'string' ,
16- desc : 'string' ,
17- stability : 'number' ,
18- stabilityText : 'string' ,
19- meta : {
20- changes : 'string[]' ,
21- added : 'string[]' ,
22- napiVersion : 'string[]' ,
23- deprecated : 'string[]' ,
24- removed : 'string[]' ,
25- } ,
26- } ;
16+ function buildHierarchicalTitle ( headings , currentIndex ) {
17+ const currentNode = headings [ currentIndex ] ;
18+ const titleChain = [ currentNode . heading . data . name ] ;
19+ let targetDepth = currentNode . heading . depth - 1 ;
2720
28- /**
29- * Transforms a section into the format expected by Orama
30- * @param {import('../legacy-json/types.d.ts').ModuleSection } node - The section to transform
31- */
32- function transformSectionForOrama ( node ) {
33- return {
34- name : node . name ,
35- type : node . type ,
36- desc : node . desc ,
37- // Account for duplicate stability nodes
38- stability : enforceArray ( node . stability ) [ 0 ] ,
39- stabilityText : enforceArray ( node . stabilityText ) [ 0 ] ,
40- meta : {
41- changes :
42- node . meta ?. changes ?. map (
43- c => `${ enforceArray ( c . version ) . join ( ', ' ) } : ${ c . description } `
44- ) ?? [ ] ,
45- added : node . meta ?. added ?? [ ] ,
46- napiVersion : node . meta ?. napiVersion ?? [ ] ,
47- deprecated : node . meta ?. deprecated ?? [ ] ,
48- removed : node . meta ?. removed ?? [ ] ,
49- } ,
50- } ;
21+ // Walk backwards through preceding headings to build hierarchy
22+ for ( let i = currentIndex - 1 ; i >= 0 && targetDepth > 0 ; i -- ) {
23+ const heading = headings [ i ] ;
24+ const headingDepth = heading . heading . depth ;
25+
26+ if ( headingDepth <= targetDepth ) {
27+ titleChain . unshift ( heading . heading . data . name ) ;
28+ targetDepth = headingDepth - 1 ;
29+ }
30+ }
31+
32+ return titleChain . join ( ' > ' ) ;
5133}
5234
5335/**
@@ -70,36 +52,33 @@ export default {
7052 * @param {Input } input
7153 * @param {Partial<GeneratorOptions> } options
7254 */
73- async generate ( input , { output, version } ) {
55+ async generate ( input , { output } ) {
7456 if ( ! input ?. length ) {
7557 throw new Error ( 'Input data is required and must not be empty' ) ;
7658 }
7759
78- if ( ! output || ! version ) {
79- throw new Error ( 'Output path and version are required' ) ;
60+ if ( ! output ) {
61+ throw new Error ( 'Output path is required' ) ;
8062 }
8163
82- const db = create ( { schema : ORAMA_SCHEMA } ) ;
83- const buildSection = createSectionBuilder ( ) ;
84- const groupedModules = groupNodesByModule ( input ) ;
85- const headNodes = input . filter ( node => node . heading ?. depth === 1 ) ;
86-
87- // Process each head node and insert into database
88- headNodes . forEach ( headNode => {
89- const nodes = groupedModules . get ( headNode . api ) ;
90-
91- const section = buildSection ( headNode , nodes ) ;
92- const node = ( section . modules || section . globals || section . miscs ) [ 0 ] ;
93- if ( ! node ) return ;
64+ const db = create ( { schema : SCHEMA } ) ;
65+ const apiGroups = groupNodesByModule ( input ) ;
9466
95- const oramaData = transformSectionForOrama ( node ) ;
96- insert ( db , oramaData ) ;
97- } ) ;
67+ // Process all API groups and flatten into a single document array
68+ const documents = Array . from ( apiGroups . values ( ) ) . flatMap ( headings =>
69+ headings . map ( ( node , index ) => {
70+ const hierarchicalTitle = buildHierarchicalTitle ( headings , index ) ;
9871
99- // Generate output filename and persist database
100- const sanitizedVersion = version . raw . replaceAll ( '.' , '-' ) ;
101- const outputFilename = `${ output } /${ sanitizedVersion } -orama-db.json` ;
72+ return {
73+ title : hierarchicalTitle ,
74+ // TODO(@avivkeller): Add `description` key.
75+ path : `${ node . api } .html#${ node . slug } ` ,
76+ } ;
77+ } )
78+ ) ;
10279
103- await persistToFile ( db , 'json' , outputFilename ) ;
80+ // Insert all documents and persist database
81+ await insertMultiple ( db , documents ) ;
82+ await persistToFile ( db , 'json' , `${ output } /orama-db.json` ) ;
10483 } ,
10584} ;
0 commit comments