forked from react-native-community/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add deeplinks to our documentation
These changes will only start to work once our documentation is updated to support this feature: - facebook/react-native-website#3618 - facebook/react-native-website#3619 - facebook/react-native-website#3620
- Loading branch information
Showing
19 changed files
with
254 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as link from '../doclink'; | ||
|
||
const mockPlatform = jest.fn().mockReturnValue('darwin'); | ||
jest.mock('os', () => ({ | ||
platform: mockPlatform, | ||
})); | ||
|
||
describe('link', () => { | ||
it('builds a link with the platform and os defined', () => { | ||
mockPlatform.mockReturnValueOnce('darwin'); | ||
link.setPlatform('android'); | ||
|
||
const url = new URL(link.docs('environment-setup')).toString(); | ||
expect(url).toMatch(/os=macos/); | ||
expect(url).toMatch(/platform=android/); | ||
expect(url).toEqual( | ||
expect.stringContaining('https://reactnative.dev/docs/environment-setup'), | ||
); | ||
|
||
// Handles a change of os | ||
mockPlatform.mockReturnValueOnce('win32'); | ||
expect(link.docs('environment-setup')).toMatch(/os=windows/); | ||
|
||
// Handles a change of platform | ||
link.setPlatform('ios'); | ||
expect(link.docs('environment-setup')).toMatch(/platform=ios/); | ||
}); | ||
|
||
it('preserves anchor-links', () => { | ||
expect(link.docs('environment-setup', 'ruby')).toMatch(/#ruby/); | ||
}); | ||
|
||
describe('overrides', () => { | ||
afterAll(() => link.setVersion(null)); | ||
it.each([ | ||
[{hash: 'ruby'}, /#ruby/], | ||
[{hash: 'ruby', os: 'linux'}, /os=linux/], | ||
[{platform: 'ios'}, /platform=ios/], | ||
[{'extra stuff': 'here?ok'}, /extra\+stuff=here%3Fok/], | ||
])("link.doc('environment-setup, %o) -> %o", (param, re) => { | ||
expect(link.docs('environment-setup', param)).toMatch(re); | ||
}); | ||
}); | ||
|
||
describe('versions', () => { | ||
afterAll(() => link.setVersion(null)); | ||
it('supports linking to a specific version of React Native', () => { | ||
link.setVersion('0.71'); | ||
expect(link.docs('environment-setup', 'ruby')).toEqual( | ||
expect.stringContaining( | ||
'https://reactnative.dev/docs/0.71/environment-setup', | ||
), | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import os from 'os'; | ||
import assert from 'assert'; | ||
|
||
type Platforms = 'android' | 'ios'; | ||
|
||
function getOS(): string { | ||
// Using os.platform instead of process.platform so we can test more easily. Once jest upgrades | ||
// to ^29.4 we could use process.platforms and jest.replaceProperty(process, 'platforms', 'someplatform'); | ||
switch (os.platform()) { | ||
case 'aix': | ||
case 'freebsd': | ||
case 'linux': | ||
case 'openbsd': | ||
case 'sunos': | ||
// King of controversy, right here. | ||
return 'linux'; | ||
case 'darwin': | ||
return 'macos'; | ||
case 'win32': | ||
return 'windows'; | ||
default: | ||
return ''; | ||
} | ||
} | ||
|
||
let _platform: Platforms = 'android'; | ||
let _version: string | undefined; | ||
|
||
interface Overrides { | ||
os?: string; | ||
platform?: string; | ||
hash?: string; | ||
version?: string; | ||
} | ||
|
||
interface Other { | ||
[key: string]: string; | ||
} | ||
|
||
/** | ||
* Create a deeplink to our documentation based on the user's OS and the Platform they're trying to build. | ||
*/ | ||
function doclink( | ||
section: string, | ||
path: string, | ||
hashOrOverrides?: string | (Overrides & Other), | ||
): string { | ||
const url = new URL('https://reactnative.dev/'); | ||
|
||
// Overrides | ||
const isObj = typeof hashOrOverrides === 'object'; | ||
|
||
const hash = isObj ? hashOrOverrides.hash : hashOrOverrides; | ||
const version = | ||
isObj && hashOrOverrides.version ? hashOrOverrides.version : _version; | ||
const OS = isObj && hashOrOverrides.os ? hashOrOverrides.os : getOS(); | ||
const platform = | ||
isObj && hashOrOverrides.platform ? hashOrOverrides.platform : _platform; | ||
|
||
url.pathname = _version | ||
? `${section}/${version}/${path}` | ||
: `${section}/${path}`; | ||
|
||
url.searchParams.set('os', OS); | ||
url.searchParams.set('platform', platform); | ||
|
||
if (isObj) { | ||
const otherKeys = Object.keys(hashOrOverrides).filter( | ||
(key) => !['hash', 'version', 'os', 'platform'].includes(key), | ||
); | ||
for (let key of otherKeys) { | ||
url.searchParams.set(key, hashOrOverrides[key]); | ||
} | ||
} | ||
|
||
if (hash) { | ||
assert.doesNotMatch( | ||
hash, | ||
/#/, | ||
"Anchor links should be written withou a '#'", | ||
); | ||
url.hash = hash; | ||
} | ||
|
||
return url.toString(); | ||
} | ||
|
||
export const docs = doclink.bind(null, 'docs'); | ||
export const contributing = doclink.bind(null, 'contributing'); | ||
export const community = doclink.bind(null, 'community'); | ||
export const showcase = doclink.bind(null, 'showcase'); | ||
export const blog = doclink.bind(null, 'blog'); | ||
|
||
/** | ||
* When the user builds, we should define the target platform globally. | ||
*/ | ||
export function setPlatform(target: Platforms): void { | ||
_platform = target; | ||
} | ||
|
||
/** | ||
* Can we figure out what version of react native they're using? | ||
*/ | ||
export function setVersion(reactNativeVersion: string): void { | ||
_version = reactNativeVersion; | ||
} |
Oops, something went wrong.