Skip to content

Commit 5e99399

Browse files
committed
Docs: add code helper
1 parent 5e66fb7 commit 5e99399

File tree

5 files changed

+195
-4
lines changed

5 files changed

+195
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
npm-debug.log*
1919
yarn-debug.log*
2020
yarn-error.log*
21+
scripts/js

docs/guides/basic_authentication.mdx

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ Sign up is just one easy call which returns an instance of the main etebase clas
2222
<CodeTabs>
2323
<TabItem value="js">
2424

25+
<HiddenCode>
26+
27+
```js
28+
const serverUrl = "";
29+
```
30+
31+
</HiddenCode>
32+
2533
```js
2634
// serverUrl can be obtained from the dashboard (or omitted for default)
2735
const etebase = await Etebase.Account.signup({
@@ -104,6 +112,14 @@ Login is too just one easy call which returns an instance of the main etebase cl
104112
<CodeTabs>
105113
<TabItem value="js">
106114

115+
<HiddenCode>
116+
117+
```js
118+
const serverUrl = "";
119+
```
120+
121+
</HiddenCode>
122+
107123
```js
108124
// serverUrl can be obtained from the dashboard (or omitted for default)
109125
const etebase = await Etebase.Account.login("username", "password", serverUrl);
@@ -178,6 +194,14 @@ Unlike signup and login, changing password requires an already set up etebase ob
178194
<CodeTabs>
179195
<TabItem value="js">
180196

197+
<HiddenCode>
198+
199+
```js
200+
const etebase = await Etebase.Account.restore('');
201+
```
202+
203+
</HiddenCode>
204+
181205
```js
182206
await etebase.changePassword("new password");
183207
```
@@ -225,6 +249,14 @@ etebase.change_password("new password")?;
225249
<CodeTabs>
226250
<TabItem value="js">
227251

252+
<HiddenCode>
253+
254+
```js
255+
const etebase = await Etebase.Account.restore('');
256+
```
257+
258+
</HiddenCode>
259+
228260
```js
229261
await etebase.logout();
230262
```
@@ -276,12 +308,20 @@ Saving and restoring a session is as simple as:
276308
<CodeTabs>
277309
<TabItem value="js">
278310

311+
<HiddenCode>
312+
279313
```js
280-
const etebase = await Etebase.Account.login("username", "password", serverUrl);
314+
const serverUrl = "";
315+
```
316+
317+
</HiddenCode>
318+
319+
```js
320+
let etebase = await Etebase.Account.login("username", "password", serverUrl);
281321
const savedSession = await etebase.save();
282322

283323
// Later on...
284-
const etebase = await Etebase.Account.restore(savedSession);
324+
etebase = await Etebase.Account.restore(savedSession);
285325
```
286326

