Skip to content

Commit 0dcf175

Browse files
committed
chore: add docs build script
1 parent 952ab4c commit 0dcf175

File tree

6 files changed

+177
-4
lines changed

6 files changed

+177
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ yarn.lock
99
/modules/
1010
/.vscode/
1111
.DS_Store
12+
/dist_docs/

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ yarn.lock
1313
/test/
1414
/.vscode/
1515
.DS_Store
16+
/dist_docs/

package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,30 @@
6565
"typescript": "2.6.2",
6666
"webpack": "3.10.0",
6767
"semantic-release": "^12.2.4",
68-
"gitbook-cli": "2.3.2"
68+
"gitbook-cli": "2.3.2",
69+
"glob": "^7.1.2",
70+
"mkdirp": "0.5.1"
6971
},
7072
"config": {
7173
"commitizen": {
7274
"path": "./node_modules/mol-conventional-changelog"
7375
}
7476
},
7577
"scripts": {
76-
"clean": "rimraf lib modules",
78+
"clean": "rimraf lib modules && npm run docs:clean",
7779
"build": "npm run clean && gulp build-ts && gulp build-modules",
7880
"start": "npm run storybook",
79-
"test": "npm run test:server && npm run test:client",
8081
"semantic-release": "semantic-release",
82+
"test": "npm run test:server && npm run test:client",
8183
"test:server": "mocha -r ts-node/register src/**/*.test-server.ts*",
8284
"test:client": "jest",
8385
"test:client:coverage": "jest --coverage",
8486
"test:watch": "jest --watch",
8587
"test:story": "start-storybook -p 6007",
8688
"test:story:build": "build-storybook",
87-
"docs:watch": "gitbook serve ./docs"
89+
"docs:watch": "gitbook serve ./docs",
90+
"docs:clean": "rimraf dist_docs",
91+
"docs:build": "node ./scripts/build_docs.js"
8892
},
8993
"jest": {
9094
"moduleFileExtensions": [

scripts/build_docs.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const glob = require('glob');
2+
const {join, sep, relative} = require('path');
3+
const {copyFileSync} = require('fs');
4+
const mkdirp = require('mkdirp');
5+
6+
const ROOT = join(__dirname, '..');
7+
const SRC = join(ROOT, 'src');
8+
const DOCS = join(ROOT, 'docs');
9+
const DOCS_BUILD = join(ROOT, 'dist_docs');
10+
11+
const copy = (src, dest) => {
12+
const targetDir = join(dest, '..');
13+
14+
mkdirp.sync(targetDir);
15+
copyFileSync(src, dest);
16+
};
17+
18+
const copyInlineDocsFile = (srcFile) => {
19+
const steps = srcFile.split(sep);
20+
const indexDocs = steps.lastIndexOf('__docs__');
21+
const name = steps[indexDocs - 1];
22+
const lang = steps[indexDocs + 1];
23+
const relSteps = steps.slice(indexDocs + 2);
24+
const targetFile = join(DOCS_BUILD, lang, ...relSteps);
25+
26+
copy(srcFile, targetFile);
27+
};
28+
29+
const copyToBuildFolder = (srcFile) => {
30+
const relPath = relative(DOCS, srcFile);
31+
const targetFile = join(DOCS_BUILD, relPath);
32+
33+
copy(srcFile, targetFile);
34+
};
35+
36+
const inlineDocFiles = glob.sync(`${SRC}/**/__docs__/**/*.md`);
37+
const docFiles = glob.sync(`${DOCS}/**/**/*`);
38+
39+
inlineDocFiles.forEach(copyInlineDocsFile);
40+
docFiles.forEach(copyToBuildFolder);

src/Alert/__docs__/en/Alert.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# `<Alert>`
2+
3+
Uses [`Window.alert()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert) to display message ot a user.
4+
5+
## Usage
6+
7+
```jsx
8+
import {Alert} from 'libreact/lib/Alert';
9+
10+
<Alert show text='Hello world' />
11+
```
12+
13+
## Props
14+
15+
- `show` - boolean, optional, whether to show the alert.
16+
- `text` - string, require, string message to display to the user.

src/Audio/__docs__/en/Audio.md

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# `<Audio>`
2+
3+
FaCC that creates an `<audio>` element to play audio tracks, re-renders on audio state changes.
4+
5+
## Usage
6+
7+
```jsx
8+
import {Audio} from 'libreact/lib/Audio';
9+
10+
<Audio autoPlay src='https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3'>{(control, state) => {
11+
JSON.stringify(state, null, 4)
12+
}}</Audio>
13+
```
14+
15+
16+
## Props
17+
18+
In addition to props below also accepts all [React's media events](https://reactjs.org/docs/events.html#media-events).
19+
20+
```tsx
21+
interface IAudioProps {
22+
src: string;
23+
autoPlay?: boolean;
24+
loop?: boolean;
25+
muted?: boolean;
26+
preload?: 'none' | 'metadata' | 'auto';
27+
volume?: number;
28+
noJs?: React.ReactElement<any>;
29+
}
30+
```
31+
32+
- `src` - required, string, audio source file URL.
33+
- `autoPlay` - optional, boolean, whether to autoplay audio, defaults to `false`.
34+
- `loop` - optional, boolean, whether to repeat the track when it ends, defaults to `false`.
35+
- `muted` - optional, boolean, whether to mute the audio, defaults to `false`.
36+
- `preload` - optional, string, `<audio>` element preload attribute.
37+
- `volume` - optional, number, audio volume in `[0..1]` range, defaults to `1`.
38+
- `noJs` - optional, React element(s) to render insided the `<audio>` tag.
39+
- `onMount` - optional, callback, called when component mounts, receives `IAudio` as the first argument.
40+
- `onUnmount` - optional, callback, called when component un-mounts, receives `IAudio` as the first argument.
41+
42+
43+
## Arguments
44+
45+
The `children` function receives two arguments, `audio` instance as *controller* and `state`.
46+
47+
```jsx
48+
<Audio autoPlay src={src}>{(audio, state) =>
49+
50+
}</Audio>
51+
```
52+
53+
First argument is the `<Audio>` component instance with the following public signature.
54+
55+
```ts
56+
interface IAudio {
57+
el: HTMLAudioElement;
58+
play();
59+
pause();
60+
seek(time: number);
61+
volume(value: number);
62+
mute();
63+
unmute();
64+
}
65+
```
66+
67+
, where
68+
69+
- `el` - `<audio>` element DOM node.
70+
71+
The second argument is the state of the `<Audio>` component with the following signature.
72+
73+
```ts
74+
interface IAudioState {
75+
time?: number;
76+
duration?: number;
77+
isPlaying?: boolean;
78+
muted?: boolean;
79+
volume?: number;
80+
}
81+
```
82+
83+
, where
84+
85+
- `time` - current time in seconds.
86+
- `duration` - total audio track duration in seconds.
87+
- `isPlaying` - whether the audio track is currently playing.
88+
- `muted` - whether `muted` attribute is set on the audio element.
89+
- `volume` - current volume in range `[0..1]`.
90+
91+
92+
## Example
93+
94+
Play a sample audio track with control buttons.
95+
96+
```jsx
97+
<Audio src='https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3'>
98+
{({play, pause, seek, volume, mute, unmute}, state) =>
99+
<div>
100+
<button onClick={play}>Play</button>
101+
<button onClick={pause}>Pause</button>
102+
<button onClick={() => seek(state.time - 5)}>Seek -</button>
103+
<button onClick={() => seek(state.time + 5)}>Seek +</button>
104+
<button onClick={() => volume(state.volume - 0.05)}>Volume -</button>
105+
<button onClick={() => volume(state.volume + 0.05)}>Volume +</button>
106+
<button onClick={mute}>Mute</button>
107+
<button onClick={unmute}>Unmute</button>
108+
</div>
109+
}
110+
</Audio>
111+
```

0 commit comments

Comments
 (0)