From 566b23c5d5b13ab82a53a736304a788f0e850d46 Mon Sep 17 00:00:00 2001 From: hlomzik Date: Thu, 31 Aug 2023 17:48:50 +0100 Subject: [PATCH] docs: Script to generate json schema of all tags It's a part of `create-docs` script as it uses the same data and can produce two artifacts in one pass. Schema is saved into `tags-schema.json` JSON schema is like this: ```json { : { name: , description: , [attrs: { : { name: , description: , type: , required: , default: , }, [...] }], }, [...] } ``` --- scripts/create-docs.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/scripts/create-docs.js b/scripts/create-docs.js index 1d7f50a6fe..f7b27ad0be 100644 --- a/scripts/create-docs.js +++ b/scripts/create-docs.js @@ -12,6 +12,18 @@ const supertags = ['TimeSeries']; const currentTagsUrl = 'https://api.github.com/repos/heartexlabs/label-studio/contents/docs/source/tags'; +/** + * Conver jsdoc parser type to simple actual type or list of possible values + * @param {{ names: string[] }} type type from jsdoc + * @returns string[] | string + */ +const attrType = ({ names } = {}) => { + if (!names) return undefined; + // boolean values are actually string literals "true" or "false" in config + if (names[0] === 'boolean') return ['true', 'false']; + return names.length > 1 ? names : names[0]; +}; + // header with tag info and autogenerated order // don't touch whitespaces const infoHeader = (name, group, isNew = false, meta = {}) => @@ -32,6 +44,9 @@ const infoHeader = (name, group, isNew = false, meta = {}) => const outputDir = path.resolve(__dirname + '/../docs'); +// schema for CodeMirror autocomplete +const schema = {}; + fs.mkdirSync(outputDir, { recursive: true }); // get list of already exsting tags if possible to set `is_new` flag @@ -55,6 +70,22 @@ fetch(currentTagsUrl) : {}; const header = supertag ? `## ${t.name}\n\n` : infoHeader(t.name, dir, isNew, meta); + // generate tag details + all attributes + schema[t.name] = { + name: t.name, + description: t.description, + attrs: Object.fromEntries(t.params?.map(p => [ + p.name, + { + name: p.name, + description: p.description, + type: attrType(p.type), + required: !p.optional, + default: p.defaultvalue, + }, + ]) ?? []), + }; + // we can use comma-separated list of @regions used by tag const regions = t.customTags && t.customTags.find(desc => desc.tag === 'regions'); // sample regions result and description @@ -147,5 +178,10 @@ fetch(currentTagsUrl) fs.writeFileSync(path.resolve(outputDir, `${name}.md`), str); } } + + // for now only hardcoded list of all tags for View + schema.View.children = Object.keys(schema).filter(name => name !== '!top'); + // @todo proper place + fs.writeFileSync(path.resolve(__dirname, 'tags-schema.json'), JSON.stringify(schema, null, 2)); }) .catch(console.error);