Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

[DRAFT] docs: Script to generate json schema of all tags #1551

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions scripts/create-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) =>
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);