Skip to content

Commit e5b10f0

Browse files
authored
docs: support browser console based exploration (#3168)
1 parent df96fba commit e5b10f0

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

docs/.vitepress/config.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import type { DefaultTheme } from 'vitepress/theme';
33
import { apiPages } from './api-pages';
44
import {
55
algoliaIndex,
6-
currentVersion,
6+
version,
77
versionBannerInfix,
8+
versionLabel,
89
versionLinks,
910
} from './versions';
1011

@@ -149,6 +150,26 @@ const config: UserConfig<DefaultTheme.Config> = {
149150
href: 'https://fosstodon.org/@faker_js',
150151
},
151152
],
153+
[
154+
'script',
155+
{
156+
id: 'browser-console-faker',
157+
},
158+
`
159+
const logStyle = 'background: rgba(16, 183, 127, 0.14); color: rgba(255, 255, 245, 0.86); padding: 0.5rem; display: inline-block;';
160+
console.log(\`%cIf you would like to test Faker in the browser console, you can do so using 'await enableFaker()'.
161+
If you would like to test Faker in a playground, visit https://new.fakerjs.dev.\`, logStyle);
162+
async function enableFaker() {
163+
const imported = await import('https://cdn.jsdelivr.net/npm/@faker-js/faker@${version}/+esm');
164+
Object.assign(globalThis, imported);
165+
console.log(\`%cYou can now start using Faker v${version}:
166+
e.g. 'faker.food.description()' or 'fakerZH_CN.person.firstName()'
167+
For other languages please refer to https://fakerjs.dev/guide/localization.html#available-locales
168+
For a full list of all methods please refer to https://fakerjs.dev/api/\`, logStyle);
169+
return imported;
170+
}
171+
`,
172+
],
152173
],
153174

154175
themeConfig: {
@@ -197,7 +218,10 @@ const config: UserConfig<DefaultTheme.Config> = {
197218
},
198219
{
199220
text: 'Try it',
200-
items: [{ text: 'StackBlitz ', link: 'https://fakerjs.dev/new' }],
221+
items: [
222+
{ text: 'StackBlitz ', link: 'https://fakerjs.dev/new' },
223+
{ text: 'Browser Console ', link: '/guide/usage.html#browser' },
224+
],
201225
},
202226
{
203227
text: 'About',
@@ -222,7 +246,7 @@ const config: UserConfig<DefaultTheme.Config> = {
222246
],
223247
},
224248
{
225-
text: currentVersion,
249+
text: versionLabel,
226250
items: [
227251
{
228252
text: 'Release Notes',

docs/.vitepress/versions.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { execSync } from 'node:child_process';
22
import * as semver from 'semver';
3-
import { version } from '../../package.json';
3+
import { version as version_ } from '../../package.json';
44

55
function readBranchName(): string {
66
return (
@@ -39,6 +39,11 @@ const {
3939
const otherVersions = readOtherLatestReleaseTagNames();
4040
const isReleaseBranch = /^v\d+$/.test(branchName);
4141

42+
/**
43+
* The text of the version banner describing the current version.
44+
*
45+
* This is `null` in production and thus should not be displayed.
46+
*/
4247
export const versionBannerInfix: string | null = (() => {
4348
if (deployContext === 'production') {
4449
return null;
@@ -55,7 +60,19 @@ export const versionBannerInfix: string | null = (() => {
5560
return '"a development version"';
5661
})();
5762

58-
export const currentVersion = isReleaseBranch ? `v${version}` : branchName;
63+
/**
64+
* The current version of Faker from package.json.
65+
*/
66+
export const version = version_;
67+
68+
/**
69+
* The version label to display in the top-right corner of the site.
70+
*/
71+
export const versionLabel = isReleaseBranch ? `v${version}` : branchName;
72+
73+
/**
74+
* The links to other versions of the documentation.
75+
*/
5976
export const versionLinks = [
6077
{
6178
version: 'next',
@@ -69,6 +86,9 @@ export const versionLinks = [
6986
// Don't link to the current branch's version.
7087
.filter(({ link }) => link !== `https://${branchName}.fakerjs.dev/`);
7188

89+
/**
90+
* The name of the Algolia index to use for search.
91+
*/
7292
export const algoliaIndex = isReleaseBranch
7393
? `fakerjs-v${semver.major(version)}`
7494
: 'fakerjs-next';

docs/guide/usage.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ For more information on selecting and customizing a locale, please refer to our
3232

3333
If you want to try it yourself, you can open your browser console via `Ctrl + Shift + J` / `F12`.
3434

35+
On our website, you can load faker into the browser console
36+
37+
- by using `await enableFaker()`
38+
- or using the following code:
39+
3540
```js
3641
const { faker } = await import('https://esm.sh/@faker-js/faker');
3742

0 commit comments

Comments
 (0)