aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/pages/documentation/docs_info.ts
blob: 1afcf8aafe5b07da66a40b4b1e99285b61ac3d16 (plain) (blame)
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
import compareVersions = require('compare-versions');
import * as _ from 'lodash';
import {
    DocAgnosticFormat,
    DocsInfoConfig,
    DocsMenu,
    DoxityDocObj,
    MenuSubsectionsBySection,
    SectionsMap,
    TypeDocNode,
} from 'ts/types';

export class DocsInfo {
    public displayName: string;
    public packageUrl: string;
    public subPackageName?: string;
    public websitePath: string;
    public docsJsonRoot: string;
    public menu: DocsMenu;
    public sections: SectionsMap;
    public sectionNameToMarkdown: {[sectionName: string]: string};
    private docsInfo: DocsInfoConfig;
    constructor(config: DocsInfoConfig) {
        this.displayName = config.displayName;
        this.packageUrl = config.packageUrl;
        this.subPackageName = config.subPackageName;
        this.websitePath = config.websitePath;
        this.docsJsonRoot = config.docsJsonRoot;
        this.sections = config.sections;
        this.sectionNameToMarkdown = config.sectionNameToMarkdown;
        this.docsInfo = config;
    }
    public isPublicType(typeName: string): boolean {
        if (_.isUndefined(this.docsInfo.publicTypes)) {
            return false;
        }
        const isPublic = _.includes(this.docsInfo.publicTypes, typeName);
        return isPublic;
    }
    public getModulePathsIfExists(sectionName: string): string[] {
        const modulePathsIfExists = this.docsInfo.sectionNameToModulePath[sectionName];
        return modulePathsIfExists;
    }
    public getMenu(selectedVersion?: string): {[section: string]: string[]} {
        if (_.isUndefined(selectedVersion) || _.isUndefined(this.docsInfo.menuSubsectionToVersionWhenIntroduced)) {
            return this.docsInfo.menu;
        }

        const finalMenu = _.cloneDeep(this.docsInfo.menu);
        if (_.isUndefined(finalMenu.contracts)) {
            return finalMenu;
        }

        // TODO: refactor to include more sections then simply the `contracts` section
        finalMenu.contracts = _.filter(finalMenu.contracts, (contractName: string) => {
            const versionIntroducedIfExists = this.docsInfo.menuSubsectionToVersionWhenIntroduced[contractName];
            if (!_.isUndefined(versionIntroducedIfExists)) {
                const existsInSelectedVersion = compareVersions(selectedVersion,
                                                                versionIntroducedIfExists) >= 0;
                return existsInSelectedVersion;
            } else {
                return true;
            }
        });
        return finalMenu;
    }
    public getMenuSubsectionsBySection(docAgnosticFormat?: DocAgnosticFormat): MenuSubsectionsBySection {
        const menuSubsectionsBySection = {} as MenuSubsectionsBySection;
        if (_.isUndefined(docAgnosticFormat)) {
            return menuSubsectionsBySection;
        }

        const docSections = _.keys(this.sections);
        _.each(docSections, sectionName => {
            const docSection = docAgnosticFormat[sectionName];
            if (_.isUndefined(docSection)) {
                return; // no-op
            }

            if (!_.isUndefined(this.sections.types) && sectionName === this.sections.types) {
                const sortedTypesNames = _.sortBy(docSection.types, 'name');
                const typeNames = _.map(sortedTypesNames, t => t.name);
                menuSubsectionsBySection[sectionName] = typeNames;
            } else {
                let eventNames: string[] = [];
                if (!_.isUndefined(docSection.events)) {
                    const sortedEventNames = _.sortBy(docSection.events, 'name');
                    eventNames = _.map(sortedEventNames, m => m.name);
                }
                const sortedMethodNames = _.sortBy(docSection.methods, 'name');
                const methodNames = _.map(sortedMethodNames, m => m.name);
                menuSubsectionsBySection[sectionName] = [...methodNames, ...eventNames];
            }
        });
        return menuSubsectionsBySection;
    }
    public getTypeDefinitionsByName(docAgnosticFormat: DocAgnosticFormat) {
        if (_.isUndefined(this.sections.types)) {
            return {};
        }

        const typeDocSection = docAgnosticFormat[this.sections.types];
        const typeDefinitionByName = _.keyBy(typeDocSection.types, 'name');
        return typeDefinitionByName;
    }
    public isVisibleConstructor(sectionName: string): boolean {
        return _.includes(this.docsInfo.visibleConstructors, sectionName);
    }
    public convertToDocAgnosticFormat(docObj: DoxityDocObj|TypeDocNode): DocAgnosticFormat {
        return this.docsInfo.convertToDocAgnosticFormatFn(docObj, this);
    }
}