-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy pathapi-v6.mjs
More file actions
116 lines (99 loc) · 3.29 KB
/
Copy pathapi-v6.mjs
File metadata and controls
116 lines (99 loc) · 3.29 KB
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
import fs from 'fs';
import fetch from 'node-fetch';
const API_DIR = new URL('../versioned_docs/version-v6/apis/', import.meta.url);
const tag = 'latest-6';
const pluginApis = [
'action-sheet',
'app',
'app-launcher',
{
id: 'background-runner',
editUrl: 'https://github.com/ionic-team/capacitor-background-runner/blob/2.x/README.md',
editApiUrl:
'https://github.com/ionic-team/capacitor-background-runner/blob/2.x/packages/capacitor-plugin/src/definitions.ts',
},
'browser',
'camera',
'clipboard',
'device',
'dialog',
'filesystem',
'geolocation',
{
id: 'google-maps',
editUrl: 'https://github.com/ionic-team/capacitor-google-maps/blob/6.x/plugin/README.md',
editApiUrl:
'https://github.com/ionic-team/capacitor-google-maps/blob/6.x/plugin/src/definitions.ts',
},
'haptics',
'keyboard',
'local-notifications',
'motion',
'network',
'preferences',
'push-notifications',
'screen-orientation',
'screen-reader',
'share',
'splash-screen',
'status-bar',
'text-zoom',
'toast',
];
const isString = (value) => typeof value === 'string' || value instanceof String;
async function buildPluginApiDocs(plugin) {
const pluginId = isString(plugin) ? plugin : plugin.id;
const [readme, pkgJson] = await Promise.all([getReadme(pluginId), getPkgJsonData(pluginId)]);
const apiContent = createApiPage(plugin, readme, pkgJson);
const fileName = `${pluginId}.md`;
const filePath = new URL(fileName, API_DIR);
fs.writeFileSync(filePath, apiContent);
}
function createApiPage(plugin, readme, pkgJson) {
const pluginId = isString(plugin) ? plugin : plugin.id;
const title = `${toTitleCase(pluginId)} Capacitor Plugin API`;
const desc = pkgJson.description ? pkgJson.description.replace(/\n/g, ' ') : title;
const editUrl = isString(plugin)
? `https://github.com/ionic-team/capacitor-plugins/blob/6.x/${pluginId}/README.md`
: plugin.editUrl;
const editApiUrl = isString(plugin)
? `https://github.com/ionic-team/capacitor-plugins/blob/6.x/${pluginId}/src/definitions.ts`
: plugin.editApiUrl;
const sidebarLabel = toTitleCase(pluginId);
// // escape right curly brace in inline code blocks for MDX v3 compatibility
// const regexp = /[<|(<)]code>(.*)[<|(<)]\/code>/g;
// readme = readme.replace(regexp, (result) => {
// return result.replace(/\{/g, '{');
// });
// // removes JSDoc HTML comments as they break docusauurs
// readme = readme.replaceAll(/<!--.*-->/g, '').replaceAll('<->', '<->');
return `
---
title: ${title}
description: ${desc}
custom_edit_url: ${editUrl}
editApiUrl: ${editApiUrl}
sidebar_label: ${sidebarLabel}${plugin.isExperimental ? ' 🧪' : ''}
---
${readme}`.trim();
}
async function getReadme(pluginId) {
const url = `https://cdn.jsdelivr.net/npm/@capacitor/${pluginId}@${tag}/README.md`;
const rsp = await fetch(url);
return rsp.text();
}
async function getPkgJsonData(pluginId) {
const url = `https://cdn.jsdelivr.net/npm/@capacitor/${pluginId}@${tag}/package.json`;
const rsp = await fetch(url);
return rsp.json();
}
async function main() {
await Promise.all(pluginApis.map(buildPluginApiDocs));
console.log(`Plugin API Files Updated 🎸`);
}
function toTitleCase(str) {
return str.replace(/(^\w|-\w)/g, (s) => {
return s.replace(/-/, ' ').toUpperCase();
});
}
main();