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
18 changed files
with
220 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as link from '../doclink'; | ||
|
||
const mockPlatform = jest.fn(); | ||
jest.mock('os', () => ({ | ||
platform: mockPlatform, | ||
})); | ||
|
||
describe('link', () => { | ||
it('builds a link with the platform and os defined', () => { | ||
mockPlatform.mockReturnValue('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.mockReturnValue('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('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,70 @@ | ||
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 | null = null; | ||
|
||
/** | ||
* 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, hash?: string): string { | ||
const url = new URL('https://reactnative.dev/'); | ||
url.pathname = version | ||
? `${section}/${version}/${path}` | ||
: `${section}/${path}`; | ||
url.searchParams.set('os', getOS()); | ||
url.searchParams.set('platform', platform); | ||
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; | ||
} |
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,28 @@ | ||
const stringLength = require('string-length'); | ||
|
||
const testsFromStringLengthLibrary = [ | ||
['', 0], | ||
['\u001B[1m\u001B[22m', 0], | ||
// countAnsiEscapeCodes: true | ||
//['\u001B[1m\u001B[22m', 9], | ||
['𠀔', 1], | ||
['foo𠁐bar𠀃', 8], | ||
['あ', 1], | ||
['谢', 1], | ||
['🐴', 1], | ||
['𝌆', 1], | ||
['\u001B[1mfoo\u001B[22m', 3], | ||
// countAnsiEscapeCodes: true | ||
//['\u001B[1mfoo\u001B[22m', 12], | ||
['❤️', 1], | ||
['👊🏽', 1], | ||
['🏴❤️谢👪', 4], | ||
['\u001B[1m👩👧👦°✿\u001B[22m', 3], | ||
// countAnsiEscapeCodes: true | ||
//['\u001B[1m👩👧👦°✿\u001B[22m', 12], | ||
]; | ||
describe('stringLength', () => { | ||
it.each(testsFromStringLengthLibrary)("'%s'.length === %i", (str, length) => { | ||
expect(stringLength(str)).toEqual(length); | ||
}); | ||
}); |
Oops, something went wrong.