Skip to content

Commit

Permalink
Graft over shared libs from jy95 to add support for resolving $ref
Browse files Browse the repository at this point in the history
Most of these TS files are grafted over verbatim from the
jy95/docusaurus-json-schema-plugin repository as it provides a succinct
way of acquiring and then rendering nested JSON schemas

Signed-off-by: Jeremy Ho <jujaga@gmail.com>
  • Loading branch information
jujaga committed Dec 19, 2024
1 parent c9ab614 commit 8e5a8be
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/spec/resource/process_event.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import JSONSchemaViewer from "@theme/JSONSchemaViewer";
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

import generateResolverOptions from "@site/src/components/shared/generateResolverOptions";
import { generateResolverOptions } from "@site/src/components/shared";
import example from "@site/docs/spec/resource/process_event.example.json";
import schema from "@site/docs/spec/resource/process_event.schema.json";

Expand Down
20 changes: 20 additions & 0 deletions src/components/shared/generatePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Example of basePath = '/schemas/examples/refs'
export default function generatePath(path: string, basePath: string = '') {
const parts = path.toString().split('/');
let finalPath = basePath;

for (let i = 0; i < parts.length; i++) {
if (parts[i] === '..') {
// Move up one directory in the base path
finalPath = finalPath.split('/').slice(0, -1).join('/');
} else if (parts[i] === '.') {
// Ignore current directory notation
continue;
} else {
// Append the current directory to the base path
finalPath = `${finalPath}/${parts[i]}`;
}
}

return finalPath;
}
34 changes: 34 additions & 0 deletions src/components/shared/generateResolverOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import LocalFileResolver from '@site/src/components/shared/localFileResolver';
import RemoteResolver from '@site/src/components/shared/remoteResolver';

type Params = {
basePath?: string;
jsonPointer?: string;
remote?: boolean;
};

export default function generateResolverOptions(params: Params) {
const { basePath, jsonPointer, remote } = params;

let config = {};

if (basePath) {
config['resolvers'] = {
file: LocalFileResolver(basePath)
};
}

if (remote) {
if (config['resolvers'] === undefined) {
config['resolvers'] = {};
}
config['resolvers']['http'] = RemoteResolver('http');
config['resolvers']['https'] = RemoteResolver('https');
}

if (jsonPointer) {
config['jsonPointer'] = jsonPointer;
}

return config;
}
4 changes: 4 additions & 0 deletions src/components/shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { default as generatePath } from './generatePath';
export { default as generateResolverOptions } from './generateResolverOptions';
export { default as localFileResolver } from './localFileResolver';
export { default as remoteResolver } from './remoteResolver';
16 changes: 16 additions & 0 deletions src/components/shared/localFileResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import generatePath from '@site/src/components/shared/generatePath'

// Here a workaround for Docusaurus, as your assets are public at the end, require them
export default function LocalFileResolver(basePath: string = '') {
return {
resolve: (ref: string) => {
return new Promise((resolve, reject) => {
const temp_url = generatePath(ref, basePath)
//import("@site/static/schemas/examples/array/additionalItems1.json")
import(`@site/static/${temp_url.substring(1)}`)
.then((result) => resolve(result.default))
.catch((err) => reject(err))
})
},
}
}
18 changes: 18 additions & 0 deletions src/components/shared/remoteResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type Param = 'http' | 'https';

export default function LocalFileResolver(_type: Param = 'http') {
return {
resolve: (ref: string) => {
return new Promise((resolve, reject) => {
fetch(ref.toString(), {
headers: {
Accept: 'application/json'
}
})
.then((response) => response.json())
.then((result) => resolve(result))
.catch((err) => reject(err));
});
}
};
}

0 comments on commit 8e5a8be

Please sign in to comment.