Welcome to mirror list, hosted at ThFree Co, Russian Federation.

stability.mjs « doc « tools - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aa1ccc827fae4facaa939afd2b8ef43634acca9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Build stability table to documentation.html/json/md by generated all.json

import fs from 'fs';

import raw from 'rehype-raw';
import htmlStringify from 'rehype-stringify';
import gfm from 'remark-gfm';
import markdown from 'remark-parse';
import remark2rehype from 'remark-rehype';
import { unified } from 'unified';
import { visit } from 'unist-util-visit';

const source = new URL('../../out/doc/api/', import.meta.url);
const data = JSON.parse(fs.readFileSync(new URL('./all.json', source), 'utf8'));
const markBegin = '<!-- STABILITY_OVERVIEW_SLOT_BEGIN -->';
const markEnd = '<!-- STABILITY_OVERVIEW_SLOT_END -->';
const mark = `${markBegin}(.*)${markEnd}`;

const output = {
  json: new URL('./stability.json', source),
  docHTML: new URL('./documentation.html', source),
  docJSON: new URL('./documentation.json', source),
  docMarkdown: new URL('./documentation.md', source),
};

function collectStability(data) {
  const stability = [];

  for (const mod of data.modules) {
    if (mod.displayName && mod.stability >= 0) {
      const link = mod.source.replace('doc/api/', '').replace('.md', '.html');

      let { stabilityText } = mod;
      if (stabilityText.includes('. ')) {
        stabilityText = stabilityText.slice(0, stabilityText.indexOf('.'));
      }

      stability.push({
        api: mod.name,
        displayName: mod.textRaw,
        link: link,
        stability: mod.stability,
        stabilityText: `(${mod.stability}) ${stabilityText}`,
      });
    }
  }

  stability.sort((a, b) => a.displayName.localeCompare(b.displayName));
  return stability;
}

function createMarkdownTable(data) {
  const md = ['| API | Stability |', '| --- | --------- |'];

  for (const mod of data) {
    md.push(`| [${mod.displayName}](${mod.link}) | ${mod.stabilityText} |`);
  }

  return md.join('\n');
}

function createHTML(md) {
  const file = unified()
    .use(markdown)
    .use(gfm)
    .use(remark2rehype, { allowDangerousHtml: true })
    .use(raw)
    .use(htmlStringify)
    .use(processStability)
    .processSync(md);

  return file.value.trim();
}

function processStability() {
  return (tree) => {
    visit(tree, { type: 'element', tagName: 'tr' }, (node) => {
      const [api, stability] = node.children;
      if (api.tagName !== 'td') {
        return;
      }

      api.properties.class = 'module_stability';

      const level = stability.children[0].value[1];
      stability.properties.class = `api_stability api_stability_${level}`;
    });
  };
}

function updateStabilityMark(file, value, mark) {
  const fd = fs.openSync(file, 'r+');
  const content = fs.readFileSync(fd, { encoding: 'utf8' });

  const replaced = content.replace(mark, value);
  if (replaced !== content) {
    fs.writeSync(fd, replaced, 0, 'utf8');
  }
  fs.closeSync(fd);
}

const stability = collectStability(data);

// add markdown
const markdownTable = createMarkdownTable(stability);
updateStabilityMark(output.docMarkdown,
                    `${markBegin}\n${markdownTable}\n${markEnd}`,
                    new RegExp(mark, 's'));

// add html table
const html = createHTML(markdownTable);
updateStabilityMark(output.docHTML, `${markBegin}${html}${markEnd}`,
                    new RegExp(mark, 's'));

// add json output
updateStabilityMark(output.docJSON,
                    JSON.stringify(`${markBegin}${html}${markEnd}`),
                    new RegExp(JSON.stringify(mark), 's'));