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

Commit 016234a

Browse files
authored
fix: Tag documentation script (#1358)
1 parent 32c3ef4 commit 016234a

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

scripts/create-docs.js

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,80 @@
1-
const jsdoc2md = require("jsdoc-to-markdown");
2-
const fs = require("fs");
3-
const path = require("path");
4-
const fetch = require("node-fetch");
1+
const jsdoc2md = require('jsdoc-to-markdown');
2+
const fs = require('fs');
3+
const path = require('path');
54

65
const groups = [
7-
{ dir: "object", title: "Objects", order: 301, nested: true },
8-
{ dir: "control", title: "Controls", order: 401, nested: true },
9-
{ dir: "visual", title: "Visual & Experience", order: 501 },
6+
{ dir: 'object', title: 'Objects', order: 301, nested: true },
7+
{ dir: 'control', title: 'Controls', order: 401, nested: true },
8+
{ dir: 'visual', title: 'Visual & Experience', order: 501 },
109
];
1110

12-
const supertags = ["TimeSeries"];
11+
const supertags = ['TimeSeries'];
1312

14-
const currentTagsUrl = "https://api.github.com/repos/heartexlabs/label-studio/contents/docs/source/tags";
13+
const currentTagsUrl = 'https://api.github.com/repos/heartexlabs/label-studio/contents/docs/source/tags';
1514

1615
// header with tag info and autogenerated order
1716
// don't touch whitespaces
18-
const infoHeader = (name, group, isNew = false, meta = {}) => [
19-
"---",
20-
...[
21-
`title: ${name}`,
22-
`type: tags`,
23-
`order: ${groups.find(g => g.dir === group).order++}`,
24-
isNew ? "is_new: t" : "",
25-
meta.title && `meta_title: ${meta.title}`,
26-
meta.description && `meta_description: ${meta.description}`,
27-
].filter(Boolean),
28-
"---",
29-
"",
30-
"",
31-
].join("\n");
32-
33-
const outputDir = path.resolve(__dirname + "/../docs");
17+
const infoHeader = (name, group, isNew = false, meta = {}) =>
18+
[
19+
'---',
20+
...[
21+
`title: ${name}`,
22+
'type: tags',
23+
`order: ${groups.find(g => g.dir === group).order++}`,
24+
isNew ? 'is_new: t' : '',
25+
meta.title && `meta_title: ${meta.title}`,
26+
meta.description && `meta_description: ${meta.description}`,
27+
].filter(Boolean),
28+
'---',
29+
'',
30+
'',
31+
].join('\n');
32+
33+
const outputDir = path.resolve(__dirname + '/../docs');
3434

3535
fs.mkdirSync(outputDir, { recursive: true });
3636

3737
// get list of already exsting tags if possible to set `is_new` flag
3838
fetch(currentTagsUrl)
3939
.then(res => (res.ok ? res.json() : null))
40-
.then(list => list && list.map(file => file.name.replace(/.md$/, "")))
40+
.then(list => list && list.map(file => file.name.replace(/.md$/, '')))
4141
.catch(() => null)
4242
.then(tags => {
4343
function processTemplate(t, dir, supertag) {
4444
// all tags are with this kind and leading capital letter
45-
if (t.kind !== "member" || !t.name.match(/^[A-Z]/)) return;
46-
if (!supertag && t.customTags && t.customTags.find(desc => desc.tag === "subtag")) return;
45+
if (t.kind !== 'member' || !t.name.match(/^[A-Z]/)) return;
46+
if (!supertag && t.customTags && t.customTags.find(desc => desc.tag === 'subtag')) return;
4747
const name = t.name.toLowerCase();
4848
// there are no new tags if we didn't get the list
4949
const isNew = tags ? !tags.includes(name) : false;
50-
const meta = t.customTags ? Object.fromEntries(
51-
// convert @meta_* params into key-value hash
52-
t.customTags
53-
.filter(tag => tag.tag.startsWith("meta_"))
54-
.map(tag => [tag.tag.substr(5), tag.value]),
55-
) : {};
50+
const meta = t.customTags
51+
? Object.fromEntries(
52+
// convert @meta_* params into key-value hash
53+
t.customTags.filter(tag => tag.tag.startsWith('meta_')).map(tag => [tag.tag.substr(5), tag.value]),
54+
)
55+
: {};
5656
const header = supertag ? `## ${t.name}\n\n` : infoHeader(t.name, dir, isNew, meta);
5757

5858
// we can use comma-separated list of @regions used by tag
59-
const regions = t.customTags && t.customTags.find(desc => desc.tag === "regions");
59+
const regions = t.customTags && t.customTags.find(desc => desc.tag === 'regions');
6060
// sample regions result and description
61-
let results = "";
61+
let results = '';
6262

6363
if (regions) {
6464
for (const region of regions.value.split(/,\s*/)) {
65-
const files = path.resolve(__dirname + "/../src/regions/" + region + ".js");
65+
const files = path.resolve(__dirname + '/../src/regions/' + region + '.js');
6666
const regionsData = jsdoc2md.getTemplateDataSync({ files });
6767
// region descriptions named after region and defined as separate type:
6868
// @typedef {Object} AudioRegionResult
69-
const serializeData = regionsData.find(reg => reg.name === region + "Result");
69+
const serializeData = regionsData.find(reg => reg.name === region + 'Result');
7070

7171
if (serializeData) {
72-
results = jsdoc2md.renderSync({ data: [serializeData], "example-lang": "json" })
73-
.split("\n")
72+
results = jsdoc2md
73+
.renderSync({ 'data': [serializeData], 'example-lang': 'json' })
74+
.split('\n')
7475
.slice(5) // remove first 5 lines with header
75-
.join("\n")
76-
.replace(/\*\*Example\*\*\s*\n/, "### Example JSON\n");
76+
.join('\n')
77+
.replace(/\*\*Example\*\*\s*\n/, '### Example JSON\n');
7778
results = `### Sample Results JSON\n${results}\n`;
7879
}
7980
}
@@ -83,26 +84,26 @@ fetch(currentTagsUrl)
8384
delete t.customTags;
8485

8586
let str = jsdoc2md
86-
.renderSync({ data: [t], "example-lang": "html" })
87+
.renderSync({ 'data': [t], 'example-lang': 'html' })
8788
// add header with info instead of header for github
8889
// don't add any header to subtags as they'll be inserted into supertag's doc
8990
.replace(/^(.*?\n){3}/, header)
9091
// remove useless Kind: member
91-
.replace(/\*\*Kind\*\*.*?\n/, "### Parameters\n")
92-
.replace(/(\*\*Example\*\*\s*\n)/, results + "$1")
93-
.replace(/\*\*Example\*\*\s*\n/g, "### Example\n")
92+
.replace(/\*\*Kind\*\*.*?\n/, '### Parameters\n')
93+
.replace(/(\*\*Example\*\*\s*\n)/, results + '$1')
94+
.replace(/\*\*Example\*\*\s*\n/g, '### Example\n')
9495
// move comments from examples to description
95-
.replace(/```html[\n\s]*<!--[\n\s]*([\w\W]*?)[\n\s]*-->[\n\s]*/g, "\n$1\n\n```html\n")
96+
.replace(/```html[\n\s]*<!--[\n\s]*([\w\W]*?)[\n\s]*-->[\n\s]*/g, '\n$1\n\n```html\n')
9697
// change example language if it looks like JSON
97-
.replace(/```html[\n\s]*([[{])/g, "```json\n$1");
98+
.replace(/```html[\n\s]*([[{])/g, '```json\n$1');
9899

99100
if (supertags.includes(t.name)) {
100101
console.log(`Fetching subtags of ${t.name}`);
101102
const templates = jsdoc2md.getTemplateDataSync({ files: `${t.meta.path}/${t.name}/*.js` });
102103
const subtags = templates
103104
.map(t => processTemplate(t, dir, t.name))
104105
.filter(Boolean)
105-
.join("\n\n");
106+
.join('\n\n');
106107

107108
if (subtags) {
108109
// insert before the first example or just at the end of doc
@@ -114,17 +115,16 @@ fetch(currentTagsUrl)
114115
}
115116

116117
for (const { dir, title, nested } of groups) {
117-
console.log("## " + title);
118-
const prefix = __dirname + "/../src/tags/" + dir;
119-
const getTemplateDataByGlob = glob =>
120-
jsdoc2md.getTemplateDataSync({ files: path.resolve(prefix + glob) });
121-
let templateData = getTemplateDataByGlob("/*.js");
118+
console.log('## ' + title);
119+
const prefix = __dirname + '/../src/tags/' + dir;
120+
const getTemplateDataByGlob = glob => jsdoc2md.getTemplateDataSync({ files: path.resolve(prefix + glob) });
121+
let templateData = getTemplateDataByGlob('/*.js');
122122

123123
if (nested) {
124-
templateData = templateData.concat(getTemplateDataByGlob("/*/*.js"));
124+
templateData = templateData.concat(getTemplateDataByGlob('/*/*.js'));
125125
}
126126
// tags inside nested dirs go after others, so we have to resort file list
127-
templateData.sort((a, b) => a.name > b.name ? 1 : -1);
127+
templateData.sort((a, b) => (a.name > b.name ? 1 : -1));
128128
for (const t of templateData) {
129129
const name = t.name.toLowerCase();
130130
const str = processTemplate(t, dir);

0 commit comments

Comments
 (0)