Skip to content

Commit 494818f

Browse files
authored
add helpers and components to resolve docs version automatically (#4817)
1 parent 4701254 commit 494818f

11 files changed

+149
-88
lines changed

docs/_integration-with-existing-apps-ios.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';
1+
import Tabs from '@theme/Tabs';
2+
import TabItem from '@theme/TabItem';
3+
import constants from '@site/core/TabsConstants';
4+
import CodeBlock from '@theme/CodeBlock';
5+
import RNTemplateRepoLink from '@site/core/RNTemplateRepoLink';
6+
import {getTemplateBranchNameForCurrentVersion} from '@site/src/getTemplateBranchNameForCurrentVersion';
27

38
## Key Concepts
49

@@ -28,11 +33,11 @@ To ensure a smooth experience, create a new folder for your integrated React Nat
2833

2934
Go to the root directory and run the following command:
3035

31-
```shell
32-
curl -O https://raw.githubusercontent.com/react-native-community/template/refs/heads/0.78-stable/template/package.json
33-
```
36+
<CodeBlock language="bash" title="shell">
37+
{`curl -O https://raw.githubusercontent.com/react-native-community/template/refs/heads/${getTemplateBranchNameForCurrentVersion()}/template/package.json`}
38+
</CodeBlock>
3439

35-
This will copy the `package.json` [file from the Community template](https://github.com/react-native-community/template/blob/0.78-stable/template/package.json) to your project.
40+
This will copy the `package.json` <RNTemplateRepoLink href="template/package.json">file from the Community template</RNTemplateRepoLink> to your project.
3641

3742
Next, install the NPM packages by running:
3843

@@ -55,7 +60,7 @@ yarn install
5560

5661
Installation process has created a new `node_modules` folder. This folder stores all the JavaScript dependencies required to build your project.
5762

58-
Add `node_modules/` to your `.gitignore` file (here the [Community default one](https://github.com/react-native-community/template/blob/0.78-stable/template/_gitignore)).
63+
Add `node_modules/` to your `.gitignore` file (here the <RNTemplateRepoLink href="template/_gitignore">Community default one</RNTemplateRepoLink>).
5964

6065
### 3. Install Development tools
6166

@@ -86,9 +91,9 @@ To configure CocoaPods, we need two files:
8691

8792
For the **Gemfile**, go to the root directory of your project and run this command
8893

89-
```sh
90-
curl -O https://raw.githubusercontent.com/react-native-community/template/refs/heads/0.78-stable/template/Gemfile
91-
```
94+
<CodeBlock language="bash" title="shell">
95+
{`curl -O https://raw.githubusercontent.com/react-native-community/template/refs/heads/${getTemplateBranchNameForCurrentVersion()}/template/Gemfile`}
96+
</CodeBlock>
9297

9398
This will download the Gemfile from the template.
9499

@@ -108,14 +113,14 @@ Xcode 16 generates a project in a slightly different ways from previous versions
108113

109114
Similarly, for the **Podfile**, go to the `ios` folder of your project and run
110115

111-
```sh
112-
curl -O https://raw.githubusercontent.com/react-native-community/template/refs/heads/0.78-stable/template/ios/Podfile
113-
```
116+
<CodeBlock language="bash" title="shell">
117+
{`curl -O https://raw.githubusercontent.com/react-native-community/template/refs/heads/${getTemplateBranchNameForCurrentVersion()}/template/ios/Podfile`}
118+
</CodeBlock>
114119

115-
Please use the Community Template as a reference point for the [Gemfile](https://github.com/react-native-community/template/blob/0.78-stable/template/Gemfile) and for the [Podfile](https://github.com/react-native-community/template/blob/0.78-stable/template/ios/Podfile).
120+
Please use the Community Template as a reference point for the <RNTemplateRepoLink href="template/Gemfile">Gemfile</RNTemplateRepoLink> and for the <RNTemplateRepoLink href="template/ios/Podfile">Podfile</RNTemplateRepoLink>.
116121

117122
:::note
118-
Remember to change [this line](https://github.com/react-native-community/template/blob/0.78-stable/template/ios/Podfile#L17).
123+
Remember to change <RNTemplateRepoLink href="template/ios/Podfile#L17">this line</RNTemplateRepoLink>.
119124
:::
120125

121126
Now, we need to run a couple of extra commands to install the Ruby gems and the Pods.
@@ -140,7 +145,7 @@ First, create an empty `index.js` file in the root of your React Native project.
140145

141146
`index.js` is the starting point for React Native applications, and it is always required. It can be a small file that `import`s other file that are part of your React Native component or application, or it can contain all the code that is needed for it.
142147

143-
Our `index.js` should look as follows (here the [Community template file as reference](https://github.com/react-native-community/template/blob/0.78-stable/template/index.js)):
148+
Our `index.js` should look as follows (here the <RNTemplateRepoLink href="template/index.js">Community template file as reference</RNTemplateRepoLink>):
144149

145150
```js
146151
import {AppRegistry} from 'react-native';
@@ -151,7 +156,7 @@ AppRegistry.registerComponent('HelloWorld', () => App);
151156

152157
### Create a `App.tsx` file
153158

154-
Let's create an `App.tsx` file. This is a [TypeScript](https://www.typescriptlang.org/) file that can have [JSX](<https://en.wikipedia.org/wiki/JSX_(JavaScript)>) expressions. It contains the root React Native component that we will integrate into our iOS application ([link](https://github.com/react-native-community/template/blob/0.78-stable/template/App.tsx)):
159+
Let's create an `App.tsx` file. This is a [TypeScript](https://www.typescriptlang.org/) file that can have [JSX](<https://en.wikipedia.org/wiki/JSX_(JavaScript)>) expressions. It contains the root React Native component that we will integrate into our iOS application (<RNTemplateRepoLink href="template/App.tsx">link</RNTemplateRepoLink>):
155160

156161
```tsx
157162
import React from 'react';
@@ -224,7 +229,7 @@ const styles = StyleSheet.create({
224229
export default App;
225230
```
226231

227-
Here the [Community template file as reference](https://github.com/react-native-community/template/blob/0.78-stable/template/App.tsx)
232+
Here is the <RNTemplateRepoLink href="template/App.tsx">Community template file as reference</RNTemplateRepoLink>.
228233

229234
## 5. Integrating with your iOS code
230235

@@ -460,15 +465,15 @@ const {getDefaultConfig} = require('@react-native/metro-config');
460465
module.exports = getDefaultConfig(__dirname);
461466
```
462467

463-
You can checkout the [metro.config.js file](https://github.com/react-native-community/template/blob/0.78-stable/template/metro.config.js) from the Community template file as reference.
468+
You can checkout the <RNTemplateRepoLink href="template/metro.config.js">`metro.config.js` file</RNTemplateRepoLink> from the Community template file as reference.
464469

465470
Then, you need to create a `.watchmanconfig` file in the root of your project. The file must contain an empty json object:
466471

467472
```sh
468473
echo {} > .watchmanconfig
469474
```
470475

471-
Once you have the configurations file in place, you can run the bundler. Run the following command in the root directory of your project:
476+
Once you have the configuration file in place, you can run the bundler. Run the following command in the root directory of your project:
472477

473478
<Tabs groupId="package-manager" queryString defaultValue={constants.defaultPackageManager} values={constants.packageManagers}>
474479
<TabItem value="npm">

docs/_integration-with-existing-apps-kotlin.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';
1+
import Tabs from '@theme/Tabs';
2+
import TabItem from '@theme/TabItem';
3+
import constants from '@site/core/TabsConstants';
4+
import CodeBlock from '@theme/CodeBlock';
5+
import RNTemplateRepoLink from '@site/core/RNTemplateRepoLink';
6+
import {getTemplateBranchNameForCurrentVersion} from '@site/src/getTemplateBranchNameForCurrentVersion';
27

38
## Key Concepts
49

@@ -28,11 +33,11 @@ To ensure a smooth experience, create a new folder for your integrated React Nat
2833

2934
Go to the root directory and run the following command:
3035

31-
```shell
32-
curl -O https://raw.githubusercontent.com/react-native-community/template/refs/heads/0.75-stable/template/package.json
33-
```
36+
<CodeBlock language="bash" title="shell">
37+
{`curl -O https://raw.githubusercontent.com/react-native-community/template/refs/heads/${getTemplateBranchNameForCurrentVersion()}/template/package.json`}
38+
</CodeBlock>
3439

35-
This will copy the `package.json` [file from the Community template](https://github.com/react-native-community/template/blob/0.75-stable/template/package.json) to your project.
40+
This will copy the `package.json` <RNTemplateRepoLink href="template/package.json">file from the Community template</RNTemplateRepoLink> to your project.
3641

3742
Next, install the NPM packages by running:
3843

@@ -55,15 +60,15 @@ yarn install
5560

5661
Installation process has created a new `node_modules` folder. This folder stores all the JavaScript dependencies required to build your project.
5762

58-
Add `node_modules/` to your `.gitignore` file (here the [Community default one](https://github.com/react-native-community/template/blob/0.75-stable/template/_gitignore)).
63+
Add `node_modules/` to your `.gitignore` file (here the <RNTemplateRepoLink href="template/_gitignore">Community default one</RNTemplateRepoLink>).
5964

6065
## 3. Adding React Native to your app
6166

6267
### Configuring Gradle
6368

6469
React Native uses the React Native Gradle Plugin to configure your dependencies and project setup.
6570

66-
First, let's edit your `settings.gradle` file by adding those lines (as suggested from the [Community template](https://github.com/react-native-community/template/blob/0.77-stable/template/android/settings.gradle)):
71+
First, let's edit your `settings.gradle` file by adding those lines (as suggested from the <RNTemplateRepoLink href="template/android/settings.gradle">Community template</RNTemplateRepoLink>):
6772

6873
```groovy
6974
// Configures the React Native Gradle Settings plugin used for autolinking
@@ -78,7 +83,7 @@ includeBuild("../node_modules/@react-native/gradle-plugin")
7883
// include(":app")
7984
```
8085

81-
Then you need to open your top level `build.gradle` and include this line (as suggested from the [Community template](https://github.com/react-native-community/template/blob/0.77-stable/template/android/build.gradle)):
86+
Then you need to open your top level `build.gradle` and include this line (as suggested from the <RNTemplateRepoLink href="template/android/build.gradle">Community template</RNTemplateRepoLink>):
8287

8388
```diff
8489
buildscript {
@@ -94,7 +99,7 @@ buildscript {
9499
```
95100

96101
This makes sure the React Native Gradle Plugin (RNGP) is available inside your project.
97-
Finally, add those lines inside your Applications's `build.gradle` file (it's a different `build.gradle` file usually inside your `app` folder - you can use the [Community template file as reference](https://github.com/react-native-community/template/blob/0.77-stable/template/android/app/build.gradle)):
102+
Finally, add those lines inside your Applications's `build.gradle` file (it's a different `build.gradle` file usually inside your `app` folder - you can use the <RNTemplateRepoLink href="template/android/build.gradle">Community template file as reference</RNTemplateRepoLink>):
98103

99104
```diff
100105
apply plugin: "com.android.application"
@@ -118,7 +123,7 @@ dependencies {
118123
+}
119124
```
120125

121-
Finally, open your application `gradle.properties` files and add the following line (here the [Community template file as reference](https://github.com/react-native-community/template/blob/0.77-stable/template/android/gradle.properties)):
126+
Finally, open your application `gradle.properties` files and add the following line (here the <RNTemplateRepoLink href="template/android/gradle.properties">Community template file as reference</RNTemplateRepoLink>):
122127

123128
```diff
124129
+reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
@@ -155,7 +160,7 @@ Then you need to enable [cleartext traffic](https://developer.android.com/traini
155160
</manifest>
156161
```
157162

158-
As usual, here the AndroidManifest.xml file from the Community template to use as a reference: [main](https://github.com/react-native-community/template/blob/0.77-stable/template/android/app/src/main/AndroidManifest.xml) and [debug](https://github.com/react-native-community/template/blob/0.77-stable/template/android/app/src/debug/AndroidManifest.xml)
163+
As usual, here the AndroidManifest.xml file from the Community template to use as a reference: <RNTemplateRepoLink href="template/android/app/src/main/AndroidManifest.xml">main</RNTemplateRepoLink> and <RNTemplateRepoLink href="template/android/app/src/debug/AndroidManifest.xml">debug</RNTemplateRepoLink>.
159164

160165
This is needed as your application will communicate with your local bundler, [Metro](https://metrobundler.dev/), via HTTP.
161166

@@ -173,7 +178,7 @@ First, create an empty `index.js` file in the root of your React Native project.
173178

174179
`index.js` is the starting point for React Native applications, and it is always required. It can be a small file that `import`s other file that are part of your React Native component or application, or it can contain all the code that is needed for it.
175180

176-
Our index.js should look as follows (here the [Community template file as reference](https://github.com/react-native-community/template/blob/0.77-stable/template/index.js)):
181+
Our index.js should look as follows (here the <RNTemplateRepoLink href="template/index.js">Community template file as reference</RNTemplateRepoLink>):
177182

178183
```js
179184
import {AppRegistry} from 'react-native';
@@ -184,7 +189,7 @@ AppRegistry.registerComponent('HelloWorld', () => App);
184189

185190
### Create a `App.tsx` file
186191

187-
Let's create an `App.tsx` file. This is a [TypeScript](https://www.typescriptlang.org/) file that can have [JSX](<https://en.wikipedia.org/wiki/JSX_(JavaScript)>) expressions. It contains the root React Native component that we will integrate into our Android application ([link](https://github.com/react-native-community/template/blob/0.77-stable/template/App.tsx)):
192+
Let's create an `App.tsx` file. This is a [TypeScript](https://www.typescriptlang.org/) file that can have [JSX](<https://en.wikipedia.org/wiki/JSX_(JavaScript)>) expressions. It contains the root React Native component that we will integrate into our Android application (<RNTemplateRepoLink href="template/App.tsx">link</RNTemplateRepoLink>):
188193

189194
```tsx
190195
import React from 'react';
@@ -257,7 +262,7 @@ const styles = StyleSheet.create({
257262
export default App;
258263
```
259264

260-
Here the [Community template file as reference](https://github.com/react-native-community/template/blob/0.77-stable/template/App.tsx)
265+
Here is the <RNTemplateRepoLink href="template/App.tsx">Community template file as reference</RNTemplateRepoLink>.
261266

262267
## 5. Integrating with your Android code
263268

@@ -368,7 +373,7 @@ import android.app.Application
368373
</TabItem>
369374
</Tabs>
370375

371-
As usual, here the [MainApplication.kt Community template file as reference](https://github.com/react-native-community/template/blob/0.77-stable/template/android/app/src/main/java/com/helloworld/MainApplication.kt)
376+
As usual, here the <RNTemplateRepoLink href="template/android/app/src/main/java/com/helloworld/MainApplication.kt">`MainApplication.kt` Community template file as reference</RNTemplateRepoLink>.
372377

373378
#### Creating a `ReactActivity`
374379

@@ -424,7 +429,7 @@ class MyReactActivity : ReactActivity() {
424429
</TabItem>
425430
</Tabs>
426431

427-
As usual, here the [MainActivity.kt Community template file as reference](https://github.com/react-native-community/template/blob/0.80-stable/template/android/app/src/main/java/com/helloworld/MainActivity.kt)
432+
As usual, here the <RNTemplateRepoLink href="template/android/app/src/main/java/com/helloworld/MainActivity.kt">`MainActivity.kt` Community template file as reference</RNTemplateRepoLink>.
428433

429434
Whenever you create a new Activity, you need to add it to your `AndroidManifest.xml` file. You also need set the theme of `MyReactActivity` to `Theme.AppCompat.Light.NoActionBar` (or to any non-ActionBar theme) as otherwise your application will render an ActionBar on top of your React Native screen:
430435

@@ -458,9 +463,9 @@ const {getDefaultConfig} = require('@react-native/metro-config');
458463
module.exports = getDefaultConfig(__dirname);
459464
```
460465

461-
You can checkout the [metro.config.js file](https://github.com/react-native-community/template/blob/0.77-stable/template/metro.config.js) from the Community template file as reference.
466+
You can checkout the <RNTemplateRepoLink href="template/metro.config.js">`metro.config.js` file</RNTemplateRepoLink> from the Community template file as reference.
462467

463-
Once you have the config file in place, you can run the bundler. Run the following command in the root directory of your project:
468+
Once you have the configuration file in place, you can run the bundler. Run the following command in the root directory of your project:
464469

465470
<Tabs groupId="package-manager" queryString defaultValue={constants.defaultPackageManager} values={constants.packageManagers}>
466471
<TabItem value="npm">

docs/the-new-architecture/using-codegen.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import CodeBlock from '@theme/CodeBlock';
2+
import {getCurrentVersion} from '@site/src/getCurrentVersion';
3+
14
# Using Codegen
25

36
This guide teaches how to:
@@ -15,9 +18,9 @@ The **Codegen** process is tightly coupled with the build of the app, and the sc
1518

1619
For the sake of this guide, create a project using the React Native CLI as follows:
1720

18-
```bash
19-
npx @react-native-community/cli@latest init SampleApp --version 0.76.0
20-
```
21+
<CodeBlock language="bash" title="shell">
22+
{`npx @react-native-community/cli@latest init SampleApp --version ${getCurrentVersion()}`}
23+
</CodeBlock>
2124

2225
**Codegen** is used to generate the glue-code for your custom modules or components. See the guides for Turbo Native Modules and Fabric Native Components for more details on how to create them.
2326

docs/turbo-native-modules.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ id: turbo-native-modules-introduction
33
title: 'Native Modules: Introduction'
44
---
55

6-
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';
6+
import Tabs from '@theme/Tabs';
7+
import TabItem from '@theme/TabItem';
8+
import constants from '@site/core/TabsConstants';
9+
import CodeBlock from '@theme/CodeBlock';
10+
import {getCurrentVersion} from '@site/src/getCurrentVersion';
711
import {TurboNativeModulesAndroid, TurboNativeModulesIOS} from './\_turbo-native-modules-components';
812

913
# Native Modules
@@ -19,9 +23,9 @@ The basic steps are:
1923

2024
Lets work through each of these steps by building an example Turbo Native Module. The rest of this guide assume that you have created your application running the command:
2125

22-
```shell
23-
npx @react-native-community/cli@latest init TurboModuleExample --version 0.76.0
24-
```
26+
<CodeBlock language="bash" title="shell">
27+
{`npx @react-native-community/cli@latest init TurboModuleExample --version ${getCurrentVersion()}`}
28+
</CodeBlock>
2529

2630
## Native Persistent Storage
2731

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import A from '@theme/MDXComponents/A';
2+
import type {ComponentProps} from 'react';
3+
import {getTemplateBranchNameForCurrentVersion} from '../src/getTemplateBranchNameForCurrentVersion';
4+
5+
type Props = ComponentProps<'a'>;
6+
7+
export default function RNTemplateRepoLink({href, children, ...rest}: Props) {
8+
return (
9+
<A
10+
href={`https://github.com/react-native-community/template/blob/${getTemplateBranchNameForCurrentVersion()}/${href.startsWith('/') ? href.slice(1) : href}`}
11+
{...rest}>
12+
{children}
13+
</A>
14+
);
15+
}

website/src/getCurrentVersion.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {useActiveVersion} from '@docusaurus/plugin-content-docs/client';
2+
3+
export function getCurrentVersion() {
4+
const version = useActiveVersion(undefined);
5+
return version.label === 'Next' ? 'latest' : version.label;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {useActiveVersion} from '@docusaurus/plugin-content-docs/client';
2+
3+
export function getTemplateBranchNameForCurrentVersion() {
4+
const version = useActiveVersion(undefined);
5+
return version.label === 'Next' ? 'main' : `${version.label}-stable`;
6+
}

0 commit comments

Comments
 (0)