287327
</TabItem>
@@ -371,14 +411,14 @@ stored securely (e.g. in the operating system's key store), or securely derived
371411
<TabItem value="js">
372412

373413
```js
374-
const etebase = await Etebase.Account.login("username", "password");
414+
let etebase = await Etebase.Account.login("username", "password");
375415

376416
// Save the key somewhere safe (e.g. the OS's key store)
377417
const encryptionKey = Etebase.randomBytes(32);
378418
const savedSession = await etebase.save(encryptionKey);
379419

380420
// Later on...
381-
const etebase = await Etebase.Account.restore(savedSession, encryptionKey);
421+
etebase = await Etebase.Account.restore(savedSession, encryptionKey);
382422
```
383423

384424
</TabItem>
@@ -554,6 +594,14 @@ When saving etebase instances with `cacheSave` the server URL is also being save
554594
<CodeTabs>
555595
<TabItem value="js">
556596

597+
<HiddenCode>
598+
599+
```js
600+
const savedSession = "";
601+
```
602+
603+
</HiddenCode>
604+
557605
```js
558606
const etebase = await Etebase.Account.restore(savedSession);
559607
etebase.serverUrl = "http://new-development-server";

scripts/extract.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const { createMdxAstCompiler } = require("@mdx-js/mdx");
2+
3+
const astCompiler = createMdxAstCompiler({ remarkPlugins: [] });
4+
5+
const fs = require("fs");
6+
const glob = require("glob");
7+
const util = require("util");
8+
const { parse } = require("path");
9+
const { execSync } = require("child_process");
10+
11+
main()
12+
13+
async function main() {
14+
const matches = await util.promisify(glob)("**/*.{md,mdx}", { cwd: "docs" });
15+
for (const file of matches) {
16+
const path = parse(file);
17+
const basePath = `scripts/js/src/${path.dir}/${path.name}`;
18+
fs.mkdirSync(basePath, {
19+
recursive: true,
20+
});
21+
22+
const content = fs.readFileSync(`docs/${file}`);
23+
24+
const root = astCompiler.parse(content);
25+
26+
let index = 1;
27+
function writeFile(node, i, array) {
28+
if (writeCodeToFile(node, i, array, `${basePath}/${index}.ts`)) {
29+
index++;
30+
}
31+
if (node.children) {
32+
node.children.forEach((element, i, array) => {
33+
writeFile(element, i, array);
34+
});
35+
}
36+
}
37+
38+
writeFile(root);
39+
}
40+
41+
process.chdir("scripts/js");
42+
43+
execSync("yarn init -y");
44+
execSync("yarn add etebase typescript");
45+
execSync("yarn tsc --init");
46+
47+
try {
48+
execSync("yarn tsc --noEmit");
49+
} catch (e) {
50+
console.log(e.stdout.toString());
51+
console.error(e.stderr.toString());
52+
process.exit(1);
53+
}
54+
}
55+
56+
function writeCodeToFile(node, i, array, file) {
57+
if (
58+
node.type === "jsx" &&
59+
node.value &&
60+
node.value.includes('<TabItem value="js">')
61+
) {
62+
const codePieces = [];
63+
for (let j = i + 1; j < array.length; j++) {
64+
if (
65+
array[j].type === "jsx" &&
66+
array[j].value &&
67+
array[j].value.includes("</TabItem>")
68+
) {
69+
break;
70+
}
71+
if (array[j].type === "code" && array[j].lang === "js") {
72+
codePieces.push({
73+
code: array[j].value,
74+
start: array[j].position.start,
75+
end: array[j].position.end,
76+
});
77+
}
78+
}
79+
const output = `
80+
import * as Etebase from 'etebase';
81+
82+
async function main() {
83+
${codePieces.map((piece) => `
84+
//-start ${JSON.stringify(piece.start)}
85+
${piece.code}
86+
//-end ${JSON.stringify(piece.end)}
87+
`
88+
).join('')}
89+
}`;
90+
fs.writeFileSync(file, output);
91+
return true;
92+
}
93+
return false;
94+
}

scripts/insert.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const glob = require("glob");
4+
5+
glob("**/*.ts", { cwd: "scripts/js/src" }, (_err, matches) => {
6+
matches.reverse().forEach((element) => {
7+
importFromFile(element, "scripts/js/src");
8+
});
9+
});
10+
11+
function importFromFile(file, base) {
12+
const exportedContent = fs.readFileSync(path.join(base, file)).toString();
13+
const reg = /\/\/-start (\S*)(.*?)\/\/-end (\S*)/gms;
14+
15+
const changes = [];
16+
17+
let match;
18+
while ((match = reg.exec(exportedContent)) !== null) {
19+
const start = JSON.parse(match[1]);
20+
const code = match[2];
21+
const end = JSON.parse(match[3]);
22+
changes.push({ start, code, end });
23+
}
24+
25+
const parsed = path.parse(file);
26+
let outputPath = `docs/${parsed.dir}.md`;
27+
if (!fs.existsSync()) {
28+
outputPath += "x";
29+
}
30+
31+
let oldContent = fs.readFileSync(outputPath).toString();
32+
const newContent = changes
33+
.sort((a, b) => a.start.offset - b.start.offset)
34+
.reduce(
35+
(content, change) =>
36+
content.slice(0, change.start.offset) +
37+
"```js" +
38+
change.code +
39+
"```" +
40+
content.slice(change.end.offset),
41+
oldContent
42+
);
43+
44+
fs.writeFileSync(outputPath, newContent);
45+
}

src/theme/HiddenCode.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function HiddenCode() {
2+
return null;
3+
}

0 commit comments

Comments
 (0)