Skip to content

Commit 0346de6

Browse files
committed
feat: docs sync
1 parent 97ab422 commit 0346de6

File tree

6 files changed

+1312
-8
lines changed

6 files changed

+1312
-8
lines changed

.github/sync.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
magicbell/magicbell:
2+
- source: docs-dist
3+
dest: src/go/app/site/docs/mdx/03-libraries/magicbell-java-client

.github/workflows/publish-docs.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Publish Docs
2+
3+
on:
4+
release:
5+
types: [created]
6+
workflow_dispatch:
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Build docs
16+
run: yarn build:docs
17+
18+
- name: Push docs to remote repo
19+
uses: BetaHuhn/repo-file-sync-action@v1
20+
with:
21+
GH_PAT: ${{ secrets.BELLA_ACTION_TOKEN }}
22+
COMMIT_PREFIX: 'docs: '
23+
GIT_USERNAME: 'MagicBella'
24+
GIT_EMAIL: '${{ secrets.BELLA_EMAIL_ADDRESS }}'

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Tooling
22
node_modules/
3-
target
3+
target
4+
5+
docs-dist

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
"type": "module",
66
"scripts": {
77
"changeset": "./scripts/changeset-wrapper.sh",
8-
"codegen": "tsx scripts/liblab-gen.ts"
8+
"codegen": "tsx scripts/liblab-gen.ts",
9+
"build:docs": "tsx scripts/liblab-docs.ts"
910
},
1011
"devDependencies": {
1112
"@changesets/changelog-github": "^0.5.0",
1213
"@changesets/cli": "^2.27.10",
14+
"@magicbell/codegen": "^0.3.0",
1315
"@types/node": "^22.10.2",
16+
"glob": "^11.0.0",
1417
"replace-in-file": "^8.2.0",
1518
"tsx": "^4.19.2"
1619
}

scripts/liblab-docs.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import fs from 'node:fs/promises';
2+
import path from 'node:path';
3+
4+
import { exists, getDirs } from '@magicbell/codegen/fs';
5+
import * as md from '@magicbell/codegen/markdown';
6+
import * as glob from 'glob';
7+
8+
function pascalToHyphenCase(str: string): string {
9+
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
10+
}
11+
12+
function rewriteHref(url: string) {
13+
if (!url.endsWith('.md')) return url;
14+
return url.replace(/^documentation\//, '').replace(/([^/]+)\.md$/, (_, fileName) => pascalToHyphenCase(fileName));
15+
}
16+
17+
const root = process.cwd();
18+
const outdir = path.join(root, 'docs-dist');
19+
await fs.rm(outdir, { recursive: true, force: true });
20+
21+
const pkg = JSON.parse(await fs.readFile('package.json', 'utf-8'));
22+
23+
// process readme
24+
const [readme] = glob.sync('README.md', { cwd: root });
25+
const readmeAst = await md.read(readme);
26+
md.removeAllBeforeHeading(readmeAst, 'Setup & Configuration');
27+
md.reIndentHeadings(readmeAst, 1);
28+
md.mapLinks(readmeAst, rewriteHref);
29+
md.insertFrontMatter(readmeAst, { title: pkg.name });
30+
31+
await md.write(readmeAst, path.join(outdir, 'index.mdx'));
32+
33+
// process pages
34+
const docs = glob.sync('**/*.md', {
35+
cwd: path.join(root, 'documentation'),
36+
});
37+
38+
for (const file of docs) {
39+
const ast = await md.read(path.join(root, 'documentation', file));
40+
41+
md.reIndentHeadings(ast, 1);
42+
md.mapLinks(ast, rewriteHref);
43+
44+
const title = md.getTitle(ast);
45+
md.insertFrontMatter(ast, { title });
46+
md.removeFirstHeading(ast);
47+
48+
await md.write(ast, path.join(outdir, pascalToHyphenCase(file) + 'x'));
49+
}
50+
51+
for (const dir of getDirs(docs)) {
52+
const file = path.join(outdir, dir, 'index.mdx');
53+
if (await exists(file)) continue;
54+
const ast: md.Root = { type: 'root', children: [] };
55+
md.insertFrontMatter(ast, { title: path.basename(dir) });
56+
await md.write(ast, file);
57+
}

0 commit comments

Comments
 (0)