@@ -18,7 +18,7 @@ import { TextDocument } from 'vscode-languageserver-textdocument';
1818import { ParseResultElement } from '@swagger-api/apidom-core' ;
1919
2020import { setMetadataMap , findNamespace } from './utils/utils.ts' ;
21- import { ContentLanguage , MetadataMaps } from './apidom-language-types.ts' ;
21+ import { ContentLanguage , MetadataMaps , RefractorPlugins } from './apidom-language-types.ts' ;
2222
2323export interface ParserOptions {
2424 sourceMap ?: boolean ;
@@ -33,74 +33,119 @@ export async function parse(
3333 freeze = true ,
3434 setMetadata = true ,
3535 defaultContentLanguage ?: ContentLanguage ,
36+ refractorPlugins ?: RefractorPlugins ,
3637) : Promise < ParseResultElement > {
3738 // TODO improve detection mechanism
3839 const text : string = typeof textDocument === 'string' ? textDocument : textDocument . getText ( ) ;
3940 let result ;
4041 const contentLanguage = await findNamespace ( text , defaultContentLanguage ) ;
4142 if ( contentLanguage . namespace === 'asyncapi' && contentLanguage . format === 'JSON' ) {
42- result = await asyncapi2AdapterJson . parse ( text , { sourceMap : true } ) ;
43+ const options : Record < string , unknown > = {
44+ sourceMap : true ,
45+ refractorOpts : {
46+ plugins : [ ...( refractorPlugins ?. [ 'asyncapi-2' ] || [ ] ) ] ,
47+ } ,
48+ } ;
49+
50+ result = await asyncapi2AdapterJson . parse ( text , options ) ;
4351 } else if ( contentLanguage . namespace === 'asyncapi' && contentLanguage . format === 'YAML' ) {
4452 const options : Record < string , unknown > = {
4553 sourceMap : true ,
54+ refractorOpts : {
55+ plugins : [
56+ registerPlugins && refractorPluginReplaceEmptyElementAsyncAPI2 ( ) ,
57+ ...( refractorPlugins ?. [ 'asyncapi-2' ] || [ ] ) ,
58+ ] . filter ( Boolean ) ,
59+ } ,
4660 } ;
47- if ( registerPlugins ) {
48- options . refractorOpts = { plugins : [ refractorPluginReplaceEmptyElementAsyncAPI2 ( ) ] } ;
49- }
61+
5062 result = await asyncapi2AdapterYaml . parse ( text , options ) ;
5163 } else if (
5264 contentLanguage . namespace === 'openapi' &&
5365 contentLanguage . version === '2.0' &&
5466 contentLanguage . format === 'JSON'
5567 ) {
56- result = await openapi2AdapterJson . parse ( text , { sourceMap : true } ) ;
68+ const options : Record < string , unknown > = {
69+ sourceMap : true ,
70+ refractorOpts : {
71+ plugins : [ ...( refractorPlugins ?. [ 'openapi-2' ] || [ ] ) ] ,
72+ } ,
73+ } ;
74+
75+ result = await openapi2AdapterJson . parse ( text , options ) ;
5776 } else if (
5877 contentLanguage . namespace === 'openapi' &&
5978 contentLanguage . version === '2.0' &&
6079 contentLanguage . format === 'YAML'
6180 ) {
6281 const options : Record < string , unknown > = {
6382 sourceMap : true ,
83+ refractorOpts : {
84+ plugins : [
85+ registerPlugins && refractorPluginReplaceEmptyElementOpenAPI2 ( ) ,
86+ ...( refractorPlugins ?. [ 'openapi-2' ] || [ ] ) ,
87+ ] . filter ( Boolean ) ,
88+ } ,
6489 } ;
65- if ( registerPlugins ) {
66- options . refractorOpts = { plugins : [ refractorPluginReplaceEmptyElementOpenAPI2 ( ) ] } ;
67- }
90+
6891 result = await openapi2AdapterYaml . parse ( text , options ) ;
6992 } else if (
7093 contentLanguage . namespace === 'openapi' &&
7194 contentLanguage . version ?. startsWith ( '3.0' ) &&
7295 contentLanguage . format === 'JSON'
7396 ) {
74- result = await openapi3_0AdapterJson . parse ( text , { sourceMap : true } ) ;
97+ const options : Record < string , unknown > = {
98+ sourceMap : true ,
99+ refractorOpts : {
100+ plugins : [ ...( refractorPlugins ?. [ 'openapi-3-0' ] || [ ] ) ] ,
101+ } ,
102+ } ;
103+
104+ result = await openapi3_0AdapterJson . parse ( text , options ) ;
75105 } else if (
76106 contentLanguage . namespace === 'openapi' &&
77107 contentLanguage . version ?. startsWith ( '3.0' ) &&
78108 contentLanguage . format === 'YAML'
79109 ) {
80110 const options : Record < string , unknown > = {
81111 sourceMap : true ,
112+ refractorOpts : {
113+ plugins : [
114+ registerPlugins && refractorPluginReplaceEmptyElementOpenAPI3_0 ( ) ,
115+ ...( refractorPlugins ?. [ 'openapi-3-0' ] || [ ] ) ,
116+ ] . filter ( Boolean ) ,
117+ } ,
82118 } ;
83- if ( registerPlugins ) {
84- options . refractorOpts = { plugins : [ refractorPluginReplaceEmptyElementOpenAPI3_0 ( ) ] } ;
85- }
119+
86120 result = await openapi3_0AdapterYaml . parse ( text , options ) ;
87121 } else if (
88122 contentLanguage . namespace === 'openapi' &&
89123 contentLanguage . version ?. startsWith ( '3.1' ) &&
90124 contentLanguage . format === 'JSON'
91125 ) {
92- result = await openapi3_1AdapterJson . parse ( text , { sourceMap : true } ) ;
126+ const options : Record < string , unknown > = {
127+ sourceMap : true ,
128+ refractorOpts : {
129+ plugins : [ ...( refractorPlugins ?. [ 'openapi-3-1' ] || [ ] ) ] ,
130+ } ,
131+ } ;
132+
133+ result = await openapi3_1AdapterJson . parse ( text , options ) ;
93134 } else if (
94135 contentLanguage . namespace === 'openapi' &&
95136 contentLanguage . version ?. startsWith ( '3.1' ) &&
96137 contentLanguage . format === 'YAML'
97138 ) {
98139 const options : Record < string , unknown > = {
99140 sourceMap : true ,
141+ refractorOpts : {
142+ plugins : [
143+ registerPlugins && refractorPluginReplaceEmptyElementOpenAPI3_1 ( ) ,
144+ ...( refractorPlugins ?. [ 'openapi-3-1' ] || [ ] ) ,
145+ ] . filter ( Boolean ) ,
146+ } ,
100147 } ;
101- if ( registerPlugins ) {
102- options . refractorOpts = { plugins : [ refractorPluginReplaceEmptyElementOpenAPI3_1 ( ) ] } ;
103- }
148+
104149 result = await openapi3_1AdapterYaml . parse ( text , options ) ;
105150 } else if ( contentLanguage . namespace === 'ads' && contentLanguage . format === 'JSON' ) {
106151 result = await adsAdapterJson . parse ( text , { sourceMap : true } ) ;
0 commit comments