File size: 2,523 Bytes
db5855f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// @ts-check

import fs from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

const NOTEBOOKS_DIR = join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..', 'notebooks');
const INDEX_FILE_NAME = 'README.md';

/** @typedef {import('./notebook-metadata.ts').INotebookMetadata} INotebookMetadata */

/**

 * @param {Record<string, INotebookMetadata>} notebooksMetadataMap

 * @returns {Record<string, INotebookMetadata[]>}

 */
function getCategoryToNotebooksMetadataMap(notebooksMetadataMap) {
  /** @type {Record<string, INotebookMetadata[]>} */
  const categoryToNotebooksMetadataMap = {};
  return Object.values(notebooksMetadataMap).reduce((acc, notebookMetadata) => {
    const { categories } = notebookMetadata.tags;
    for (const category of categories) {
      if (!acc[category]) {
        acc[category] = [notebookMetadata];
      } else {
        acc[category].push(notebookMetadata);
      }
    }
    return acc;
  }, categoryToNotebooksMetadataMap);
}

/**

 * @param {Record<string, INotebookMetadata[]>} categoryToNotebooksMetadataMap

 * @returns {string}

 */
function formatToIndexMarkdown(categoryToNotebooksMetadataMap) {
  const mdContents = [
    '# OpenVINO Notebooks\n\n',
    '[OpenVINO™ Notebooks at GitHub Pages](https://openvinotoolkit.github.io/openvino_notebooks/)\n\n',
  ];

  for (const [category, notebooksMetadataList] of Object.entries(categoryToNotebooksMetadataMap).sort()) {
    mdContents.push(`## ${category}\n\n`);
    const notebooksListItems = notebooksMetadataList.map(({ title, path }) => `- [${title}](./${path})\n`);
    mdContents.push(...notebooksListItems, '\n');
  }

  return mdContents.join('');
}

/**

 * @param {string} notebooksMetadataMapFilePath

 * @returns {Promise<void>}

 */
export async function generateNotebooksIndex(notebooksMetadataMapFilePath) {
  const notebooksMetadataMapContent = await fs.promises.readFile(notebooksMetadataMapFilePath, { encoding: 'utf8' });

  /** @type {Record<string, INotebookMetadata>} */
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  const notebooksMetadataMap = JSON.parse(notebooksMetadataMapContent);

  const categoryToNotebooksMetadataMap = getCategoryToNotebooksMetadataMap(notebooksMetadataMap);

  const indexContent = formatToIndexMarkdown(categoryToNotebooksMetadataMap);

  await fs.promises.writeFile(join(NOTEBOOKS_DIR, INDEX_FILE_NAME), indexContent, { flag: 'w' });
}