forked from ionic-team/ionic-docs
/
api.ts
59 lines (52 loc) · 1.64 KB
/
api.ts
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
import {
Page,
buildPages
} from '../index';
import fs from 'fs-extra';
import { join, resolve } from 'path';
// import { components } from '@ionic/docs/core.json';
import { components } from '../../data/translated-api.json';
import markdownRenderer from '../markdown-renderer';
export default {
title: 'Build API pages',
task: () => buildPages(getAPIPages)
};
async function getAPIPages(): Promise<Page[]> {
const pages = components.map(async component => {
const title = component.tag;
const path = `/docs/api/${title.slice(4)}`;
const demoUrl = await getDemoUrl(component);
const { readme, usage, props, methods, ...contents } = component;
return {
title,
path,
...demoUrl,
body: markdownRenderer(readme, path),
usage: renderUsage(usage, path),
props: renderDocsKey(props, path),
methods: renderDocsKey(methods, path),
template: 'api',
...contents
};
});
return Promise.all(pages);
}
const renderUsage = (usage, baseUrl) => Object.keys(usage).reduce((out, key) => {
out[key] = markdownRenderer(usage[key], baseUrl);
return out;
}, {});
const renderDocsKey = (items, baseUrl) => items.map(item => ({
...item,
docs: markdownRenderer(item.docs, baseUrl)
}));
const DEMOS_PATH = resolve(__dirname, '../../../src/demos');
const getDemoUrl = async (component) => {
const demoPath = `api/${component.tag.slice(4)}/index.html`;
const hasDemo = await fs.pathExists(join(DEMOS_PATH, demoPath));
if (hasDemo) {
return {
demoUrl: `/docs/demos/${demoPath}`,
demoSourceUrl: `https://github.com/ionic-team/ionic-docs/tree/master/src/demos/${demoPath}`
};
}
};